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

laravel get jobs from redis queue located in another codebase\project?
How to concatenate variable and string that contains () in php
Laravel mysql multiple operations with one DB instance
How to add form data to a bootstrap modal in the same page by using jQuery?
WP - How to get all the comment_post_ID in bulk comment approval
How to grab a random MYSQL Column Value using PHP and echo it?
Secure way to allow a specific PHP script to read/write a directory?
php searching empty fields in the db [duplicate]
PHP strtotime returns incorrect month [duplicate]
Listing out team members with select options
Restart foreach loop in PHP
Can't map request body json into injected response object
Can't access local php website (Linux)
php code not work, need some change [duplicate]
Connect to multiple PHP code through one Java async task
24 hour format for the actual time?

Categories

HOME
rust
webview
plesk
mpdf
apple-push-notifications
transactions
odata
iverilog
magento-2.0
firebase-database
pyresttest
gtk
stack-overflow
twitter-oauth
xades4j
fireloop
distributed-computing
vmware-workstation
adobe-illustrator
clr
social-tables
restier
prompt
ipa
ruby-daemons
acrobat
pyspark-sql
fusion
toad
objectmapper
windows2012
quickcheck
express-session
pdfnet
mars-simulator
reset
imgur
python-jira
system-on-chip
oauth2-playground
raytracing
bit-shift
linq-to-excel
ssi
knockout-2.0
lightning-workbench
ilrepack
jgrapht
amazon-elastic-beanstalk
nslayoutconstraint
asp.net-web-api-routing
word-2007
portability
emacs25
rcharts
pjax
nclam
zynq
tsqlt
meld
uibinder
mathnet
swiffy
accessibilityservice
gravatar
management-studio-express
ui4j
json-spirit
android-studio-import
wso2ml
com0com
fuseesb
nssortdescriptor
subview
google-hangouts
flurry-analytics
iplimage
visual-studio-6
siena
hyperloglog
cidr
ivalueconverter
oxygene
.net-remoting
chartfx
sly-scroller
red-system
semantic-merge
voice-recording
netstream
asio
kernel32
cewolf
chunking
3-tier
ixmldomdocument
wmd-editor
main-method
downloadfile

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