Jump to content

[SOLVED] reading a array


Roy Barten

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/145804-solved-reading-a-array/
Share on other sites

$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.

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"
								));

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.