How to update a multidimensional array?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lucoin
    New Member
    • Sep 2007
    • 24

    How to update a multidimensional array?

    In PHP I have a multidimensiona l array to record the ticket routes info like following:
    [code=php]$tickets=array( array('fromcity '=>Sydney, 'tocity'=>Londo n, 'quantity'=>3))[/code]
    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!
  • ak1dnar
    Recognized Expert Top Contributor
    • Jan 2007
    • 1584

    #2
    Hi
    Check this out: in_array()

    example


    [code=php]
    <?php
    $tickets=array( array('fromcity '=>Sydney, 'tocity'=>Londo n, 'quantity'=>3)) ;

    //New Parameters to Array
    $fromcity = 'NewYork';
    $tocity = 'London';
    $quantity = 3;

    if (in_array(array ('fromcity' => $fromcity, 'tocity' => $tocity,'quanti ty'=>$quantity) , $tickets)) {
    print "Found !";
    }else{
    print "Push it here !";
    }
    ?>
    [/code]

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Heya, Lucoin.

      Please use CODE tags when posting source code:

      &#91;CODE=ph p]
      PHP code goes here.
      &#91;/CODE]

      Comment

      • lucoin
        New Member
        • Sep 2007
        • 24

        #4
        Originally posted by ajaxrand
        Hi
        Check this out: in_array()

        example


        [code=php]
        <?php
        $tickets=array( array('fromcity '=>Sydney, 'tocity'=>Londo n, 'quantity'=>3)) ;

        //New Parameters to Array
        $fromcity = 'NewYork';
        $tocity = 'London';
        $quantity = 3;

        if (in_array(array ('fromcity' => $fromcity, 'tocity' => $tocity,'quanti ty'=>$quantity) , $tickets)) {
        print "Found !";
        }else{
        print "Push it here !";
        }
        ?>
        [/code]
        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.

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Heya, Lucoin.

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

          [code=php]
          $tickets = array();
          while( $_row = mysql_fetch_ass oc($_result) )
          {
          $tickets[$_row['fromCity'] . $_row['toCity']] = $_row;
          }
          [/code]

          Then, all you have to do is check to see if $tickets[$fromCity . $toCity] exists.

          Comment

          • ak1dnar
            Recognized Expert Top Contributor
            • Jan 2007
            • 1584

            #6
            Originally posted by lucoin
            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 multidimentiona l arrays.But I tried it with "array_pop" :

            [CODE=php]
            <?php
            $tickets=array( array('fromcity '=>Sydney, 'tocity'=>Londo n, 'quantity'=>3)) ;

            $tickets_temp = array();
            $tickets_temp = array_pop($tick ets);

            //New Parameters to Array
            $fromcity = 'Sydneyxxxx';
            $tocity = 'Londonxxxx';

            print route_chk($from city,$tickets_t emp);
            print route_chk($toci ty,$tickets_tem p);

            function route_chk($var, $arr){
            if(in_array($va r,$arr)){
            $out = 'Found!';
            }else{
            $out = 'push the new values here!';
            }
            return $out.'<br>';
            }
            ?>

            [/CODE]

            Comment

            • lucoin
              New Member
              • Sep 2007
              • 24

              #7
              Originally posted by ajaxrand
              There might be other ways to use "in_array" with part of a multidimentiona l arrays.But I tried it with "array_pop" :

              [CODE=php]
              <?php
              $tickets=array( array('fromcity '=>Sydney, 'tocity'=>Londo n, 'quantity'=>3)) ;

              $tickets_temp = array();
              $tickets_temp = array_pop($tick ets);

              //New Parameters to Array
              $fromcity = 'Sydneyxxxx';
              $tocity = 'Londonxxxx';

              print route_chk($from city,$tickets_t emp);
              print route_chk($toci ty,$tickets_tem p);

              function route_chk($var, $arr){
              if(in_array($va r,$arr)){
              $out = 'Found!';
              }else{
              $out = 'push the new values here!';
              }
              return $out.'<br>';
              }
              ?>

              [/CODE]
              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=$_POS T['num_rows'];
              //loop to get all the variables posted
              for($i=1; $i<=$num_rows; $i++){
              if(isset($_POST['ticket_'.$i])&&!empty($_POS T['ticket_'.$i])){
              $qty=$_POST['ticket_'.$i];
              $tc=$_POST['tc_'.$i];
              $price=$_POST['price_'.$i];
              $check=array('f c'=>$fc,'tc'=>$ tc);
              if($qty<=6 && $qty>=1){
              if (!isset($_SESSI ON["tickets"]))
              { session_registe r('tickets');
              $tickets=array( array('fc'=>$fc ,'tc'=>$tc,'pri ce'=>$price,'qt y'=>$qty));
              $_SESSION['tickets']=$tickets;
              }
              else
              {
              $tickets=$_SESS ION['tickets'];
              //check if there is a same route
              foreach($ticket s as $route){
              if($route['tc']==$tc && $route['fc']==$fc){
              $route['qty']+=$qty;
              break;
              }else{ array_push($_SE SSION['tickets'],array('fc'=>$f c,'tc'=>$tc,'pr ice'=>$price,'q ty'=>$qty));
              $tickets=$_SESS ION['tickets'];}}
              //check finish
              }
              }
              }
              }
              [/PHP]
              I don't know why the value not change.

              Comment

              Working...