+ Reply to Thread
Results 1 to 2 of 2

Thread: Array manipulation

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    40

    Array manipulation

    I have two arrays containing different keys and values. However, some keys store strings (integers) which I want to mix together in a single array (ie array_merge). All I need is to achieve is to collect these integers.

    using var_dump the arrays look like these:

    First Array :
    Code:
    array
        0 => 
            array
               'featured_pic' => string '740' (length=3)
        1 => 
            array
               'featured_pic' => string '741' (length=3)
        2 => 
            array
              'featured_pic' => string '742' (length=3)
    Second Array :
    Code:
    array
       0 => 
         array
           'accommodation_roomphoto' => string '456' (length=3)
           'accommodation_roomname' => string 'Single room' (length=11)
           'accommodation_roomsize' => string 'single' (length=6)
           'price_unit' => string '60' (length=2)
           'price_currency' => string 'USD' (length=3)
       1 => 
         array
           'accommodation_roomphoto' => string '434' (length=3)
           'accommodation_roomname' => string 'Double room' (length=11)
           'accommodation_roomsize' => string 'double' (length=6)
           'price_unit' => string '80' (length=2)
           'price_currency' => string 'USD' (length=3)
    What I really need is featured_pic from array#1 and accommodation_roomphoto from array#2. I need to collect all those numbers into a single array (I don't need the keys anymore - all I need is a series of numbers that come from those keys, in the example above: 740, 741, 742, 456, 434):
    Code:
    array
       0 => '740'
       1 => '741'
       2 => '742'
       3 => '456'
       4 => '434'
    The resulting array should be something like the example above (order is not important)

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    41
    If you know the keys you are interested in, this is just a simple looping job:
    Code:
    $result = array();
    
    foreach ($array1 as $item) {
      if (isset($item['featured_pic'])) $result[] = (int) $item['featured_pic'];
    }
    foreach ($array2 as $item) {
      if (isset($item['accommodation_roomphoto'])) $result[] = (int) $item['accommodation_roomphoto'];
    }
    
    print_r($result);
    Alternatively if you don't know the keys you want, this routine will grab all numbers stored as strings from both arrays:
    Code:
    $result = array();
    
    foreach ($array1 as $item) {
      foreach ($item as $sub) {
        if (is_numeric($sub)) $result[] = (int) $sub;
      }
    }
    foreach ($array2 as $item) {
      foreach ($item as $sub) {
        if (is_numeric($sub)) $result[] = (int) $sub;
      }
    }
    
    print_r($result);
    ...however this will not give you result you want from the array above, because the price_unit key in the second set of arrays is also numeric.

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts