php


Remove the top level array and merge sub array into one


I have a multidimensional array and I want to remove the top level array and merge all its sub array into one array.
Below is my array:
$arr = [KEY1] => Array
(
[0] => Array
(
[Feb] => 120
)
[1] => Array
(
[Jan] => 230
)
[3] => Array
(
[Mar] => 340
)
)
[KEY2] => Array
(
[0] => Array
(
[Feb] => 12
)
[1] => Array
(
[Jan] => 23
)
[3] => Array
(
[Mar] => 34
)
)
I need to arrange and sort this array like below:
Output:
[KEY1] => Array
(
[Jan] => 230,
[Feb] => 120,
[Mar] => 340
)
[KEY2] => Array
(
[Jan] => 23,
[Feb] => 12,
[Mar] => 34
)
I have used
call_user_func_array('array_merge', $arr ); but not working.
Please suggest any wise way to do this.
Thanks
You can try like this as simple way,
$arr = [
'key1' =>[
0 => ["feb" => 123],
1 => ["dev" => 213],
2 => ["jan" => 111],
],
'key2' =>[
0 => ["feb" => 132],
1 => ["dev" => 321],
2 => ["jan" => 555],
],
];
$result = [];
foreach($arr as $k => $v){
foreach($v as $k1 => $v1){
foreach($v1 as $k2 => $v2)
$result[$k][$k2] = $v2;
}
}
print_r($result);
Hope this will solve your problem.
EDIT
Here is your sorting function.
function sortNestedArray(&$a)
{
sort($a);
for ($i = 0; $i < count($a); $i++) {
if (is_array($a[$i])) {
sortNestedArray($a[$i]);
}
}
return $a;
}
$a = sortNestedArray($result);
print_r($a);

Related Links

print_r function in php not working properly [duplicate]
Model and behavior use in cakephp
Using JSON to send form data via ajax to php, but FIRST response shows error when there isn't
Questions regarding a PHP SQL booking system [closed]
Could not create 'project properly says cakephp console in win 8
PHP explode function takes in more than 1 whitespace, PHP, EXPLODE, WHITESPACES
PHP json_encode() vs empty NSDictionary
How to select two tables and determine if the data is < or =
Optimize MySQL code
Why doesn't this work? PHP random bg image CSS. Laravel Slim Twig
Pass Variable to Register Sidebar?
Doctrine DBAL prepared query
PHPExcel Display Charts not working
Compare current time with values in an array true/false
Passing IPPReferenceType for parentref but the still sub customer data is not adding up in Quickbook API online
WordPress - archive side list not linking correctly

Categories

HOME
xpath
orientdb
events
artificial-intelligence
android-studio
facebook-messenger
workflow
qc
google-play-services
jint
repair
clone
haxe
angular-meteor
telegraf
yahoo
siesta
game-maker
orc
pm2
xbap
google-awareness
cpu-usage
wsf
jest
spyder
dashdb
raima
fusetools
hdmi
xdebug
acrobat
code-climate
pyspark-sql
splunk-query
copy-protection
winsock
publishing
riotjs
plantuml
android-collapsingtoolbar
xvfb
hidden-markov-models
access-denied
fiware-wirecloud
morphline
aws-kinesis-firehose
android-5.0-lollipop
md5-file
spring.net
patternlab.io
clrs
network-protocols
swiperefreshlayout
mifos
elastic4s
thread-exceptions
spring-mongo
okta-api
management-studio-express
flush
outlook.com
http.client
highest
calcite
ngcordova
swagger-maven-plugin
mojo
utf8-decode
global-scope
producer
tabris
osascript
aho-corasick
lumia-imaging-sdk
communicate
webautomation
miglayout
cidr
jms-serializer
eclim
ggts
disjoint-union
ember-table
html-frames
chefspec
smartfox
uipangesturerecognizer
background-repeat
servercontrol
fgetc
cleartype
mysqli-multi-query
powergui
watchpoint
service-factory

Resources

Encrypt Message