Array
(
[0] => Array
(
[id] => 123-456-000-000
[name] => john
)
[1] => Array
(
[id] => 123-456-789-014
[name] => james
)
)
Array
(
[0] => Array
(
[id] => 123-456-000-000
[address] => Japan
)
[1] => Array
(
[tin] => 123-456-789-014
[address] => Spain
)
)
I have 2 array above and I want to combine them so i used array-merge. But I didnt get the result i want. What it gave me is
Array
(
[0] => Array
(
[id] => 123-456-000-000
[name] => john
)
[1] => Array
(
[id] => 123-456-789-014
[name] => james
)
[2] => Array
(
[tin] => 123-456-000-000
[address] => Japan
)
[3] => Array
(
[tin] => 123-456-789-014
[address] => Spain
)
)
What i wanted is
Array
(
[0] => Array
(
[id] => 123-456-000-000
[name] => john
[address] => Japan
)
[1] => Array
(
[id] => 123-456-789-014
[name] => james
[address] => Spain
)
)
How to achieve this kind of array merge
Assume both array have the same number of elements and in the same order, then one foreach loop can do the job
$arr1 = Array (
0 => Array (
"id" => "123-456-000-000",
"name" => "john"
),
1 => Array (
"id" => "123-456-789-014",
"name" => "james"
)
);
$arr2 = Array (
0 => Array (
"tin" => "123-456-000-000",
"address" => "Japan"
),
1 => Array (
"tin" => "123-456-789-014",
"address" => "Spain"
)
);
foreach ($arr2 as $key => $inner) {
$arr1[$key]["address"] = $inner["address"];
}
output: $arr1
Array
(
[0] => Array
(
[id] => 123-456-000-000
[name] => john
[address] => Japan
)
[1] => Array
(
[id] => 123-456-789-014
[name] => james
[address] => Spain
)
)
Supposing that your arrays are at the same size and that your IDs are ordered int he same way.
Then you can try:
$arrayReturn = array();
foreach( $array1 as $key1 => $result1 ){
$mergeArray = [$result1['id'], $result1['name'], $array2[$key1]['name']];
array_push($arrayReturn, $mergeArray);
}
Where $array1 and $array2 are both of my initial arrays. $arrayReturn is my result.
Is there no other ways for you to directly merge the arrays at their creation? It would be much more elegant and will avoid all that.
Related
Array
(
[3M] => Array
(
[0] => Array
(
[name] => 3M
[price] => 158.15
)
[440] => Array
(
[name] => 3M
[price] => 156.69
)
)
[AO Smith] => Array
(
[1] => Array
(
[name] => AO Smith
[price] => 47.29
)
[441] => Array
(
[name] => AO Smith
[price] => 47.19
)
)
So I have an Array that is above^^^. I would like to get it into a condensed array format. I need a function that loops through the above and outputs it in the format below.
Array
(
[3M] => Array
(
[price1] => 158.15
[price2] => 156.69
)
[AO Smith] => Array
(
[price1] => 47.29
[price2] => 47.19
)
)
Above is how I would like the data oriented.
Thanks for the help.
What you'll find is the format you want is not good and not as usable or flexible. This however will give you a better format. name and price are descriptive, price1 and price2 are no different than 0 and 1:
foreach($array as $key => $values) {
$result[$key] = array_column($values, 'price');
}
Yields:
Array
(
[3M] => Array
(
[0] => 158.15
[1] => 156.69
)
[AO Smith] => Array
(
[0] => 47.29
[1] => 47.19
)
)
I have this array with information, which I want to create a new associative array with. Each key in the associative array should be the "name" from the old one. And in each new key, I want the corresponding information to be collected.
Array
(
[0] => Array
(
[id] => 1
[counter] => 21478813
[serie] => 2607171234
[name] => Ben
)
[1] => Array
(
[id] => 2
[counter] => 21478858
[serie] => 2607177151
[name] => Evan
)
[2] => Array
(
[id] => 3
[counter] => 21478817
[serie] => 2607171341
[name] => Steve
)
[3] => Array
(
[id] => 4
[counter] => 21471798
[serie] => 2607178561
[name] => Ben
)
[4] => Array
(
[id] => 5
[counter] => 21478811
[serie] => 2607171347
[name] => Ben
)
)
This is the array I'm trying to create:
Array
(
["Ben"] => Array
(
[0] => Array
(
[id] => 1
[counter] => 21478813
[serie] => 2607171234
[name] => Ben
)
[1] => Array
(
[id] => 4
[counter] => 21471798
[serie] => 2607178561
[name] => Ben
)
[2] => Array
(
[id] => 5
[counter] => 21478811
[serie] => 2607171347
[name] => Ben
)
)
["Evan"] => Array
(
[0] => Array
(
[id] => 2
[counter] => 21478858
[serie] => 2607177151
[name] => Evan
)
)
["Steve"] => Array
(
[0] => Array
(
[id] => 3
[counter] => 21478817
[serie] => 2607171341
[name] => Steve
)
)
)
$newArr = array();
foreach($myArr as $value) {
$name = $value['name'];
if (isset($newArr[$name])) {
$newArr[$name][] = $value;
}
else {
$newArr[$name] = array($value);
}
}
Use a foreach loop to create a new array:
$newArr = [];
foreach($myArr as $key => $value){
$newArr[$myArr[$key][$value['name']]][] = $myArr[$key];
}
i have big problem, because i don't know how get values from this array where value is be key into new array. This is my source array
Array
(
[0] => Array
(
[ID] => 250602
[NAME] => qwe
)
[1] => Array
(
[ID] => 250603
[NAME] => wer
)
[2] => Array
(
[ID] => 250629
[NAME] => sdf
)
[3] => Array
(
[ID] => 250629
[NAME] => xcv
)
[4] => Array
(
[ID] => 250629
[NAME] => fghfgh
)
[5] => Array
(
[ID] => 250601
[NAME] => pggd
)
[6] => Array
(
[ID] => 250601
[NAME] => dfgdfg
)
[7] => Array
(
[ID] => 250606
[NAME] => dfgdfg
)
)
When id is the same it will be created a new table that will look like for id = 250629
[NAME] => Array
(
[0] => sdf
[1] => xcv
[2] => fghfgh
)
How about foreach loop like this?
<?php
$final_array=array();
foreach($arrays as $sub_arr){ //it will traverse loop for all sub-arrays
$final_array[$sub_arr['ID']][]=$sub_arr['NAME'];
}
print_r($final_array); //you should see expected output.
?>
It will product below output for your given data:
Array
(
[250602] => Array
(
[0] => qwe
)
[250603] => Array
(
[0] => wer
)
[250629] => Array
(
[0] => sdf
[1] => xcv
[2] => fghfgh
)
[250601] => Array
(
[0] => pggd
[1] => dfgdfg
)
[250606] => Array
(
[0] => dfgdfg
)
)
Working Demo
Like this
$by_name = array();
foreach($your_array as $item)
$by_name[$item['ID']] []= $item['name'];
This makes use of php's lazy array initialization ([]= creates a new array implicitly).
If you get your array from mysql, you might also consider GROUP_CONCAT.
I search a solution to transform an array to another array. I have this one :
Array
(
[Germany] => Array
(
[0] => Munich
[1] => Frankfurt
)
[France] => Array
(
[0] => Paris
[1] => Marseille
[2] => Lille
[3] => Starsbourg
[4] => Lyon
[5] => Bordeaux
[6] => Toulouse
)
[Spain] => Array
(
[0] => Madrid
[1] => Barcelona
[2] => Valencia
)
)
What is the best way (with array_filter for example, or any other PHP function) to transform it to this please :
Array
(
[0] => Array
(
[value] => Germany
[cities] => Array
(
[0] => Munich
[1] => Frankfurt
)
)
[1] => Array
(
[value] => France
[cities] => Array
(
[0] => Paris
[1] => Marseille
[2] => Lille
[3] => Starsbourg
[4] => Lyon
[5] => Bordeaux
[6] => Toulouse
)
)
[0] => Array
(
[value] => Spain
[cities] => Array
(
[0] => Madrid
[1] => Barcelona
[2] => Valencia
)
)
)
$new_array = array();
foreach ($old_array as $country => $cities)
{
$new_array[] = array(
'value' => $country,
'cities' => $cities
);
}
Should do the job.
You could use array_map:
$newArray = array_map(function($key, $val){
return array(
'value' => $key,
'cities' => $val
);
}, array_keys($oldArray), $oldArray);
I have two arrays that have common indexes (church and office). I need to "merge" the total of the first array into the second array to get the desired output (seen below the double line). I'm not sure how to do this with array_merge(). Any help would be greatly appreciated!
Array
(
[church] => Array
(
[total] => 77
)
[office] => Array
(
[total] => 202
)
)
Array
(
[church] => Array
(
[name] => Array
(
[0] => Bill
[1] => Sally
)
[addr] => Array
(
[0] => Address Same as Billing
[1] => Address Same as Billing
)
[message] => Array
(
[0] =>
[1] =>
)
[amount] => Array
(
[0] => 25
[1] => 50
)
)
[office] => Array
(
[name] => Array
(
[0] => Marta
[1] => Ruth
)
[addr] => Array
(
[0] => Address Same as Billing
[1] => Address Same as Billing
)
[message] => Array
(
[0] =>
[1] =>
)
[amount] => Array
(
[0] => 100
[1] => 100
)
)
)
====================================================
Array
(
[church] => Array
(
[total] => 77
[name] => Array
(
[0] => Bill
[1] => Sally
)
[addr] => Array
(
[0] => Address Same as Billing
[1] => Address Same as Billing
)
[message] => Array
(
[0] =>
[1] =>
)
[amount] => Array
(
[0] => 25
[1] => 50
)
)
[office] => Array
(
[total] => 202
[name] => Array
(
[0] => Marta
[1] => Ruth
)
[addr] => Array
(
[0] => Address Same as Billing
[1] => Address Same as Billing
)
[message] => Array
(
[0] =>
[1] =>
)
[amount] => Array
(
[0] => 100
[1] => 100
)
)
)
Try something like this:
$a = array('church' => array('total' => 5), 'office' => array('total' => 10));
$b = array('church' => array('name' => 'church'), 'office' => array('name' => 'office'));
foreach ( $b as $key => $value ) {
$b[$key] = array_merge($a[$key], $b[$key]);
}