Jump to content

extract data from multi dimensional array


corbeeresearch

Recommended Posts

Hi,

 

I have session data like this:

 

$_SESSION[‘data’][‘basic_premium’]
$_SESSION[‘data’][‘gross_premium’]
$_SESSION[‘data’][‘net_premium’]  

 

each second parameter is made of arrays

 

I tried to extract it using:

 

foreach ($_SESSION['data'] as $row)
{
echo $row;
}

 

but I get

Array0.125ArrayArrayArrayArrayArrayArrayArrayArrayArray1927.51850.423.13ArrayArrayArray50664.525 

. Where did I went wrong? Thanks

Put this in your script after the array has been built, and paste in the output. Also, the quotes in your first code block are non-standard 'curly-quotes' or 'smart' quotes or something along those lines, and will cause you headaches eventually.

 

echo '<pre>';
print_r($_SESSION['data']);
echo '</pre>';

I see, here is the output:

 

[basic_premium] => Array
        (
            [0] => 14000
            [1] => 14490
        )

    [gross_premium] => Array
        (
            [0] => 15430
            [1] => 15420
        )

    [net_premium] => Array
        (
            [0] => 25233.495
            [1] => 25431.03
        )

 

foreach ($_SESSION['data']['basic_premium'] as $row)

{

echo $row;

}

 

Will echo all of the values in basic_premium elements, but I suspect that isn't what you really want to do . . .

A nested foreach loop?

 


foreach($_SESSION['data'] as $key => $arr)
{
     if(is_array($arr))
     {
          foreach($arr as $amount)
          {
               echo $key . ': ' . $amount . '<br />';
          }
     }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.