471,596 Members | 786 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,596 software developers and data experts.

How to print multidimensional array?

rahulephp
Hi
I need to print below array in the following manner.

Output would be:

Property Name: 1
Property Address: 1
Price: 1
Property Size: 1
URL 1

Property Name: 2
Property Address: 2
Price: 2
Property Size: 2
URL 2

Here is array:
Expand|Select|Wrap|Line Numbers
  1.  
  2. Array
  3. (
  4.     [temp_property_name] => Array
  5.         (
  6.             [0] => Property Name: 1
  7.             [1] => Property Name: 2
  8.         )
  9.  
  10.     [temp_property_add] => Array
  11.         (
  12.             [0] => Property Address: 1
  13.             [1] => Property Address: 2
  14.         )
  15.  
  16.     [temp_property_price] => Array
  17.         (
  18.             [0] => Price: 1
  19.             [1] => Price: 2
  20.         )
  21.  
  22.     [temp_property_size] => Array
  23.         (
  24.             [0] => Property Size: 1
  25.             [1] => Property Size: 2
  26.         )
  27.  
  28.     [temp_property_detail] => Array
  29.         (
  30.             [0] => URL 1
  31.             [1] => URL 2
  32.         )
  33.  
  34. )
  35.  
Please let me know, how would be this possible?
Dec 9 '09 #1

✓ answered by rahulephp

Hi Again;

You were write:
The actual solution for this would be

Expand|Select|Wrap|Line Numbers
  1. $child_count = count($temp_property_detail['temp_property_name']);
  2.  
  3.     //db($child_count);
  4.     //exit;
  5.  
  6.      for($i = 0; $i <= child_count+1; $i++)
  7.      {
  8.          echo $temp_property_detail['temp_property_name'][$i].'<br/>';
  9.        echo $temp_property_detail['temp_property_add'][$i].'<br/>';
  10.      echo $temp_property_detail['temp_property_price'][$i].'<br/>';
  11.      echo $temp_property_detail['temp_property_size'][$i].'<br/>';
  12.      echo $temp_property_detail['temp_property_detail'][$i].'<br/>';
  13.      echo '<br/>';
  14.      }
  15.  
  16.  
And the output would be:

Expand|Select|Wrap|Line Numbers
  1. Debug:
  2.  
  3. Property Name: 1
  4. Property Address: 1
  5. Price: 1
  6. Property Size: 1
  7. URL 1
  8.  
  9. Property Name: 2
  10. Property Address: 2
  11. Price: 2
  12. Property Size: 2
  13. URL 2
  14.  
But can we do this using FOREACH Loop???

5 2402
Atli
5,058 Expert 4TB
Hey.

You could pick one of the elements, like temp_property_name, and count how many children it has. Then you could use a for loop or a while loop to count up to that number, printing the child at that position in all the elements.

For example:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $child_count = count($array['temp_property_name']);
  3. for($i = 0; $i < $child_count; ++$i)
  4. {
  5.     echo $array['temp_property_name'][$i];
  6.     echo $array['temp_property_add'][$i];
  7.     // etc...
  8. }
  9. ?>
Dec 9 '09 #2
Hey thanks buddy for your quick reply
i'll check this and get back to you soon.
hope it'll be helpful for me
Dec 9 '09 #3
Hi Again;

You were write:
The actual solution for this would be

Expand|Select|Wrap|Line Numbers
  1. $child_count = count($temp_property_detail['temp_property_name']);
  2.  
  3.     //db($child_count);
  4.     //exit;
  5.  
  6.      for($i = 0; $i <= child_count+1; $i++)
  7.      {
  8.          echo $temp_property_detail['temp_property_name'][$i].'<br/>';
  9.        echo $temp_property_detail['temp_property_add'][$i].'<br/>';
  10.      echo $temp_property_detail['temp_property_price'][$i].'<br/>';
  11.      echo $temp_property_detail['temp_property_size'][$i].'<br/>';
  12.      echo $temp_property_detail['temp_property_detail'][$i].'<br/>';
  13.      echo '<br/>';
  14.      }
  15.  
  16.  
And the output would be:

Expand|Select|Wrap|Line Numbers
  1. Debug:
  2.  
  3. Property Name: 1
  4. Property Address: 1
  5. Price: 1
  6. Property Size: 1
  7. URL 1
  8.  
  9. Property Name: 2
  10. Property Address: 2
  11. Price: 2
  12. Property Size: 2
  13. URL 2
  14.  
But can we do this using FOREACH Loop???
Dec 9 '09 #4
Dormilich
8,656 Expert Mod 8TB
you could certainly do this, but IMO it would result in more complex code.
Dec 9 '09 #5
Thanks Buddy,

I want to mark it solved.

here is solution using FOREACH loop:

Expand|Select|Wrap|Line Numbers
  1. foreach ($temp_property_detail['temp_property_name'] as $i => $name) {
  2.     echo $name.'<br/>';
  3.     echo $temp_property_detail['temp_property_add'][$i].'<br/>';
  4.     echo $temp_property_detail['temp_property_price'][$i].'<br/>';
  5.     echo $temp_property_detail['temp_property_size'][$i].'<br/>';
  6.     echo $temp_property_detail['temp_property_detail'][$i].'<br/>';
  7.     echo '<br/>';
  8. }
  9.  
Dec 10 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by opt_inf_env | last post: by
9 posts views Thread by Charles Banas | last post: by
8 posts views Thread by Ronen Kfir | last post: by
1 post views Thread by Mark Smith | last post: by
2 posts views Thread by chris | last post: by
1 post views Thread by Chuy08 | last post: by
5 posts views Thread by LittleCake | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.