Roy Barten Posted February 18, 2009 Share Posted February 18, 2009 Hello i created the following array with the following code: function ReservationResultsInArray() { include ("array.php"); $countreservations = 0; foreach($array as $val) { $countreservations++; $startdate = strtotime($val['start']); $enddate = strtotime($val['end']); $hours = (($enddate - $startdate) / 60) / 60; $result[] = array( "id" => $val['id'], "hours" => $hours, "start" => $val['start'] ); } echo "<pre>"; print_r ($result); echo "</pre>"; } // einde functie The result of the print_r is as following: Array ( [0] => Array ( [id] => 1 [hours] => 2 [start] => 2009-01-31 10:00 ) [1] => Array ( [id] => 2 [hours] => 50 [start] => 2009-03-01 10:00 ) [2] => Array ( [id] => 3 [hours] => 50 [start] => 2009-03-01 12:00 ) [3] => Array ( [id] => 4 [hours] => 50 [start] => 2009-03-01 12:00 ) [4] => Array ( [id] => 5 [hours] => 50 [start] => 2009-03-01 12:00 ) ) Now i want to compare the hours from key 0, from wich the value = 2 with a variable into another code. how do i do this? I thought something like this but it does not work: if ($result['0']['hours']== $hours){ But nothing happend. What do i wrong? Quote Link to comment https://forums.phpfreaks.com/topic/145804-solved-reading-a-array/ Share on other sites More sharing options...
premiso Posted February 18, 2009 Share Posted February 18, 2009 $result[0]['hours']== $hours The index is just the number adding the ' to it treated it like a string, which it is not. Try that. Quote Link to comment https://forums.phpfreaks.com/topic/145804-solved-reading-a-array/#findComment-765519 Share on other sites More sharing options...
Roy Barten Posted February 18, 2009 Author Share Posted February 18, 2009 I tried that with a simple echo but still nothing happend. I ttied it with the following code: $hours = 2; if ($result[0]['hours']== $hours) { echo "test geslaagd!"; } I don't understand, what do i wrong? Quote Link to comment https://forums.phpfreaks.com/topic/145804-solved-reading-a-array/#findComment-765525 Share on other sites More sharing options...
premiso Posted February 18, 2009 Share Posted February 18, 2009 $hours = 2; echo "Hours: " . $hours . " <br />Array Hours: " . $result[0]['hours'] . "<br />Returns:<br />"; if ($result[0]['hours']== $hours) { echo "Yep the hours were equal."; }else { echo "Nope the hours were not equal."; } See what that returns. Quote Link to comment https://forums.phpfreaks.com/topic/145804-solved-reading-a-array/#findComment-765531 Share on other sites More sharing options...
Roy Barten Posted February 18, 2009 Author Share Posted February 18, 2009 It returns the following: Hours: 2 Array Hours: Returns: Nope the hours were not equal. Sorry, maybe you know it, but i am out of iedeas about this ??? ??? Quote Link to comment https://forums.phpfreaks.com/topic/145804-solved-reading-a-array/#findComment-765538 Share on other sites More sharing options...
premiso Posted February 18, 2009 Share Posted February 18, 2009 Is this code on the same page that you populate the $results array? And after it is populated? Quote Link to comment https://forums.phpfreaks.com/topic/145804-solved-reading-a-array/#findComment-765548 Share on other sites More sharing options...
Roy Barten Posted February 18, 2009 Author Share Posted February 18, 2009 Yes, at least i think so caus i don't completely understand you but here is all of the code: <table width="900" border="1" cellpadding="0" cellspacing="0"> <tr height="20" > <td colspan="2"><div align="center">Maandag</div></td> </tr> <tr> <? function ReservationResultsInArray() { include ("array.php"); $countreservations = 0; foreach($array as $val) { $countreservations++; $startdate = strtotime($val['start']); $enddate = strtotime($val['end']); $hours = (($enddate - $startdate) / 60) / 60; $result[] = array( "id" => $val['id'], "hours" => $hours, "start" => $val['start'] ); } echo "<pre>"; print_r ($result[0]['hours']); echo "</pre>"; } // einde functie ReservationResultsInArray(); $hours = 2; echo "Hours: " . $hours . " <br />Array Hours: "; echo $result[0]['hours'] . "<br />Returns:<br />"; if ($result[0]['hours']== $hours) { echo "Yep the hours were equal."; }else { echo "Nope the hours were not equal."; } ?> </table> You see also some HTMl but that doesn't ,mather until so far. you also see include array.php. This array is as following: $array = array(array ( "id" => "1", "name" => "laptop 1", "username" => "user 1", "start" => "2009-01-31 10:00", "end" => "2009-01-31 12:00" ), array ( "id" => "2", "name" => "laptop 2", "username" => "user 1", "start" => "2009-03-01 10:00", "end" => "2009-03-03 12:00" ), array ( "id" => "3", "name" => "laptop 3", "username" => "user 2", "start" => "2009-03-01 12:00", "end" => "2009-03-03 14:00" ), array ( "id" => "4", "name" => "laptop 2", "username" => "user 2", "start" => "2009-03-01 12:00", "end" => "2009-03-03 14:00" ), array ( "id" => "5", "name" => "laptop 3", "username" => "user 2", "start" => "2009-03-01 12:00", "end" => "2009-03-03 14:00" )); //echo "<pre>"; //print_r($array); //echo "</pre>"; /* unset($array); array 1 item array ( "id" => "6", "name" => "laptop 1", "username" => "user 1", "start" => "2009-01-31 10:00", "end" => "2009-01-31 12:00" ), array ( "id" => "7", "name" => "laptop 1", "username" => "user 2", "start" => "2009-02-01 10:00", "end" => "2009-02-03 12:00" )); Quote Link to comment https://forums.phpfreaks.com/topic/145804-solved-reading-a-array/#findComment-765556 Share on other sites More sharing options...
premiso Posted February 18, 2009 Share Posted February 18, 2009 Your function does not return the array, thus it is only valid in the function scope. Try this: <?php function ReservationResultsInArray() { include ("array.php"); $countreservations = 0; foreach($array as $val) { $countreservations++; $startdate = strtotime($val['start']); $enddate = strtotime($val['end']); $hours = (($enddate - $startdate) / 60) / 60; $result[] = array( "id" => $val['id'], "hours" => $hours, "start" => $val['start'] ); } return $result; } // einde functie $result = ReservationResultsInArray(); I bet that will make it work. Quote Link to comment https://forums.phpfreaks.com/topic/145804-solved-reading-a-array/#findComment-765566 Share on other sites More sharing options...
Roy Barten Posted February 18, 2009 Author Share Posted February 18, 2009 it returns the following: Hours: 2 Array Hours: 2 Returns: Yep the hours were equal. So thank you!!! it works!! GREAT!! ;D ;D Quote Link to comment https://forums.phpfreaks.com/topic/145804-solved-reading-a-array/#findComment-765570 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.