Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Help parsing an array into nodes?
Old 05-15-2008, 09:52 PM Help parsing an array into nodes?
Inet411's Avatar
Skilled Talker

Posts: 88
Name: programmer
Location: internet
Trades: 0
The title is kind of misleading. I can't really explain in words what I'm looking for so I'll give some examples.

Here is a print out of my array:

array ( [0] => 9 [1] => 7 [2] => 3 [3] => 1 )

What I need to turn that into is:

1
13
137
1379

Now the array is dynamically generated and may be longer or shorter in number of keys/values.

Here would be another example:

array ( [0] => 8 [1] => 6 [2] => 1 )

would need to be
1
16
168

I'm sure their is a simple loop or something but I've been trying to do this for a half an hour now and just can't get it. Any help would be appreciated.

the end result can be in multiple arrays or strings or just an echo of each....

Thanks,
Rob

p.s. I apologize for just flat out asking for someone to write me code, but in my defense this is the first time I've ever posted a question and not an answer. Thanks in advance.
__________________

Please login or register to view this content. Registration is FREE


Last edited by Inet411; 05-15-2008 at 10:04 PM..
Inet411 is offline
Reply With Quote
View Public Profile Visit Inet411's homepage!
 
 
Register now for full access!
Old 05-16-2008, 03:06 AM Re: Help parsing an array into nodes?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
first point, as I see here, you need to reverse the order of your arrays.
To do so, use array_reverse() [ http://www.php.net/manual/en/function.array-reverse.php ]
PHP Code:
$ary=array(8,6,1);
$ary=array_reverse($ary);
print_r($ary)
/*should output
array( [0]=> 1 [1]=>6 [2]=>8 )
*/ 
And now, to concatenate everything, use a simple foreach loop:
http://www.php.net/manual/en/control...es.foreach.php
PHP Code:
$output=null;
foreach(
$ary as $key=>$value){
  
$output.=$value;  //Add $value to $output for each iterations over the array
  /*
  To display the value with each number added one after the other.
  I have to admit I don't see the logic in that one, but it's your need...
  */
  
echo $output;

__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 05-16-2008 at 03:08 AM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 05-16-2008, 10:38 AM Re: Help parsing an array into nodes?
Inet411's Avatar
Skilled Talker

Posts: 88
Name: programmer
Location: internet
Trades: 0
Thanks Tripy,

I've already tried something similar to this already.

//Your code outputs
116168

//Which is what I want (sortof) but I need it like so
1 16 168
or
1
16
168

There needs to be a break, space or some sort of delimiter. Thats where I'm running into the problem.

I've tried:
PHP Code:
$ary=array(8,6,1);
$ary=array_reverse($ary);
$output=null;
foreach(
$ary as $key=>$value){
       
$output.="|".$value;
       echo 
$output;

Which yields:
|1|1|6|1|6|8
But I would need something more like:
1|16|168
OR: returning an array is fine also. ie. ( [0] => 1 [1] => 16 [2] => 168 )

and
PHP Code:
$ary=array(8,6,1);
$ary=array_reverse($ary);
$output=null;
foreach(
$ary as $key=>$value){
       
$output.=$value."<br />";
       echo 
$output;

Which yields:
1
1
6
1
6
8
But I would need something like:
1
16
168
OR: returning an array. ie. ( [0] => 1 [1] => 16 [2] => 168 )
__________________

Please login or register to view this content. Registration is FREE


Last edited by Inet411; 05-16-2008 at 10:52 AM..
Inet411 is offline
Reply With Quote
View Public Profile Visit Inet411's homepage!
 
Old 05-16-2008, 11:03 AM Re: Help parsing an array into nodes?
Inet411's Avatar
Skilled Talker

Posts: 88
Name: programmer
Location: internet
Trades: 0
Nevermind.

I got it:

PHP Code:
$ary=array(8,6,1);
$ary=array_reverse($ary);
foreach(
$ary as $key=>$value){
$temp.=$value;
$output[$key]=$temp;
}  

print_r($output); 
Gives me:
Array ( [0] => 1 [1] => 16 [2] => 168 )
__________________

Please login or register to view this content. Registration is FREE

Inet411 is offline
Reply With Quote
View Public Profile Visit Inet411's homepage!
 
Old 05-16-2008, 11:56 AM Re: Help parsing an array into nodes?
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Just a bit of an optimization:

PHP Code:
<?php
$my_array 
= array(9,7,3,1);

$output_string '';
for (
$counter=count($my_array)-1;$counter > -1$counter--) {
  
$output_string .= $my_array[$counter];
  echo 
$output_string.'<br />';
}
?>
No real need for the array_reverse.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 05-16-2008, 12:56 PM Re: Help parsing an array into nodes?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
True...
I admit that I never needed array_reverse() in almost 10 years, but so for the negative for loop.
Which I completely forgot about...
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 05-16-2008, 01:01 PM Re: Help parsing an array into nodes?
Inet411's Avatar
Skilled Talker

Posts: 88
Name: programmer
Location: internet
Trades: 0
Quote:
Originally Posted by JeremyMiller View Post
Just a bit of an optimization:

PHP Code:
<?php
$my_array 
= array(9,7,3,1);

$output_string '';
for (
$counter=count($my_array)-1;$counter > -1$counter--) {
  
$output_string .= $my_array[$counter];
  echo 
$output_string.'<br />';
}
?>
No real need for the array_reverse.

Thanks Mr. Miller. Thats it exactly. I was going for a loop originally (if you read the end of my first post) but I went in another direction and got lost. This is perfect. Thank you.
__________________

Please login or register to view this content. Registration is FREE

Inet411 is offline
Reply With Quote
View Public Profile Visit Inet411's homepage!
 
Old 05-16-2008, 01:30 PM Re: Help parsing an array into nodes?
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
array_reverse is something I've had to use; for example, when I use a string as the key and need to flip it over. It's rare, however, that I have to use it too.

Glad the code helped.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Reply     « Reply to Help parsing an array into nodes?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.49808 seconds with 12 queries