How to print multidimensional array?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rahulephp
    New Member
    • Sep 2009
    • 59

    How to print multidimensional array?

    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:
    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?
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    You could pick one of the elements, like temp_property_n ame, 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:
    [code=php]<?php
    $child_count = count($array['temp_property_ name']);
    for($i = 0; $i < $child_count; ++$i)
    {
    echo $array['temp_property_ name'][$i];
    echo $array['temp_property_ add'][$i];
    // etc...
    }
    ?>[/code]

    Comment

    • rahulephp
      New Member
      • Sep 2009
      • 59

      #3
      Hey thanks buddy for your quick reply
      i'll check this and get back to you soon.
      hope it'll be helpful for me

      Comment

      • rahulephp
        New Member
        • Sep 2009
        • 59

        #4
        Hi Again;

        You were write:
        The actual solution for this would be

        Code:
        $child_count = count($temp_property_detail['temp_property_name']);
        	
        	//db($child_count);
        	//exit;
        	
         	for($i = 0; $i <= child_count+1; $i++)
         	{
                 echo $temp_property_detail['temp_property_name'][$i].'<br/>';
          	 echo $temp_property_detail['temp_property_add'][$i].'<br/>';
        	 echo $temp_property_detail['temp_property_price'][$i].'<br/>';
        	 echo $temp_property_detail['temp_property_size'][$i].'<br/>';
        	 echo $temp_property_detail['temp_property_detail'][$i].'<br/>';
        	 echo '<br/>';
         	}
        And the output would be:

        Code:
        Debug:
        
        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
        But can we do this using FOREACH Loop???

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          you could certainly do this, but IMO it would result in more complex code.

          Comment

          • rahulephp
            New Member
            • Sep 2009
            • 59

            #6
            Thanks Buddy,

            I want to mark it solved.

            here is solution using FOREACH loop:

            Code:
            foreach ($temp_property_detail['temp_property_name'] as $i => $name) {
                echo $name.'<br/>';
                echo $temp_property_detail['temp_property_add'][$i].'<br/>';
                echo $temp_property_detail['temp_property_price'][$i].'<br/>';
                echo $temp_property_detail['temp_property_size'][$i].'<br/>';
                echo $temp_property_detail['temp_property_detail'][$i].'<br/>';
                echo '<br/>';
            }

            Comment

            Working...