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