Is there a function to combine these arrays in php? - php

Given arrays 'a1' and 'b1' how may they be combined them to produce the final array? Basically replacing the value within 'a1' with the array data for the matching value within 'b1'. I guess the question would be if there is a function that can do this that I'm not seeing.
$a1 = array('id1'=>array('a'=>'444-444',
'b'=>'222-222',
'c'=>'111-111'),
'id2'=>array('a'=>'888-888',
'b'=>'666-666',
'c'=>'555-555')
);
$b1 = array('222-222'=>array('first'=>array('9999',
'dddd',
'yyyy'),
'second'=>'mmgghh'
),
'666-666'=>array('first'=>array('bbbb',
'cccc',
'7777'),
'second'=>'ffffgggg'
)
);
Desired combination:
array(2) {
["id1"]=>
array(3) {
["a"]=>
string(7) "444-444"
["b"]=>
array(1) {
["222-222"]=>
array(2) {
["first"]=>
array(3) {
[0]=>
string(4) "9999"
[1]=>
string(4) "dddd"
[2]=>
string(4) "yyyy"
}
["second"]=>
string(6) "mmgghh"
}
}
["c"]=>
string(7) "111-111"
}
["id2"]=>
array(3) {
["a"]=>
string(7) "888-888"
["b"]=>
array(1) {
["666-666"]=>
array(2) {
["first"]=>
array(3) {
[0]=>
string(4) "bbbb"
[1]=>
string(4) "cccc"
[2]=>
string(4) "7777"
}
["second"]=>
string(6) "ffffgggg"
}
}
["c"]=>
string(7) "555-555"
}
}

array_walk_recursive($a1,function(&$value,$key,$addin){
if(is_scalar($value) && isset($addin[$value])){
$value = array($value=>$addin[$value]);
}
},$b1);

Related

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

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

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.

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); }

PHP: Count values in Arrays

I have this Array:
array(66) {
[0]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "105a"
}
[1]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "105b"
}
[2]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "105c"
}
[3]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "105d"
}
[4]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "117b"
}
[5]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "117c"
}
[6]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "123a"
}
[7]=> array(2) {
["location"]=> string(10) "Whateverelse" ["bsid"]=> string(4) "123b"
}
}
How can I count how many Breakwater's I have and how many Whateverelse's and get something like this:
array(2) {
[0]=> array(2) {
["Breakwater"]=> string(2) "20"
} [1]=> array(2) {
["Whateverelse"]=> string(1) "1"
}
}
Just loop around the original array, and each time a location is hit, increment a counter in the locations array where the index is the location.
$loc = array();
foreach($arr as $value) {
$location = $value['location'];
if(isset($loc[$location])) {
$loc[$location]++;
} else {
$loc[$location] = 1;
}
}
print_r($loc);
Will output
array
(
["Breakwater"] => 7,
["Whateverelse"] => 1
}
I got this finally :
$output = array("Breakwater" => 0, "Whateverelse" => 0);
foreach ($array as $val) {
$output["Breakwater"] += ($val["location"] == "Breakwater") ? 1 : 0;
$output["Whateverelse"] += ($val["location"] == "Whateverelse") ? 1 : 0;
}
var_dump($output);

Categories