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: 82
Name: programmer
Location: internet
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.

Last edited by Inet411 : 05-15-2008 at 10:04 PM.
Inet411 is offline
Reply With Quote
View Public Profile Visit Inet411's homepage!
 
When You Register, These Ads Go Away!
     
Old 05-16-2008, 03:06 AM Re: Help parsing an array into nodes?
tripy's Avatar
Fetchez la vache!

Posts: 1,989
Name: Thierry
Location: In the void
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;

__________________
Listen to the ducky: "This is awesome!!!"


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

Posts: 82
Name: programmer
Location: internet
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 )

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: 82
Name: programmer
Location: internet
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 )
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
Full-Time TeraTasker

Posts: 955
Name: Jeremy Miller
Location: Reno, NV
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 - TeraTask Technologies, LLC
Content Farmer - Automated Posting for Content & Blog Sites
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
Fetchez la vache!

Posts: 1,989
Name: Thierry
Location: In the void
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...
__________________
Listen to the ducky: "This is awesome!!!"

tripy is offline
Reply With Quote
View Public Profile
 
Old 05-16-2008, 01:01 PM Re: Help parsing an array into nodes?
Inet411's Avatar
Skilled Talker

Posts: 82
Name: programmer
Location: internet
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.
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
Full-Time TeraTasker

Posts: 955
Name: Jeremy Miller
Location: Reno, NV
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 - TeraTask Technologies, LLC
Content Farmer - Automated Posting for Content & Blog Sites
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Reply     « Reply to Help parsing an array into nodes?
 

Thread Tools

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

vB 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.16606 seconds with 13 queries