How to print multidimensional array?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rahulephp
Forum Commoner
Posts: 28
Joined: Mon Oct 05, 2009 11:05 am

How to print multidimensional array?

Post by 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:

Code: Select all

 
 
 
Array
(
    [temp_property_name] => Array
        (
            [0] => Property Name: 1
            [1] => Property Name: 2
        )
 
    [temp_property_add] => Array
        (
            [0] => Property Address: 1
            [1] => Property Address: 2
        )
 
    [temp_property_price] => Array
        (
            [0] => Price: 1
            [1] => Price: 2
        )
 
    [temp_property_size] => Array
        (
            [0] => Property Size: 1
            [1] => Property Size: 2
        )
 
    [temp_property_detail] => Array
        (
            [0] => URL 1
            [1] => URL 2
        )
 
)
 
Please let me know, how would be this possible?
pbs
Forum Contributor
Posts: 230
Joined: Fri Nov 07, 2008 5:31 am
Location: Nashik, India
Contact:

Re: How to print multidimensional array?

Post by pbs »

Try this code

Code: Select all

 
<?php
$cnt = count($arr['temp_property_name']);
for($i=0;$i<$cnt;$i++)
{
    echo "<br>Property Name: ".($i+1)." ".$arr['temp_property_name'][$i];
    echo "<br>Property Address: ".($i+1)." ".$arr['temp_property_add'][$i];
    echo "<br>Price: ".($i+1)." ".$arr['temp_property_price'][$i];
    echo "<br>Property Size: ".($i+1)." ".$arr['temp_property_size'][$i];
    echo "<br>URL: ".($i+1)." ".$arr['temp_property_detail'][$i];
}
?>
 
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: How to print multidimensional array?

Post by jayshields »

Your array has been constructed incorrectly, so it's more complicated than it should be.

Code: Select all

 
for($x = 0; $x <= 1 /*amount of properties - 1*/; $x++)
  foreach($array as $v)
    echo $v[$x];
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: How to print multidimensional array?

Post by AbraCadaver »

jayshields wrote:Your array has been constructed incorrectly, so it's more complicated than it should be.

Code: Select all

 
for($x = 0; $x <= 1 /*amount of properties - 1*/; $x++)
  foreach($array as $v)
    echo $v[$x];
Yes, as I posted in the OP's other post: viewtopic.php?f=1&t=109960&p=581824#p581824 They should rethink their array.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply