php - Flatten multidimensional array not working -
i have 2 arrays following.
$alerts_array=array(1) { [0]=> array(11) { ["customalertsid"]=> int(3) ["customalerts_name"]=> string(10) "title demo" ["customalerts_publishdate"]=> string(10) "2016-07-03" ["customalerts_expirydate"]=> string(10) "2016-07-21" } } $singlebtn_array =array(3) { ["button_text0"]=> string(16) "button name1only" ["button_text1"]=> string(12) "button name2" ["button_text2"]=> string(16) "button name3_new" }
i have merged 2 arrays single multidimensional array looks following
$alerts_array = array_merge($alerts_array,$singlebtn_array); array(4) { [0]=> array(11) { ["customalertsid"]=> int(3) ["customalerts_name"]=> string(10) "title demo" ["customalerts_publishdate"]=> string(10) "2016-07-03" ["customalerts_expirydate"]=> string(10) "2016-07-21" } [1]=> array(1) { ["button_text0"]=> string(16) "button name1only" } [2]=> array(1) { ["button_text1"]=> string(12) "button name2" } [3]=> array(1) { ["button_text2"]=> string(16) "button name3_new" } }
i need both keys , values in new flattened array
i need this:
array(4) { [0]=> array(11) { ["customalertsid"]=> int(3) ["customalerts_name"]=> string(10) "title demo" ["customalerts_publishdate"]=> string(10) "2016-07-03" ["customalerts_expirydate"]=> string(10) "2016-07-21" ["button_text0"]=> string(16) "button name1only" ["button_text1"]=> string(12) "button name2" ["button_text2"]=> string(16) "button name3_new" }}
i have usedthe following code combining.
$newarr = array(); foreach ($alerts_array $key=>$tmp) { $newarr = array_merge($newarr, array_values($tmp)); }
the $newarr
giving me result ,but keys lost
simply merge first key [0]
of $alerts_array
, this:
$alerts_array = array_merge($alerts_array[0], $singlebtn_array);
that output:
array(7) { ["customalertsid"]=> int(3) ["customalerts_name"]=> string(10) "title demo" ["customalerts_publishdate"]=> string(10) "2016-07-03" ["customalerts_expirydate"]=> string(10) "2016-07-21" ["button_text0"]=> string(16) "button name1only" ["button_text1"]=> string(12) "button name2" ["button_text2"]=> string(16) "button name3_new" }
also see working demo here.
Comments
Post a Comment