corbeeresearch Posted September 21, 2010 Share Posted September 21, 2010 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 Link to comment https://forums.phpfreaks.com/topic/213966-extract-data-from-multi-dimensional-array/ Share on other sites More sharing options...
Pikachu2000 Posted September 21, 2010 Share Posted September 21, 2010 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>'; Link to comment https://forums.phpfreaks.com/topic/213966-extract-data-from-multi-dimensional-array/#findComment-1113551 Share on other sites More sharing options...
corbeeresearch Posted September 21, 2010 Author Share Posted September 21, 2010 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 ) Link to comment https://forums.phpfreaks.com/topic/213966-extract-data-from-multi-dimensional-array/#findComment-1113552 Share on other sites More sharing options...
corbeeresearch Posted September 21, 2010 Author Share Posted September 21, 2010 I actually needed to extract each individuals, unfortunately when I do foreach ($_SESSION['data'] as $row) { echo $row['basic_premium']; } I get error it says Message: Undefined index: basic_premium Link to comment https://forums.phpfreaks.com/topic/213966-extract-data-from-multi-dimensional-array/#findComment-1113553 Share on other sites More sharing options...
Pikachu2000 Posted September 21, 2010 Share Posted September 21, 2010 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 . . . Link to comment https://forums.phpfreaks.com/topic/213966-extract-data-from-multi-dimensional-array/#findComment-1113555 Share on other sites More sharing options...
corbeeresearch Posted September 21, 2010 Author Share Posted September 21, 2010 yeah, that's not what I wanted to do. I needed it to loop the three data all at once. Are there any way to achieve this? Link to comment https://forums.phpfreaks.com/topic/213966-extract-data-from-multi-dimensional-array/#findComment-1113557 Share on other sites More sharing options...
the182guy Posted September 21, 2010 Share Posted September 21, 2010 A nested foreach loop? foreach($_SESSION['data'] as $key => $arr) { if(is_array($arr)) { foreach($arr as $amount) { echo $key . ': ' . $amount . '<br />'; } } } Link to comment https://forums.phpfreaks.com/topic/213966-extract-data-from-multi-dimensional-array/#findComment-1113598 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.