473,246 Members | 1,793 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 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 2495
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,658 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

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

Similar topics

3
by: opt_inf_env | last post by:
Hello, What is a common way to print elements of a multidimensional array? I treid to do it in the straightforward way: $input_list = "Something"; print "$input_list"; And it does not work. ...
9
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
8
by: Ronen Kfir | last post by:
I need to take as an input matrix N*M from stdin & print it as a matrix shape, something like: |6 8 2 4 7| |4 0 1 8 2| |3 1 5 2 6| |9 3 8 4 0| I don't have any idea how...
1
by: Mark Smith | last post by:
I'm trying to copy data from a 1D array to a 2D array. The obvious thing doesn't work: int twoDee = new int; int oneDee = new int { 1, 2 }; Array.Copy(oneDee, 2, twoDee, 2, 2); This causes a...
2
by: chris | last post by:
Hi there, I created a Multidimensional array of labels Label lblMultiArray = new Label { {Label3, LblThuTotal}, {Label4,LblFriTotal} }; Now I would like to compare the values in the array,...
10
by: | last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily complicated than the other languages I know. I feel this is because there aren't many good official resources out there to...
1
by: Chuy08 | last post by:
If I have a multidimensional array like the following: Array $records =Array 0 = 30 year, 6.0; 1 = 30 year, 6.0; 2 = Pay Option, 1.0; 3 = Pay Option, 1.0; How could I flatten this to...
5
by: LittleCake | last post by:
Hi All, I have a multidimensional array where each sub-array contains just two entries, which indicates a relationship between those two entries. for example the first sub-array: =Array ( =30...
12
by: filippo nanni | last post by:
Hello everybody, my question is this: I have two multidimensional arrays and I have to create a third one (for later use) from comparing these two. Here is my example code: //BEGIN CODE var...
9
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.