There might be other ways to use "in_array" with part of a multidimentional arrays.But I tried it with "array_pop":
-
<?php
-
$tickets=array(array('fromcity'=>Sydney, 'tocity'=>London, 'quantity'=>3));
-
-
$tickets_temp = array();
-
$tickets_temp = array_pop($tickets);
-
-
//New Parameters to Array
-
$fromcity = 'Sydneyxxxx';
-
$tocity = 'Londonxxxx';
-
-
print route_chk($fromcity,$tickets_temp);
-
print route_chk($tocity,$tickets_temp);
-
-
function route_chk($var,$arr){
-
if(in_array($var,$arr)){
-
$out = 'Found!';
-
}else{
-
$out = 'push the new values here!';
-
}
-
return $out.'<br>';
-
}
-
?>
-
-
Thank you, now I know how to check if the route exists. but the problem is how to update the Qty? by the way, I didn't use any database, I just use session to add to a chopping cart. My code:
[PHP]
<?php
session_start();
$fc=$_POST['from_city'];
$num_rows=$_POST['num_rows'];
//loop to get all the variables posted
for($i=1; $i<=$num_rows; $i++){
if(isset($_POST['ticket_'.$i])&&!empty($_POST['ticket_'.$i])){
$qty=$_POST['ticket_'.$i];
$tc=$_POST['tc_'.$i];
$price=$_POST['price_'.$i];
$check=array('fc'=>$fc,'tc'=>$tc);
if($qty<=6 && $qty>=1){
if (!isset($_SESSION["tickets"]))
{ session_register('tickets');
$tickets=array(array('fc'=>$fc,'tc'=>$tc,'price'=> $price,'qty'=>$qty));
$_SESSION['tickets']=$tickets;
}
else
{
$tickets=$_SESSION['tickets'];
//check if there is a same route
foreach($tickets as $route){
if($route['tc']==$tc && $route['fc']==$fc){
$route['qty']+=$qty;
break;
}else{ array_push($_SESSION['tickets'],array('fc'=>$fc,'tc'=>$tc,'price'=>$price,'qty'=> $qty));
$tickets=$_SESSION['tickets'];}}
//check finish
}
}
}
}
[/PHP]
I don't know why the value not change.