Swapping array elements from two different arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris

    #1

    Swapping array elements from two different arrays

    How would I replace the elements with matching keys between $array1
    and $array2 to end up with 'a,b,c,d,e,f'.

    $array1 - Array ( [2] =c [3] =d )

    $array2 - Array ( [0] =a [1] =b [2] =x [3] =x [4] =e [5] =>
    f )

    Many thanks,

    Chris

  • badar.waqas@gmail.com

    #2
    Re: Swapping array elements from two different arrays

    On Nov 10, 3:28 pm, Chris <matchett...@go oglemail.comwro te:
    How would I replace the elements with matching keys between $array1
    and $array2 to end up with 'a,b,c,d,e,f'.
    >
    $array1 - Array ( [2] =c [3] =d )
    >
    $array2 - Array ( [0] =a [1] =b [2] =x [3] =x [4] =e [5] =>
    f )
    >
    Many thanks,
    >
    Chris
    you can use this code

    foreach($array1 as $key =$value)
    {
    if(array_key_ex ists($key, $array2))
    {
    $array2[$key] = $value;
    }
    }

    Comment

    • Chris

      #3
      Re: Swapping array elements from two different arrays

      On 10 Nov, 11:18, "badar.wa...@gm ail.com" <badar.wa...@gm ail.com>
      wrote:
      On Nov 10, 3:28 pm, Chris <matchett...@go oglemail.comwro te:
      >
      How would I replace the elements with matching keys between $array1
      and $array2 to end up with 'a,b,c,d,e,f'.
      >
      $array1 - Array ( [2] =c [3] =d )
      >
      $array2 - Array ( [0] =a [1] =b [2] =x [3] =x [4] =e [5] =>
      f )
      >
      Many thanks,
      >
      Chris
      >
      you can use this code
      >
      foreach($array1 as $key =$value)
      {
      if(array_key_ex ists($key, $array2))
      {
      $array2[$key] = $value;
      }
      >
      >
      >
      }- Hide quoted text -
      >
      - Show quoted text -
      Cheers,

      That has solved my problem...fulle r snippet given below.

      // Performing SQL query
      dbConnect("$db" );
      $query = "SELECT * FROM room";
      $result = mysql_query($qu ery) or die("Query failed : " .
      mysql_error());

      $query_booking = "SELECT * FROM booking WHERE weeknumber =
      $weeknumber";
      $result_booking = mysql_query($qu ery_booking) or die("Query failed :
      " . mysql_error());

      while($rowRoom = mysql_fetch_arr ay($result, MYSQL_ASSOC)) {
      $timetable = explode(',', $rowRoom['timetable']);
      }

      while($rowBooki ng = mysql_fetch_arr ay($result_book ing, MYSQL_ASSOC)) {
      $period = $rowBooking['period'];
      $teacher = $rowBooking['teacher'];
      $booking[$period] = "$teacher";
      }

      foreach($timeta ble as $key =$value)
      {
      if(array_key_ex ists($key, $booking))
      {
      $int = $key -1;
      $timetable[$int] = $booking[$period];
      }
      }

      Chris

      Comment

      • Rik Wasmus

        #4
        Re: Swapping array elements from two different arrays

        On Sat, 10 Nov 2007 11:28:49 +0100, Chris <matchett123@go oglemail.com
        wrote:
        How would I replace the elements with matching keys between $array1
        and $array2 to end up with 'a,b,c,d,e,f'.
        >
        $array1 - Array ( [2] =c [3] =d )
        >
        $array2 - Array ( [0] =a [1] =b [2] =x [3] =x [4] =>e [5] =>
        f )
        >
        Many thanks,
        A foreach loop would do. If your actual array has strings for keys, not
        integers, another approach is this:

        <?php
        $array1 = array('a' ='foo', 'd' ='bar','y' ='not valid');
        $array2 = array('a' ='x','b' ='foz','c' ='baz','d' ='x');

        $rows_to_insert = array_intersect _key($array1,$a rray2);
        $merged = array_merge($ar ray2,$rows_to_i nsert);
        print_r($merged );
        ?>
        --
        Rik Wasmus

        Comment

        Working...