Jump to content

Recommended Posts

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:



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?

Link to comment
https://forums.phpfreaks.com/topic/184515-how-to-print-multidimensional-array/
Share on other sites

Hi

 

solution1:

I have did this in following way

 

$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 output is:

 

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 please let me know, how can we do same thing with using "FOREACH" loop?

 

But please let me know, how can we do same thing with using "FOREACH" loop?

 

I'm not entirely sure why you can't arrive at this on your own (is there really such a huge leap between for and foreach?). Here's one way of doing it:

 

foreach ($temp_property_detail['temp_property_name'] as $i => $name) {
    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/>';
}

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/>';

}

 

:)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.