Roy Barten Posted February 13, 2009 Share Posted February 13, 2009 hello, I create the following code: $array = array ( "name" => "laptop 1", "username" => "user 1", "start" => "2009-01-31 10:00", "end" => "2009-01-31 12:00" ); $array = array ( "name" => "laptop 1", "username" => "user 2", "start" => "2009-02-01 10:00", "end" => "2009-02-03 12:00" ); $array = array ( "name" => "laptop 2", "username" => "user 1", "start" => "2009-03-01 10:00", "end" => "2009-03-03 12:00" ); $array = array ( "name" => "laptop 2", "username" => "user 2", "start" => "2009-03-01 12:00", "end" => "2009-03-03 14:00" ); $array = array ( "name" => "laptop 3", "username" => "user 2", "start" => "2009-03-01 12:00", "end" => "2009-03-03 14:00" ); echo "<pre>"; print_r($array['name']); echo "</pre>"; unset($array); /* array 1 item */ $array = array ( 'name' => "laptop 1", 'username' => "user 1", 'start' => "2009-01-31 10:00", 'end' => "2009-01-31 12:00" ); $array = array ( "name" => "laptop 1", "username" => "user 2", "start" => "2009-02-01 10:00", "end" => "2009-02-03 12:00" ); How can I loop trough this array, on that way that i can chose a key and I get all of the values out. For example, I want all the values from $array['start']. I want the results as following: 2009-01-31 10:00 2009-02-01 10:00 2009-03-01 10:00 How do i do this? PLEASE HELP!!! Link to comment https://forums.phpfreaks.com/topic/145045-solved-loop-through-an-array/ Share on other sites More sharing options...
gunabalans Posted February 13, 2009 Share Posted February 13, 2009 Hai run this to get teh result <?php $array = array(array ( "name" => "laptop 1", "username" => "user 1", "start" => "2009-01-31 10:00", "end" => "2009-01-31 12:00" ), array ( "name" => "laptop 2", "username" => "user 1", "start" => "2009-03-01 10:00", "end" => "2009-03-03 12:00" ), array ( "name" => "laptop 3", "username" => "user 2", "start" => "2009-03-01 12:00", "end" => "2009-03-03 14:00" )); echo "<pre>"; foreach($array as $val) { print_r($val['start'].'<br>'); } echo "</pre>"; ?> Link to comment https://forums.phpfreaks.com/topic/145045-solved-loop-through-an-array/#findComment-761131 Share on other sites More sharing options...
Roy Barten Posted February 13, 2009 Author Share Posted February 13, 2009 Oke, thanks for you reply but I tried to loop through it with the foreach function, but it does not work. This is the output: Value: l Value: u Value: 2 Value: 2 I am just started with creating arrays etc so maybe I do some stupid things in my code. But here is the code i use to loop trough it: foreach ($array as $value) { echo "Value: " . $value['start'] . "<br />"; } Please can you help me? Link to comment https://forums.phpfreaks.com/topic/145045-solved-loop-through-an-array/#findComment-761133 Share on other sites More sharing options...
gunabalans Posted February 13, 2009 Share Posted February 13, 2009 <?php $array = array(array ( "name" => "laptop 1", "username" => "user 1", "start" => "2009-01-31 10:00", "end" => "2009-01-31 12:00" ), array ( "name" => "laptop 2", "username" => "user 1", "start" => "2009-03-01 10:00", "end" => "2009-03-03 12:00" ), array ( "name" => "laptop 3", "username" => "user 2", "start" => "2009-03-01 12:00", "end" => "2009-03-03 14:00" )); echo "<pre>"; foreach($array as $val) { print_r($val['start'].'<br>'); } echo "</pre>"; ?> Link to comment https://forums.phpfreaks.com/topic/145045-solved-loop-through-an-array/#findComment-761135 Share on other sites More sharing options...
Roy Barten Posted February 13, 2009 Author Share Posted February 13, 2009 Thanks!! It works! NICE!! I look to you're code and i think it will help me out also the next time!! Great!!! Thank you! Link to comment https://forums.phpfreaks.com/topic/145045-solved-loop-through-an-array/#findComment-761136 Share on other sites More sharing options...
Prismatic Posted February 13, 2009 Share Posted February 13, 2009 <?php $data = array(array("name" => "laptop 1", "username" => "user 1", "start" => "2009-01-31 10:00", "end" => "2009-01-31 12:00"), array("name" => "laptop 1", "username" => "user 1", "start" => "2009-02-01 10:00", "end" => "2009-02-03 12:00"), array("name" => "laptop 1", "username" => "user 1", "start" => "2009-03-01 10:00", "end" => "2009-03-03 12:00"), array("name" => "laptop 1", "username" => "user 1", "start" => "2009-04-01 10:00", "end" => "2009-04-03 12:00"), array("name" => "laptop 1", "username" => "user 1", "start" => "2009-05-01 10:00", "end" => "2009-05-03 12:00")); foreach($data as $key => $val) print $val["start"] ."<br />"; ?> Returns: 2009-01-31 10:00 2009-02-01 10:00 2009-03-01 10:00 2009-04-01 10:00 2009-05-01 10:00 edit - im slow Link to comment https://forums.phpfreaks.com/topic/145045-solved-loop-through-an-array/#findComment-761138 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.