teknojunkey Posted December 10, 2008 Share Posted December 10, 2008 Hi , im trying to get the data out of an array. I can see the array using this: print_r($_SESSION); this is printed: Array ( [submitted] => Array ( [1] => Array ( [code] => Array ( [4mmPilkingtonMinisterToughened] => TRUE ) ) [3] => Array ( [code] => Array ( [RecetrackCutimgsrcimagesracetrack.jpg] => TRUE ) ) [4] => Array ( [other] => 10 ) [5] => Array ( [other] => 200mm ) [6] => Array ( [other] => 200mm ) ) ) I want to get the following out of the array and displayed without the "other bits": "4mmPilkingtonMinisterToughened" "RecetrackCutimgsrcimagesracetrack.jpg" "10" "200mm" "200mm" Im a bit of a newby so please be gentle. thanks Link to comment https://forums.phpfreaks.com/topic/136403-solved-easy-get-data-out-of-an-array/ Share on other sites More sharing options...
msinternet Posted December 10, 2008 Share Posted December 10, 2008 You need teh foreach loop. foreach($_SESSION['submitted'] as $key => $value) { $data[]['key'] = $key; $data[]['value'] = $value; } echo $data[0]['key'] . "\n"; echo $data[1]['key'] . "\n"; echo $data[2]['value'] . "\n"; echo $data[3]['value'] . "\n"; echo $data[4]['value'] . "\n"; we are putting the array keys and values in an array and selectively fishing them out. Martin Link to comment https://forums.phpfreaks.com/topic/136403-solved-easy-get-data-out-of-an-array/#findComment-711844 Share on other sites More sharing options...
DarkWater Posted December 10, 2008 Share Posted December 10, 2008 $_SESSION['submitted'][1]['code'] should contain the value you're looking for, based on the print_r() you posted. Link to comment https://forums.phpfreaks.com/topic/136403-solved-easy-get-data-out-of-an-array/#findComment-711881 Share on other sites More sharing options...
teknojunkey Posted December 10, 2008 Author Share Posted December 10, 2008 Thanks I got the first item in teh array out , but nothing els. I used this : foreach($_SESSION['submitted'][1]['code'] as $nkey => $nvalue) { $data[]['nkey'] = $nkey; $data[]['nvalue'] = $nvalue; } print_r ($data[0]['nkey'] . "\n"); print_r ($data[1]['nvalue'] . "\n"); print_r ($data[2]['nkey'] . "\n"); print_r ($data[3]['nvalue'] . "\n"); can i replace the [1] with a wildcard ? if there is one thanks Link to comment https://forums.phpfreaks.com/topic/136403-solved-easy-get-data-out-of-an-array/#findComment-712062 Share on other sites More sharing options...
teknojunkey Posted December 11, 2008 Author Share Posted December 11, 2008 I guess i should give a url: http://dev.crocodile-communication.com/demoshop/ask/survey.php?sid=1568D5 the problem is on the following pages: print_r($_SESSION); seems to work fine , but with the syntax stuff. Link to comment https://forums.phpfreaks.com/topic/136403-solved-easy-get-data-out-of-an-array/#findComment-712105 Share on other sites More sharing options...
teknojunkey Posted December 11, 2008 Author Share Posted December 11, 2008 i have figured out what i want to do , I want this to display the new data in the array as it is added. foreach($_SESSION['submitted'][1]['code'] as $nkey => $nvalue) { $data[]['nkey'] = $nkey; $data[]['nvalue'] = $nvalue; } print_r ($data[0]['nkey'] . "\n"); print_r ($data[1]['nvalue'] . "\n"); print_r ($data[2]['nkey'] . "\n"); print_r ($data[3]['nvalue'] . "\n"); currently it only displays the first item in the array, is it possible to update the output text at the array is updated? thanks Link to comment https://forums.phpfreaks.com/topic/136403-solved-easy-get-data-out-of-an-array/#findComment-712726 Share on other sites More sharing options...
teknojunkey Posted December 15, 2008 Author Share Posted December 15, 2008 Hi is it possible to update the output text as the array is updated? foreach($_SESSION['submitted'][1]['code'] as $nkey => $nvalue) { $data[]['nkey'] = $nkey; $data[]['nvalue'] = $nvalue; } print_r ($data[0]['nkey'] . "\n"); print_r ($data[1]['nvalue'] . "\n"); print_r ($data[2]['nkey'] . "\n"); print_r ($data[3]['nvalue'] . "\n"); the number 1 should change to show the other keys in the array Link to comment https://forums.phpfreaks.com/topic/136403-solved-easy-get-data-out-of-an-array/#findComment-715969 Share on other sites More sharing options...
premiso Posted December 15, 2008 Share Posted December 15, 2008 foreach($_SESSION['submitted'][1]['code'] as $nkey => $nvalue) { $data[]['nkey'] = $nkey; $data[]['nvalue'] = $nvalue; } $cnt = count($data); for ($i=0; $i<$cnt; $i++) { print_r ($data[$i]['nkey'] . "\n"); print_r ($data[$i]['nvalue'] . "\n"); } Link to comment https://forums.phpfreaks.com/topic/136403-solved-easy-get-data-out-of-an-array/#findComment-716015 Share on other sites More sharing options...
teknojunkey Posted December 16, 2008 Author Share Posted December 16, 2008 I have to modify and add this for each item in the array : foreach($_SESSION['submitted'][1]['code'] as $nkey1 => $nvalue1) { $data1[]['nkey1'] = $nkey1; $data1[]['nvalue1'] = $nvalue1; } $cont1 = count($data1); for ($i=0; $i<$cont1; $i++) { print_r ($data1[$i]['nkey1'] . "\n"); print_r ($data1[$i]['nvalue1'] . "\n"); } echo "<br>"; //------------------------------------------------------------ foreach($_SESSION['submitted'][3]['code'] as $nkey3 => $nvalue3) { $data3[]['nkey3'] = $nkey3; $data3[]['nvalue3'] = $nvalue3; } $cont3 = count($data3); for ($i=0; $i<$cont3; $i++) { print_r ($data3[$i]['nkey3'] . "\n"); print_r ($data3[$i]['nvalue3'] . "\n"); } No great, but it now works thanks for your help:) Link to comment https://forums.phpfreaks.com/topic/136403-solved-easy-get-data-out-of-an-array/#findComment-716591 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.