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!!! Quote Link to comment 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>"; ?> Quote Link to comment 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? Quote Link to comment 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>"; ?> Quote Link to comment 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! Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.