473,382 Members | 1,647 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,382 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 2502
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.