How to get part of array excluding one key in multidimensional array? - php

I'm grouping one multidimensional array by age.
This is my code:
$mEmployees = array (
array("name"=>"Pedro", "age"=>20, "ID"=>1111),
array("name"=>"Carlos", "age"=>15, "ID"=>2222),
array("name"=>"Susana", "age"=>20, "ID"=>3333),
array("name"=>"Carmen", "age"=>19, "ID"=>4444)
);
$byAge=array();
foreach ($mEmployees as $k => $oneItem) {
$byAge[$oneItem['age']][$k] = $oneItem;
}
var_dump($byAge);
That works fine as you can see below:
output:
array(3) {
[20]=>
array(2) {
[0]=>
array(3) {
["name"]=>
string(5) "Pedro"
["age"]=>
int(20)
["ID"]=>
int(1111)
}
[2]=>
array(3) {
["name"]=>
string(6) "Susana"
["age"]=>
int(20)
["ID"]=>
int(3333)
}
}
[15]=>
array(1) {
[1]=>
array(3) {
["name"]=>
string(6) "Carlos"
["age"]=>
int(15)
["ID"]=>
int(2222)
}
}
[19]=>
array(1) {
[3]=>
array(3) {
["name"]=>
string(6) "Carmen"
["age"]=>
int(19)
["ID"]=>
int(4444)
}
}
}
But in the results, the age key is redundant. I want to remove this key in the $byAge array.
I tried with array_slice, but it's not possible to indicate one irregular offset (the key age is in middle).
How I can achieve this easily for this result?
array(3) {
[20]=>
array(2) {
[0]=>
array(3) {
["name"]=>
string(5) "Pedro"
["ID"]=>
int(1111)
}
[2]=>
array(3) {
["name"]=>
string(6) "Susana"
["ID"]=>
int(3333)
}
}
[15]=>
array(1) {
[1]=>
array(3) {
["name"]=>
string(6) "Carlos"
["ID"]=>
int(2222)
}
}
[19]=>
array(1) {
[3]=>
array(3) {
["name"]=>
string(6) "Carmen"
["ID"]=>
int(4444)
}
}
}

Cache the age value in a variable and unset from $oneItem.
foreach ($mEmployees as $k => $oneItem) {
$age = $oneItem['age'];
unset($oneItem['age']);
$byAge[$age][$k] = $oneItem;
}
Demo: https://3v4l.org/pDDn5

Related

How to split multidimensional array into arrays based on the values - PHP

I have this array, it could look something like this:
array(756) {
[0]=>
array(2) {
[0]=>
string(12) "joint_temps5"
[1]=>
string(4) "23.5"
}
[1]=>
array(2) {
[0]=>
string(12) "joint_temps4"
[1]=>
string(4) "23.5"
}
[2]=>
array(2) {
[0]=>
string(12) "joint_temps3"
[1]=>
string(2) "24"
}
[3]=>
array(2) {
[0]=>
string(12) "joint_temps2"
[1]=>
string(4) "24.5"
}
[4]=>
array(2) {
[0]=>
string(12) "joint_temps1"
[1]=>
string(2) "25"
}
[5]=>
array(2) {
[0]=>
string(12) "joint_temps0"
[1]=>
string(4) "25.5"
}
[6]=>
array(2) {
[0]=>
string(12) "joint_temps5"
[1]=>
string(4) "23.5"
}
[7]=>
array(2) {
[0]=>
string(12) "joint_temps4"
[1]=>
string(4) "23.5"
}
[8]=>
array(2) {
[0]=>
string(12) "joint_temps3"
[1]=>
string(2) "24"
}
[9]=>
array(2) {
[0]=>
string(12) "joint_temps2"
[1]=>
string(4) "24.5"
}
[10]=>
array(2) {
[0]=>
string(12) "joint_temps1"
[1]=>
string(2) "25"
}
[11]=>
array(2) {
[0]=>
string(12) "joint_temps0"
[1]=>
string(4) "25.5"
}
etc...};
How would i go about looping thru and splitting it up into arrays based on the value in the inner arrays[0] ex: "joint_temps5".
I have tested quite a few things but without success. My problem mainly is i dont know what might be in the string in the arrays.
I would like to end up with arrays like:
$array1[] = array(x_amount){
[0]=>
array(2) {
[0]=>
string(12) "joint_temps5"
[1]=>
string(4) "23.5"
}
[1]=>
array(2) {
[0]=>
string(12) "joint_temps5"
[1]=>
string(4) "23.5"
}
}
$array2[] = array(x_amount){
[0]=>
array(2) {
[0]=>
string(12) "joint_temps4"
[1]=>
string(4) "23.5"
}
[1]=>
array(2) {
[0]=>
string(12) "joint_temps4"
[1]=>
string(4) "23.5"
}
}
}
etc.
I would recommend to create a new array from your input array, using the value as an index of the array to be created, like so:
// test-set: input array is $a
$a[0] = array("joint_temps5","23.5");
$a[1] = array("joint_temps3","24");
$a[2] = array("joint_temps2","24.5");
$a[3] = array("joint_temps1","25");
$a[4] = array("joint_temps0","25.5");
$a[5] = array("joint_temps5","23.5");
$a[6] = array("joint_temps4","23.5");
$a[7] = array("joint_temps3","24");
$a[8] = array("joint_temps2","24.5");
$a[9] = array("joint_temps1","25");
foreach($a as $key => $value){
$b[$value[0]][] = $value; // *Explained below
}
*"Explained below": $a is the source array, $b is the newly created array.
$b[$value[0]][] means it wil create a new element for array $b[$value[0]]. And $value[0] will be substituted by the first value in the element of $a that the foreach loop hits.
Example: the first element of $a is this array: array("joint_temps5","23.5"). So in the foreach loop, the text "joint_temps5" ($value[0] in the foreach) will be used as a key/index to create a new element for array $b. The [] means that with every new execution of this line, a new element, with that key value $value[0], will be added.
It will result in:
array(6) {
["joint_temps5"]=>
array(2) {
[0]=>
array(2) {
[0]=>
string(12) "joint_temps5"
[1]=>
string(4) "23.5"
}
[1]=>
array(2) {
[0]=>
string(12) "joint_temps5"
[1]=>
string(4) "23.5"
}
}
["joint_temps3"]=>
array(2) {
[0]=>
array(2) {
[0]=>
string(12) "joint_temps3"
[1]=>
string(2) "24"
}
[1]=>
array(2) {
[0]=>
string(12) "joint_temps3"
[1]=>
string(2) "24"
}
}
["joint_temps2"]=>
array(2) {
[0]=>
array(2) {
[0]=>
string(12) "joint_temps2"
[1]=>
string(4) "24.5"
}
[1]=>
array(2) {
[0]=>
string(12) "joint_temps2"
[1]=>
string(4) "24.5"
}
}
["joint_temps1"]=>
array(2) {
[0]=>
array(2) {
[0]=>
string(12) "joint_temps1"
[1]=>
string(2) "25"
}
[1]=>
array(2) {
[0]=>
string(12) "joint_temps1"
[1]=>
string(2) "25"
}
}
["joint_temps0"]=>
array(1) {
[0]=>
array(2) {
[0]=>
string(12) "joint_temps0"
[1]=>
string(4) "25.5"
}
}
["joint_temps4"]=>
array(1) {
[0]=>
array(2) {
[0]=>
string(12) "joint_temps4"
[1]=>
string(4) "23.5"
}
}
}
You could loop through your array, and populate a new array using the string as a key, so something like:
foreach ($array as $working_array) {
$new_array[$working_array[0]][] = $working_array[1]; }
Which would give you an array something like :
$new_array["joint_temps5"]=> array(2) {
[0]=> "23.5"
[1]=> "23.5"}
If you needed to you could then parse that into an array in the format you desire quite easily.

PHP - Reindexing nested array recursively

Wrote this recursive function to reindex an array (see image below - JSONized Array I work with, to better see it)
Code:
private function reindex($array)
{
foreach ($array as $key => $value) {
if (is_array($value)) {
$array[$key]=$this->reindex($value);
}
array_values($array);
}
return $array;
}
The goal is to reindex it so there are no jump in indexes i.e
$arr['body'][0]['1']['body'] has elements at indexes 2, 4 and 6, would like to reindex it to be 0, 1, 2
Should work for different arrays also (n amount of nested arrays). Should be general function.
How can I do it?
Thanks,
Update 1:
Var_dump output:
array(4) {
["token_name"]=>
string(6) "C_ROOT"
["token_group"]=>
string(7) "C_BLOCK"
["group"]=>
bool(true)
["body"]=>
array(1) {
[0]=>
array(2) {
[0]=>
array(7) {
["token_name_org"]=>
string(8) "T_SWITCH"
["token"]=>
int(339)
["value"]=>
string(6) "switch"
["line"]=>
int(2)
["token_group"]=>
string(9) "FUNCTIONS"
["token_name"]=>
string(8) "C_SWITCH"
["args"]=>
array(1) {
[0]=>
array(1) {
[0]=>
array(7) {
["token_name_org"]=>
string(10) "T_VARIABLE"
["token"]=>
int(320)
["value"]=>
string(4) "argv"
["line"]=>
int(2)
["token_group"]=>
string(9) "VARIABLES"
["token_name"]=>
string(10) "C_VARIABLE"
["args"]=>
array(1) {
[0]=>
array(1) {
[0]=>
array(6) {
["token_name_org"]=>
string(9) "T_LNUMBER"
["token"]=>
int(317)
["value"]=>
string(1) "1"
["line"]=>
int(2)
["token_group"]=>
string(9) "VARIABLES"
["token_name"]=>
string(8) "C_NUMBER"
}
}
}
}
}
}
}
[1]=>
array(4) {
["token_name"]=>
string(7) "C_BLOCK"
["token_group"]=>
string(7) "C_BLOCK"
["group"]=>
bool(true)
["body"]=>
array(4) {
[0]=>
array(1) {
[0]=>
array(4) {
["token_name"]=>
string(12) "C_CASE_BLOCK"
["token_group"]=>
string(12) "C_CASE_BLOCK"
["group"]=>
bool(true)
["args"]=>
array(1) {
[0]=>
array(6) {
["token_name_org"]=>
string(9) "T_LNUMBER"
["token"]=>
int(317)
["value"]=>
string(2) "10"
["line"]=>
int(4)
["token_group"]=>
string(9) "VARIABLES"
["token_name"]=>
string(8) "C_NUMBER"
}
}
}
}
[2]=>
array(1) {
[0]=>
array(5) {
["token_name"]=>
string(12) "C_CASE_BLOCK"
["token_group"]=>
string(12) "C_CASE_BLOCK"
["group"]=>
bool(true)
["args"]=>
array(1) {
[0]=>
array(6) {
["token_name_org"]=>
string(26) "T_CONSTANT_ENCAPSED_STRING"
["token"]=>
int(323)
["value"]=>
string(13) "single_quoted"
["line"]=>
int(7)
["token_group"]=>
string(7) "STRINGS"
["token_name"]=>
string(8) "C_STRING"
}
}
["body"]=>
array(1) {
[0]=>
array(5) {
[0]=>
array(6) {
["token_name_org"]=>
string(10) "T_VARIABLE"
["token"]=>
int(320)
["value"]=>
string(3) "val"
["line"]=>
int(8)
["token_group"]=>
string(9) "VARIABLES"
["token_name"]=>
string(10) "C_VARIABLE"
}
[1]=>
array(6) {
["token_name_org"]=>
string(18) "C_ASSIGNMENT_EQUAL"
["line"]=>
int(8)
["value"]=>
string(1) "="
["token"]=>
string(5) "VALUE"
["token_group"]=>
string(11) "ASSIGNMENTS"
["token_name"]=>
string(18) "C_ASSIGNMENT_EQUAL"
}
[2]=>
array(6) {
["token_name_org"]=>
string(9) "T_LNUMBER"
["token"]=>
int(317)
["value"]=>
string(1) "3"
["line"]=>
int(8)
["token_group"]=>
string(9) "VARIABLES"
["token_name"]=>
string(8) "C_NUMBER"
}
[3]=>
array(6) {
["token_name_org"]=>
string(7) "VALUE_-"
["line"]=>
int(8)
["value"]=>
string(1) "-"
["token"]=>
string(5) "VALUE"
["token_group"]=>
string(9) "OPERATORS"
["token_name"]=>
string(16) "C_OPERATOR_MINUS"
}
[4]=>
array(6) {
["token_name_org"]=>
string(9) "T_LNUMBER"
["token"]=>
int(317)
["value"]=>
string(1) "5"
["line"]=>
int(8)
["token_group"]=>
string(9) "VARIABLES"
["token_name"]=>
string(8) "C_NUMBER"
}
}
}
}
}
[4]=>
array(1) {
[0]=>
array(5) {
["token_name"]=>
string(12) "C_CASE_BLOCK"
["token_group"]=>
string(12) "C_CASE_BLOCK"
["group"]=>
bool(true)
["args"]=>
array(1) {
[0]=>
array(6) {
["token_name_org"]=>
string(26) "T_CONSTANT_ENCAPSED_STRING"
["token"]=>
int(323)
["value"]=>
string(13) "double_quoted"
["line"]=>
int(11)
["token_group"]=>
string(7) "STRINGS"
["token_name"]=>
string(8) "C_STRING"
}
}
["body"]=>
array(1) {
[0]=>
array(1) {
[0]=>
array(7) {
["token_name_org"]=>
string(6) "T_ECHO"
["token"]=>
int(328)
["value"]=>
string(4) "echo"
["line"]=>
int(13)
["token_group"]=>
string(9) "FUNCTIONS"
["token_name"]=>
string(6) "C_ECHO"
["args"]=>
array(1) {
[0]=>
array(1) {
[0]=>
array(6) {
["token_name_org"]=>
string(26) "T_CONSTANT_ENCAPSED_STRING"
["token"]=>
int(323)
["value"]=>
string(13) "double quoted"
["line"]=>
int(13)
["token_group"]=>
string(7) "STRINGS"
["token_name"]=>
string(8) "C_STRING"
}
}
}
}
}
}
}
}
[6]=>
array(3) {
[0]=>
array(4) {
["token_name_org"]=>
string(9) "T_DEFAULT"
["token"]=>
int(342)
["value"]=>
string(7) "default"
["line"]=>
int(18)
}
[1]=>
array(6) {
["token_name_org"]=>
string(17) "C_SEPARATOR_COLON"
["line"]=>
int(18)
["value"]=>
string(1) ":"
["token"]=>
string(5) "VALUE"
["token_group"]=>
string(10) "SEPARATORS"
["token_name"]=>
string(17) "C_SEPARATOR_COLON"
}
[2]=>
array(6) {
["token_name_org"]=>
string(7) "T_BREAK"
["token"]=>
int(343)
["value"]=>
string(5) "break"
["line"]=>
int(19)
["token_group"]=>
string(16) "TODO_BREAK_GROUP"
["token_name"]=>
string(7) "C_BREAK"
}
}
}
}
}
}
}
Here is a method that will only reindex numeric values:
function reindex($array)
{
$index = 0;
$return = [];
foreach ($array as $key => $value) {
if (is_string($key)) {
$newKey = $key;
} else {
$newKey = $index;
++$index;
}
$return[$newKey] = is_array($value) ? reindex($value) : $value;
}
// Sort alphabetically, numeric first then alpha
ksort($return, SORT_NATURAL);
return $return;
}
Example here: http://ideone.com/969OGa

PHP Nested Array convert into flat array

I have a problem to transform my hierarchical array like this:
array(
[0]=>
array(3) {
["id"]=>
int(2353011010)
["name"]=>
string(17) "LEDER ACCESSOIRES"
["order"]=>
int(15)
}
[1]=>
array(3) {
["id"]=>
int(2371475010)
["name"]=>
string(15) "SPORT AUFKLEBER"
["order"]=>
int(25)
}
[2]=>
array(4) {
["id"]=>
int(2563635010)
["name"]=>
string(17) "KENNZEICHENHALTER"
["order"]=>
int(10)
["children"]=>
array(6) {
[0]=>
array(4) {
["id"]=>
int(3854259010)
["name"]=>
string(9) "EDELSTAHL"
["order"]=>
int(92)
["children"]=>
array(2) {
[0]=>
array(3) {
["id"]=>
int(20878056010)
["name"]=>
string(5) "test1"
["order"]=>
int(1)
}
}
}
[1]=>
array(3) {
["id"]=>
int(3854260010)
["name"]=>
string(5) "CHROM"
["order"]=>
int(91)
}
}
[3]=>
array(4) {
["id"]=>
int(19754330010)
["name"]=>
string(30) "SCHALTMANSCHETTEN CARBON OPTIK"
["order"]=>
int(3)
}
}
)
Into a flat ones like this:
array(
[0]=>
array(3) {
["id"]=>
int(2353011010)
["name"]=>
string(17) "LEDER ACCESSOIRES"
["order"]=>
int(15)
}
[1]=>
array(3) {
["id"]=>
int(2371475010)
["name"]=>
string(15) "SPORT AUFKLEBER"
["order"]=>
int(25)
}
[2]=>
array(3) {
["id"]=>
int(2563635010)
["name"]=>
string(17) "KENNZEICHENHALTER"
["order"]=>
int(10)
}
[3]=>
array(4) {
["id"]=>
int(3854259010)
["name"]=>
string(9) "EDELSTAHL"
["order"]=>
int(92),
["parentId"]=> 2563635010
}
[4]=>
array(4) {
["id"]=>
int(20878056010)
["name"]=>
string(5) "test1"
["order"]=>
int(1),
["parentId"]=> 2563635010
}
[5]=>
array(4) {
["id"]=>
int(3854260010)
["name"]=>
string(5) "CHROM"
["order"]=>
int(91),
["parentId"]=> 2563635010
}
[6]=>
array(4) {
["id"]=>
int(19754330010)
["name"]=>
string(30) "SCHALTMANSCHETTEN CARBON OPTIK"
["order"]=>
int(3)
}
)
The children antities should be removed and every child element should get a parentId entity of the higher level id. I need this solution for transfering into DB.
thx
Now, I have create a "temporary" method that works for me, but is noch flexible to use:
function recursive($categories) {
foreach ($categories as $value) {
$result[$value->id]['id'] = $value->id;
$result[$value->id]['name'] = $value->name;
$result[$value->id]['order'] = $value->order;
$result[$value->id]['parentId'] = 0;
if(isset($value->children)) {
$parentId = $value->id;
foreach($value->children as $value2) {
$result[$value2->id]['id'] = $value2->id;
$result[$value2->id]['name'] = $value2->name;
$result[$value2->id]['parentId'] = $parentId;
if(isset($value2->children)) {
$parentId = $value2->id;
foreach($value2->children as $value3) {
$result[$value3->id]['id'] = $value3->id;
$result[$value3->id]['name'] = $value3->name;
$result[$value3->id]['parentId'] = $parentId;
}
}
}
}
}
return $result;
}
Do anybody know a recursive solution for this method?

Loop through PHP multidimensional array

I am trying to loop through a multidimensional array but in the foreach loop it just outputs error
index 'name' not found. index 'calories' not founder
foreach($responsex['foods'] as $fx5)
{
echo($fx5['name']);
echo($fx5['calories']);
}
Response: i.e. $responsex
array ( 'encodedId' => '4H8xxx', 'displayName' => 'sam', )array(3) {
["foods"]=> array(3) { [0]=> array(5) { ["isFavorite"]=> bool(false)
["logDate"]=> string(10) "2016-04-15" ["logId"]=> int(7139364449)
["loggedFood"]=> array(10) { ["accessLevel"]=> string(6) "PUBLIC"
["amount"]=> int(2) ["brand"]=> string(0) "" ["calories"]=> int(574)
["foodId"]=> int(536497687) ["locale"]=> string(5) "en_AU"
["mealTypeId"]=> int(7) ["name"]=> string(14) "Potato Pudding"
["unit"]=> array(3) { ["id"]=> int(91) ["name"]=> string(3) "cup"
["plural"]=> string(4) "cups" } ["units"]=> array(8) { [0]=> int(6754)
[1]=> int(91) [2]=> int(256) [3]=> int(279) [4]=> int(226) [5]=>
int(180) [6]=> int(147) [7]=> int(389) } } ["nutritionalValues"]=>
array(6) { ["calories"]=> int(574) ["carbs"]=> float(49.16) ["fat"]=>
float(34.98) ["fiber"]=> float(3.6) ["protein"]=> float(16.1)
["sodium"]=> int(1524) } } [1]=> array(5) { ["isFavorite"]=>
bool(false) ["logDate"]=> string(10) "2016-04-15" ["logId"]=>
int(7138517833) ["loggedFood"]=> array(10) { ["accessLevel"]=>
string(6) "PUBLIC" ["amount"]=> int(1) ["brand"]=> string(0) ""
["calories"]=> int(359) ["foodId"]=> int(535239347) ["locale"]=>
string(5) "en_AU" ["mealTypeId"]=> int(7) ["name"]=> string(54) "Fish,
Noodles and Vegetables in Cheese Sauce (Mixture)" ["unit"]=> array(3)
{ ["id"]=> int(91) ["name"]=> string(3) "cup" ["plural"]=> string(4)
"cups" } ["units"]=> array(8) { [0]=> int(6837) [1]=> int(91) [2]=>
int(256) [3]=> int(279) [4]=> int(226) [5]=> int(180) [6]=> int(147)
[7]=> int(389) } } ["nutritionalValues"]=> array(6) { ["calories"]=>
int(359) ["carbs"]=> float(28.01) ["fat"]=> float(14.05) ["fiber"]=>
float(2.9) ["protein"]=> float(29.08) ["sodium"]=> int(534) } } [2]=>
array(5) { ["isFavorite"]=> bool(false) ["logDate"]=> string(10)
"2016-04-15" ["logId"]=> int(7138326866) ["loggedFood"]=> array(10) {
["accessLevel"]=> string(6) "PUBLIC" ["amount"]=> int(1) ["brand"]=>
string(0) "" ["calories"]=> int(157) ["foodId"]=> int(536493638)
["locale"]=> string(5) "en_AU" ["mealTypeId"]=> int(7) ["name"]=>
string(11) "Cashew Nuts" ["unit"]=> array(3) { ["id"]=> int(226)
["name"]=> string(2) "oz" ["plural"]=> string(2) "oz" } ["units"]=>
array(4) { [0]=> int(226) [1]=> int(180) [2]=> int(147) [3]=> int(389)
} } ["nutritionalValues"]=> array(6) { ["calories"]=> int(157)
["carbs"]=> float(8.56) ["fat"]=> float(12.43) ["fiber"]=> float(0.9)
["protein"]=> float(5.17) ["sodium"]=> int(3) } } } ["goals"]=>
array(2) { ["calories"]=> int(1161) ["estimatedCaloriesOut"]=>
int(1411) } ["summary"]=> array(7) { ["calories"]=> int(1090)
["carbs"]=> float(85.73) ["fat"]=> float(61.46) ["fiber"]=> float(7.4)
["protein"]=> float(50.35) ["sodium"]=> int(2061) ["water"]=> int(0) }
}
you can recursively iterate through the arrays and print them as follows as key value pairs.
<?php
//initially call the function
print_array($responsex);
function print_array($array){
foreach($array as $key=>$value){
//recursively print the array
if(is_array($value)){
echo("Array : ".$key."\n");
print_array($value);
}
else{
echo($key." => ".$value);
}
}
}
?>
You can define additional tasks other than printing them with the above code.
Edit:
if you are sure that the array is two dimensional, no need to go recursively.
<?php
//initially call the function
print_array($responsex);
//if you are sure that the array is two dimensional, no need to go recursively.
function print_array($array){
foreach($array as $key=>$value){
if(is_array($value)){
if($key==="foods"){
var_dump($array[$key]);
}
}
else{
echo($key." => ".$value);
}
}
}
Use this way..
<?php
$keys = array_keys($data);// put your array name as a place of $data
$iterations = count($array[$keys[0]]);
for($i = 0; $i < $iterations; $i++) {
$data = array();
foreach($array as $key => $value) {
$data[$key] = $value[$i];
}
print_r($data);
}
?>

Flatten multi dimension array but maintain keys?

I have the following:
[6199]=>
array(12) {
["Origin"]=>
array(3) {
["name"]=>
array(1) {
[0]=>
string(4) "Cuba"
}
["slug"]=>
array(1) {
[0]=>
string(27) "cuabn-havana-habanos-cigars"
}
["id"]=>
array(1) {
[0]=>
int(0)
}
}
["Filler"]=>
array(3) {
["name"]=>
array(2) {
[0]=>
string(9) "Dominican"
[1]=>
string(10) "Nicaraguan"
}
["slug"]=>
array(2) {
[0]=>
string(9) "dominican"
[1]=>
string(10) "nicaraguan"
}
["id"]=>
array(2) {
[0]=>
int(0)
[1]=>
int(1)
}
}
}
[6192]=>
array(11) {
["Origin"]=>
array(3) {
["name"]=>
array(1) {
[0]=>
string(9) "Nicaragua"
}
["slug"]=>
array(1) {
[0]=>
string(27) "nicaraguan-new-world-cigars"
}
["id"]=>
array(1) {
[0]=>
int(0)
}
}
["Filler"]=>
array(3) {
["name"]=>
array(2) {
[0]=>
string(9) "Java"
[1]=>
string(10) "Nicaraguan"
}
["slug"]=>
array(2) {
[0]=>
string(9) "java"
[1]=>
string(10) "nicaraguan"
}
["id"]=>
array(2) {
[0]=>
int(0)
[1]=>
int(1)
}
}
}
and my expected output is:
array(12) {
["Origin"]=>
array(3) {
["name"]=>
array(1) {
[0]=>
string(4) "Cuba".
[1]=>
string(9) "Nicaragua"
}
["slug"]=>
array(1) {
[0]=>
string(27) "cuabn-havana-habanos-cigars",
[0]=>
string(27) "nicaraguan-new-world-cigars"
}
["id"]=>
array(1) {
[0]=>
int(0)
}
}
["Filler"]=>
array(3) {
["name"]=>
array(2) {
[0]=>
string(9) "Dominican"
[1]=>
string(10) "Nicaraguan"
[2]=>
string(9) "Java"
}
["slug"]=>
array(2) {
[0]=>
string(9) "dominican"
[1]=>
string(10) "nicaraguan"
[3]=>
string(9) "java"
}
["id"]=>
array(2) {
[0]=>
int(0)
[1]=>
int(1)
}
}
See how it eliminates dupes and merges each array maintaining the "origin" key.
I've tried :
foreach ($resultterms as $keyname => $valuename){
foreach ($valuename as $keysub => $valuesub) {
foreach($valuesub['name'] as $keysubsub => $valuesubsub){
# code...
$prods_atts[$keysub]['name'][$keysubsub] = $valuesubsub;
$prods_atts[$keysub]['slug'][$keysubsub] = $valuesub['slug'][$keysubsub];
$prods_atts[$keysub]['id'][$keysubsub] = $valuesub['id'][$keysubsub];
}
}
}
where $resultterms is the original arrays but it's not working. I was wondering if there was a wonderful php function I could use to merge these instead of so many nested for each loops?
I believe you're just looking for array_merge_recursive.
call_user_func_array('array_merge_recursive', array_values($prod_atts));
call_user_func_array allows to transform an array into a list of arguments
array_values because in the end, you seem to want to get rid of the first layer of your array
In order to try it, could you post the var_export of your variable instead of the var_dump?
echo(var_export($prod_atts, true));
merge your array by any suggested method. After that you will get duplicated values. And you need save only the unique items
$new = array_merge_recursive($resultterms['6199'], $resultterms['6192']);
foreach($new['Origin'] as &$item) { $item = array_unique($item); }
foreach($new['Filler'] as &$item) { $item = array_unique($item); }

Categories