0

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:

this is the first one

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)

and this is the second one

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):

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)

Thank you

unfulvio
  • 826
  • 3
  • 19
  • 34
  • 2
    Can you provide an example showing what you want your final array to look like? – Brian Glaz Jan 12 '12 at 16:35
  • check the two keys I'm interested in "accommodation_roomphoto" and "featured_pic", I only need series of numbers such as (numers are from the example above): '740', '741', 742', '456', '434 – unfulvio Jan 12 '12 at 16:39

3 Answers3

0
$new_array = array();

foreach($array1 as $array) {
    $new_array[] = intval($array['featured_pic']);
}

foreach($array2 as $array) {
    $new_array[] = intval($array['accommodation_roomphoto']);
}

This is my understanding of the question.

I have simply done a foreach on both arrays and taken out the required date from each.

I have also used intval() to convert the strings into integers in your new array.

Hope this helps.

amctavish
  • 389
  • 3
  • 10
0

If you know the keys you are interested in, this is just a simple looping job:

$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:

$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.

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
0

One-liner using array_merge and array_map. Assumes that your two arrays are $a and $b:

$result = array_merge(
             array_map(function($el) { return $el['featured_pic']; }, $a),
             array_map(function($el) { return $el['accomodation_roomphoto']; }, $b)
          );

Requires PHP >= 5.3 for the anonymous functions passed to array_map.

Jon
  • 428,835
  • 81
  • 738
  • 806