473,407 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

How to update a multidimensional array?

24
In PHP I have a multidimensional array to record the ticket routes info like following:
Expand|Select|Wrap|Line Numbers
  1. $tickets=array(array('fromcity'=>Sydney, 'tocity'=>London, 'quantity'=>3))
every time I want to add a new route, it will check if the route is existing,if so, it will just update the quantity, if not I just use array_push to add the new route in the array.
I am not sure how to process the update.
Thank you for help first!
Sep 15 '07 #1
6 3721
ak1dnar
1,584 Expert 1GB
Hi
Check this out: in_array()

example


Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $tickets=array(array('fromcity'=>Sydney, 'tocity'=>London, 'quantity'=>3));
  3.  
  4. //New Parameters to Array
  5. $fromcity = 'NewYork'; 
  6. $tocity = 'London';
  7. $quantity = 3;
  8.  
  9. if (in_array(array('fromcity' => $fromcity, 'tocity' => $tocity,'quantity'=>$quantity), $tickets)) { 
  10.  print "Found !"; 
  11. }else{
  12. print "Push it here !";
  13. }
  14. ?>
  15.  
Sep 15 '07 #2
pbmods
5,821 Expert 4TB
Heya, Lucoin.

Please use CODE tags when posting source code:

[CODE=php]
PHP code goes here.
[/CODE]
Sep 15 '07 #3
lucoin
24
Hi
Check this out: in_array()

example


Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $tickets=array(array('fromcity'=>Sydney, 'tocity'=>London, 'quantity'=>3));
  3.  
  4. //New Parameters to Array
  5. $fromcity = 'NewYork'; 
  6. $tocity = 'London';
  7. $quantity = 3;
  8.  
  9. if (in_array(array('fromcity' => $fromcity, 'tocity' => $tocity,'quantity'=>$quantity), $tickets)) { 
  10.  print "Found !"; 
  11. }else{
  12. print "Push it here !";
  13. }
  14. ?>
  15.  
yeah, I tried this method, but for in_array function, it matches all the fields. but for the same route, just 'fromcity' and 'tocity' are the same that is enough. how to only check two fields, if match and then update the quantity.
Sep 15 '07 #4
pbmods
5,821 Expert 4TB
Heya, Lucoin.

Are these results you are fetching from a database? Are you using a DAL? Consider making the array keys unique:

Expand|Select|Wrap|Line Numbers
  1. $tickets = array();
  2. while( $_row = mysql_fetch_assoc($_result) )
  3. {
  4.     $tickets[$_row['fromCity'] . $_row['toCity']] = $_row;
  5. }
  6.  
Then, all you have to do is check to see if $tickets[$fromCity . $toCity] exists.
Sep 15 '07 #5
ak1dnar
1,584 Expert 1GB
yeah, I tried this method, but for in_array function, it matches all the fields. but for the same route, just 'fromcity' and 'tocity' are the same that is enough. how to only check two fields, if match and then update the quantity.
There might be other ways to use "in_array" with part of a multidimentional arrays.But I tried it with "array_pop":

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $tickets=array(array('fromcity'=>Sydney, 'tocity'=>London, 'quantity'=>3));
  3.  
  4. $tickets_temp = array();
  5. $tickets_temp = array_pop($tickets);
  6.  
  7. //New Parameters to Array
  8. $fromcity = 'Sydneyxxxx'; 
  9. $tocity = 'Londonxxxx';
  10.  
  11. print route_chk($fromcity,$tickets_temp);
  12. print route_chk($tocity,$tickets_temp);
  13.  
  14. function route_chk($var,$arr){
  15. if(in_array($var,$arr)){
  16. $out = 'Found!';
  17. }else{
  18. $out = 'push the new values here!';
  19. }
  20. return $out.'<br>';
  21. }
  22. ?>
  23.  
  24.  
Sep 15 '07 #6
lucoin
24
There might be other ways to use "in_array" with part of a multidimentional arrays.But I tried it with "array_pop":

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $tickets=array(array('fromcity'=>Sydney, 'tocity'=>London, 'quantity'=>3));
  3.  
  4. $tickets_temp = array();
  5. $tickets_temp = array_pop($tickets);
  6.  
  7. //New Parameters to Array
  8. $fromcity = 'Sydneyxxxx'; 
  9. $tocity = 'Londonxxxx';
  10.  
  11. print route_chk($fromcity,$tickets_temp);
  12. print route_chk($tocity,$tickets_temp);
  13.  
  14. function route_chk($var,$arr){
  15. if(in_array($var,$arr)){
  16. $out = 'Found!';
  17. }else{
  18. $out = 'push the new values here!';
  19. }
  20. return $out.'<br>';
  21. }
  22. ?>
  23.  
  24.  
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.
Sep 16 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
1
by: Mark Smith | last post by:
I'm trying to copy data from a 1D array to a 2D array. The obvious thing doesn't work: int twoDee = new int; int oneDee = new int { 1, 2 }; Array.Copy(oneDee, 2, twoDee, 2, 2); This causes a...
2
by: chris | last post by:
Hi there, I created a Multidimensional array of labels Label lblMultiArray = new Label { {Label3, LblThuTotal}, {Label4,LblFriTotal} }; Now I would like to compare the values in the array,...
10
by: | last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily complicated than the other languages I know. I feel this is because there aren't many good official resources out there to...
1
by: Chuy08 | last post by:
If I have a multidimensional array like the following: Array $records =Array 0 = 30 year, 6.0; 1 = 30 year, 6.0; 2 = Pay Option, 1.0; 3 = Pay Option, 1.0; How could I flatten this to...
5
by: LittleCake | last post by:
Hi All, I have a multidimensional array where each sub-array contains just two entries, which indicates a relationship between those two entries. for example the first sub-array: =Array ( =30...
4
Jezternz
by: Jezternz | last post by:
First of all I am open to any suggestions and advice. If a javscript multidimensional array is a bad way to do this please say so. I considered XML but I wondered if this would be a bad idea as it...
12
by: filippo nanni | last post by:
Hello everybody, my question is this: I have two multidimensional arrays and I have to create a third one (for later use) from comparing these two. Here is my example code: //BEGIN CODE var...
9
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.