php


Getting values from different keys belonging to different arrays and merging them into another array


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

Related Links

Elasticsearch query for empty type give error and not working?
Laravel 5.1: Query Builder Select Multi table and SUM result
What is the best way to check if something exists with PDO
Receiving MySQL Error in PHPMyAdmin
$_REQUEST['action'] != 'foo' don't work in an if statement
SQL query debug
PHP query with string contain quotes
How can I verify that a date range includes a weekend?
echo issues with datetime-local format
php ldap search for loop query
Using consolibyte QuickBooks PHP framework - cannot import all employees 0x80040400 error
MySql is not Updating columns phpmyadmin
Ratchet is only processing one message at a time
Docker impossible to link nginx container with php-fpm container
PHP - Pulling array and splitting the strings
PHP Constant include

Categories

HOME
hpoo
office365
protocol-buffers
generics
freeradius
d3.js
qc
message-queue
plpgsql
visual-studio-2010
tfs2015
clone
graphdb
createjs
web-deployment
asciimath
word2vec
salt-cloud
social-tables
dashdb
pymongo
sybase-ase
android-service
network-analysis
acrobat
d3.js-v4
delayed-job
mongoengine
db2-luw
stackpanel
jslint
macports
unification
stat
data-uri
web-inspector
asihttprequest
supertest
ol3-google-maps
non-linear-regression
punctuation
winmerge
htmlspecialchars
perl-data-structures
cefpython
jsf-2.3
openseadragon
pack
supercomputers
algebraixlib
deque
google-hangouts
dos2unix
datagridcomboboxcolumn
inequality
lmax
csslint
radius-protocol
gpu-programming
assetic
insertonsubmit
ldif
bcdedit
examine
contextswitchdeadlock
semantic-merge
daap
instance-variables
thejit
series-40
delphi-prism
firefly-mv
sql-parametrized-query
startupscript
django-pagination
mongrel
getimagesize
external-sorting
prism-2
bindable-linq

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App