nzol Posted May 5, 2011 Share Posted May 5, 2011 HI all, I need help to get data out of an array using a while loop or something simalar. What I have, is this: <?php $data = array( 1 => array( "name" => "Jordan Smith", "age" => "16 Years 0 Months" ), 2 => array( "name" => "Olivia Smith", "age" => "13 Years 12 Months" ) ); Now, what I did, is I constructed this: while($result=$data[1]) { echo $result['name']; } But it echoed "Jordan Smith" for infinity down the page. If anyone can help be do this, it will be great. Thanks Link to comment https://forums.phpfreaks.com/topic/235583-create-a-while-loop-from-an-array/ Share on other sites More sharing options...
fugix Posted May 5, 2011 Share Posted May 5, 2011 Look into foreach() loops. There purpose is to extract specific values from an array Link to comment https://forums.phpfreaks.com/topic/235583-create-a-while-loop-from-an-array/#findComment-1210824 Share on other sites More sharing options...
Adam Posted May 5, 2011 Share Posted May 5, 2011 There purpose is to extract specific values from an array Kind of, but I'd say they're for looping through the contents of an array - or object - as opposed to extracting values from them. Link to comment https://forums.phpfreaks.com/topic/235583-create-a-while-loop-from-an-array/#findComment-1210831 Share on other sites More sharing options...
fugix Posted May 5, 2011 Share Posted May 5, 2011 Kind of, but I'd say they're for looping through the contents of an array - or object - as opposed to extracting values from them. they are the same thing. Link to comment https://forums.phpfreaks.com/topic/235583-create-a-while-loop-from-an-array/#findComment-1210844 Share on other sites More sharing options...
priti Posted May 5, 2011 Share Posted May 5, 2011 function print_arr($common1) { $callback = function($value,$key) { echo $value.'<br/>'; }; array_walk_recursive($common1,$callback); } $d=print_arr($data); echo $d; Hope you find it useful. Link to comment https://forums.phpfreaks.com/topic/235583-create-a-while-loop-from-an-array/#findComment-1210873 Share on other sites More sharing options...
cyberRobot Posted May 5, 2011 Share Posted May 5, 2011 The following tutorial shows a couple methods of looping through and displaying data from a multidimensional array: http://www.webcheatsheet.com/PHP/multidimensional_arrays.php Link to comment https://forums.phpfreaks.com/topic/235583-create-a-while-loop-from-an-array/#findComment-1210883 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.