php array merge & combine - php

I have three arrays & i like to convert it in one multidimensional array.
$array1=array('Kathy', 'Adam', 'Jenny');
$array2=array('student','teacher','driver');
$array3=array(2, 5, 8);
$mix=array();
$mix['name']=array_values( $array1);
$mix['profession']=array_values( $array2);
$mix['SL']=array_values( $array3);
& from those arrays i can get below output:
Array
(
[name] => Array
(
[0] => Kathy
[1] => Adam
[2] => Jenny
)
[profession] => Array
(
[0] => student
[1] => teacher
[2] => driver
)
[SL] => Array
(
[0] => 2
[1] => 5
[2] => 8
)
)
Now i would like to get below output from above... anybody please help me how to do this ?
[0]=>Array
(
[name] => Kathy
[profession] =>student
[SL] => 2
)
[1]=>Array
(
[name] => Adam
[profession] =>teacher
[SL] => 5
)
[2]=>Array
(
[name] => Jenny
[profession] =>driver
[SL] => 8
)

You can use array_map function to do that:
<?php
$array1 = array('Kathy', 'Adam', 'Jenny');
$array2 = array('student','teacher','driver');
$array3 = array(2, 5, 8);
$mix = array_map(
function ($name, $profession, $sl) {
return array('name' => $name, 'profession' => $profession, 'SL' => $sl);
},
$array1,
$array2,
$array3
);

$array1=array('Kathy', 'Adam', 'Jenny');
$array2=array('student','teacher','driver');
$array3=array(2, 5, 8);
$mix = array();
foreach ($array1 as $index => $val)
{
$mix[] = array(
'name' => $val,
'profession' => $array2[$index],
'SL' => $array3[$index]);
}

Use this:
$array1=array('Kathy', 'Adam', 'Jenny');
$array2=array('student','teacher','driver');
$array3=array(2, 5, 8);
$mix=array();
for($i=0;$i<count($array1);$i++){
$mix[$i]=array('name'=>$array1[$i],'profession'=>$array2[$i],'SL'=>$array3[$i]);
}
print_r($mix);
Use this if all of array has same size.

No problems, you can use this code:
$array1=array('Kathy', 'Adam', 'Jenny');
$array2=array('student','teacher','driver');
$array3=array(2, 5, 8);
$mix=array();
if (count($array1)==count($array2) && count($array2)==count($array3)){
$count = count($array1);
for ($i=0;$i<$count;$i++){
$mix[] = array(
'name' => isset($array1[$i]) ? $array1[$i] : '',
'profession' => isset($array2[$i]) ? $array2[$i] : '',
'SL' => isset($array3[$i]) ? $array3[$i] : '',
);
}
}

Related

merge 2 array with different number of keys

I have 2 arrays
$arr1 = Array
(
[REG1] => 94
[REG3] => 45
)
$arr2 =Array
(
[0] => REG1
[1] => REG2
[2] => REG3
[3] => REG4
)
I have to loop 2 arrays and I would like a result in one array like this:
Array(
[0] => Array(
[REG1] => 94
)
[1] => Array(
[REG2] =>
)
[2] => Array(
[REG3] => 45
)
[3] => Array(
[REG4] =>
)
)
But for the instand I can not do what I want, here is where I am:
private function mergeData($arr1, $arr2){
$result = array_map(function($v1, $v2){
$t[$v1] = $v2;
return $t;
}, $arr2, $arr1);
return $result;
}
output:
Array(
[0] => Array(
[REG1] => 94
)
[1] => Array(
[REG2] =>45
)
[2] => Array(
[REG3] =>
)
[3] => Array(
[REG4] =>
)
)
I cannot correctly put the bone values with the right keys.
I tried with array_merge_recursive but also failed.
Thanks for your help
$arr3 = array_fill_keys( $arr2, '' );
creates an array containing the values of $arr2 as keys with as value an empty string.
$arr3 =Array
(
[REG1] => ''
[REG2] => ''
[REG3] => ''
[REG4] => ''
)
After that just merge.
$result = array_merge ( $arr3, $arr1 );
Another option would be to use the array_map function and map key with the corresponding values, if any:
$arr1 = [
'REG1' => 94,
'REG3' => 45
];
$arr2 = [
'REG1',
'REG2',
'REG3',
'REG4'
];
$result = array_map(function($item) use($arr1){
return [$item => isset($arr1[$item]) ? $arr1[$item] : ""];
},$arr2);
This will return:
Array
(
[0] => Array
(
[REG1] => 94
)
[1] => Array
(
[REG2] =>
)
[2] => Array
(
[REG3] => 45
)
[3] => Array
(
[REG4] =>
)
)
Using a loop:
<?php
$arr1 = [
'REG1' => 94,
'REG3' => 45
];
$arr2 = [
'REG1',
'REG2',
'REG3',
'REG4'
];
$result = $arr2;
foreach($result as &$v)
$v = [$v => $arr1[$v] ?? null];
unset($v);
var_export($result);
Output:
array (
0 =>
array (
'REG1' => 94,
),
1 =>
array (
'REG2' => NULL,
),
2 =>
array (
'REG3' => 45,
),
3 =>
array (
'REG4' => NULL,
),
)

Merge two arrays, replacing values of same key

I have two arrays
First Array
(
[0] => Array
(
[352] => 1
[128] =>
[64] =>
[70] => 2
)
)
Second array is like this :
Array
(
[0] => Array
(
[128] => 1.5
)
)
I want to make final array like this.(i want to store the matching into the main array in this example it is 128 -> 1.5) how can i do it.?
Array
(
[0] => Array
(
[352] => 1
[128] => 1.5
[64] =>
[70] => 2
)
)
here is my array variables:
print_r($listskilssresult);
print_r($listskilssresultmatching);
You need to use array_replace_recursive
$listskilssresult = [
[
352 => 1,
128 => '',
64 => '',
70 => 2
]
];
$listskilssresultmatching = [
[
128 => 1.5
]
];
print_r(array_replace_recursive($listskilssresult, $listskilssresultmatching));
Prints :
Array
(
[0] => Array
(
[352] => 1
[128] => 1.5
[64] =>
[70] => 2
)
)
Know the difference between array_replace_recursive and array_merge_recursive here
This is specific to your question. If you want to make something more automated, you can create a function. But this will do what you want:
<?php
$array1 = [[352 => 1, 128 => null, 64 => null, 70 => 2]];
$array2 = [[128 => 1.5]];
$keys1 = array_keys($array1[0]);
$keys2 = array_keys($array2[0]);
foreach ($keys1 as $key => $value) {
if (in_array($value, $keys2)) {
$array1[0][$value] = $array2[0][$value];
unset($array2[0][$value]);
}
}
if (!empty($array2[0])) {
foreach ($array2[0] as $key => $value) {
$array1[0][$key] = $value;
unset($array2[0][$key]);
}
}
print_r($array1[0]);
?>
The last if statement will add key + value from the 2nd array to the first if no match was found for them (from the foreach statement). You can just delete that condition if you just want to add only matching keys.
For this solution, you have to use array_merge() built-in php function.
Syntax:
$finalArray = array_merge($array1, $array2);
print_r($finalArray)
Example:
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
Output:
Array
(
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)
Reference : http://php.net/manual/en/function.array-merge.php
array_replace_recursive is the best solution!
$old_array = Array('my_index1' => Array('1' => 'A', '2' => 'B'), 'my_index2' => Array('1' => 'C', '2' => 'D'));
$new_array = Array('my_index2' => Array('2' => 'Z'));
$result = array_replace_recursive($old_array, $new_array);
//Result : Array('my_index1' => Array('1' => 'A', '2' => 'B'), 'my_index2' => Array('1' => 'C', '2' => 'Z'));
array_merge would do the job for you:
array_merge($listskilssresult,$listskilssresultmatching)

How can I merge arrays with along an identifier?

I have two arrays like this:
$arr1 = ['yellow' => 1, 'red', => 5];
$arr2 = ['gray' => 3, 'black' => 8, 'orange' => 6, 'yellow' => 9];
And I want this result:
$merged = ['yellow' => [1, 'arr1'], 'red', => [5, 'arr1'], 'gray' => [3, 'arr2'], 'black' => [8, 'arr2'], 'orange' => [6, 'arr2'], 'yellow' => [9, 'arr2']];
But as I've read the documentation about array_merge(),it doesn't have such a option. Is there any function to do that for me?
This probably is what you are looking for:
<?php
$input = [
'arr1' => ['yellow' => 1, 'red' => 5],
'arr2' => ['yellow' => 10, 'gray' => 3, 'black' => 8, 'orange' => 6]
];
$output = [];
array_walk($input, function ($entry, $source) use (&$output) {
foreach ($entry as $key=>$val) {
$output[$key][] = [$val, $source];
}
});
print_r($output);
The output of above code obviously is:
Array
(
[yellow] => Array
(
[0] => Array
(
[0] => 1
[1] => arr1
)
[1] => Array
(
[0] => 10
[1] => arr2
)
)
[red] => Array
(
[0] => Array
(
[0] => 5
[1] => arr1
)
)
[gray] => Array
(
[0] => Array
(
[0] => 3
[1] => arr2
)
)
[black] => Array
(
[0] => Array
(
[0] => 8
[1] => arr2
)
)
[orange] => Array
(
[0] => Array
(
[0] => 6
[1] => arr2
)
)
)
Ok, it took me a bit longer to come up with this answer because you have a bug in your question. See the ',' behind 'red' in array 1.
$arr1 = ['yellow' => 1, 'red' => 5];
$arr2 = ['gray' => 3, 'black' => 8, 'yellow' => 3, 'orange' => 6];
$arrays = ['arr1','arr2'];
$merged = [];
foreach ($arrays as $array) {
foreach (${$array} as $color => $value) {
$merged[$color][] = [$value,$array];
}
}
echo '<pre>';
print_r($merged);
echo '</pre>';
To keep both values of 'yellow' I introduced an extra layer of arrays. I don't see how to keep them otherwise.
So the code was tested and returns:
Array
(
[yellow] => Array
(
[0] => Array
(
[0] => 1
[1] => arr1
)
[1] => Array
(
[0] => 3
[1] => arr2
)
)
[red] => Array
(
[0] => Array
(
[0] => 5
[1] => arr1
)
)
[gray] => Array
(
[0] => Array
(
[0] => 3
[1] => arr2
)
)
[black] => Array
(
[0] => Array
(
[0] => 8
[1] => arr2
)
)
[orange] => Array
(
[0] => Array
(
[0] => 6
[1] => arr2
)
)
)
Based on #arkascha answer a version that handle duplicates:
<?php
$input = [
'arr1'=> ['yellow' => 1,'red' => 5, 'black' => 4],
'arr2'=> ['gray' => 3,'yellow' => 3, 'black' => 8,'orange' => 6],
'arr3'=> ['orange' => 2]
];
$output = [];
array_walk($input, function ($entry, $source) use (&$output) {
foreach ($entry as $key=>$val) {
if(!array_key_exists($key, $output))
$output[$key] = [[$val, $source]];
else
$output[$key][] = [$val, $source];
}
});
echo print_r($output);
?>
PHP FIDDLE

PHP - Sort Array By SubArray Value By Maintaining original Array key

I would like to Sort an Array with its sub array value ("Name") but keeping its original array key.
default Array:
Array (
[251] => Array
(
[color] =>
[name] => 8
[nbr] => 1
[url_name] => taille-8
[meta_title] =>
)
[323] => Array
(
[color] =>
[name] => 7
[nbr] => 2
[url_name] => taille-7
[meta_title] =>
)
[127] => Array
(
[color] =>
[name] => 34
[nbr] => 2
[url_name] => taille-34
[meta_title] =>
)
);
By using array_multisort, I can able to get following Array:
Array(
[0] => Array
(
[color] =>
[name] => 7
[nbr] => 2
[url_name] => taille-7
[meta_title] =>
)
[1] => Array
(
[color] =>
[name] => 8
[nbr] => 1
[url_name] => taille-8
[meta_title] =>
)
[2] => Array
(
[color] =>
[name] => 34
[nbr] => 2
[url_name] => taille-34
[meta_title] =>
)
);
But what i need is,
Array(
[323] => Array
(
[color] =>
[name] => 7
[nbr] => 2
[url_name] => taille-7
[meta_title] =>
)
[251] => Array
(
[color] =>
[name] => 8
[nbr] => 1
[url_name] => taille-8
[meta_title] =>
)
[127] => Array
(
[color] =>
[name] => 34
[nbr] => 2
[url_name] => taille-34
[meta_title] =>
)
);
Thanks in adv :)
I would go with uasort, it looks much simpler to me:
// $arr is your Array
uasort($arr, function ($a, $b) {
return $a['name'] - $b['name'];
});
Here is an example: http://sandbox.onlinephpfunctions.com/code/a9f2d1e9702834b3a35206125429739222770301
$arr being your array:
//obtain list of values to sort by
foreach ($arr as $id => $value) {
$names[$id] = $value['name'];
}
$keys = array_keys($arr);
array_multisort(
$names, SORT_ASC, SORT_NUMERIC, $arr, $keys
);
$result = array_combine($keys, $arr);
You were probably missing the last step combining the array with given keys.
$arr assuming your array containing numeric keys and sort it by using array_multisort.
array_multisort will return sorted array.
array_combine will combine your original keys with sorted array.
Use:
$result = array_sort_by_column_preserve_keys($arr);
echo '<pre>';print_r($result);echo '</pre>';
Method :
function array_sort_by_column_preserve_keys($arr) {
$ar2 = [];
foreach($arr as $key => $sub) {
$ar2[ $key ] = $sub;
}
$keys = array_keys($arr);
array_multisort($ar2, SORT_ASC, SORT_NUMERIC, $arr, $keys);
return $result = array_combine($keys, $arr);
}
Please try this,
$array=array(
"251" => array(
"color" => "",
"name" => 8,
"nbr" => 1,
"url_name" => "taille-8",
"meta_title" => ""),
"323" => array(
"color" => "",
"name" => 7,
"nbr" => 2,
"url_name" => "taille-7",
"meta_title" => ""),
"127" => array(
"color" => "",
"name" => 34,
"nbr" => 2,
"url_name" => "taille-34",
"meta_title" => ""),
);
function swapArray( &$arr,$firstPos,$secondPos){
//echo PHP_EOL."swap: ".$firstPos.", ".$secondPos.PHP_EOL;
foreach($arr[$firstPos] as $k=>$v){
//echo PHP_EOL.$k." => ".$v.PHP_EOL;
$tmp=$v;
$arr[$firstPos][$k]=$arr[$secondPos][$k];
$arr[$secondPos][$k]=$tmp;
}
}
var_dump($array);
$keys=array(); // store all key values
$num=count($array);
foreach($array as $key=>$tmpArray)
$keys[]=$key;
//var_dump($keys);
for($i=0; $i<$num; $i++){
for($j=$i+1; $j<$num; $j++){
if($array[$keys[$i]]["name"]>$array[$keys[$j]]["name"]){
swapArray($array,$keys[$i],$keys[$j]);
}
}
}
var_dump($array);

How to match key value in two arrays in php [duplicate]

This question already has answers here:
Find intersecting rows between two 2d arrays comparing differently keyed columns
(3 answers)
Closed 4 months ago.
I have two arrays
Array
(
[0] => Array
(
[id] => 1
[affiliate_id] => 190
)
[1] => Array
(
[id] => 2
[affiliate_id] => 946
)
)
Array
(
[0] => Array
(
[id] => 1
[user_id] => 190
)
[1] => Array
(
[id] => 2
[user_id] => 246
)
[2] => Array
(
[id] => 3
[user_id] => 249
)
[3] => Array
(
[id] => 3
[user_id] => 250
)
)
Now i want to get an array which has value like this
if affiliate_id of first array exists in second array as user_id then i will get its value in third array like
Array
(
[0] => Array
(
[affiliate_id] => 190
)
)
i just want affiliate_id which is exists in second array as user_id
$a = Array(
Array('id' => 1, 'affiliate_id' => 190),
Array('id' => 2, 'affiliate_id' => 946)
);
$b = Array(
Array('id' => 1, 'user_id' => 190),
Array('id' => 2, 'user_id' => 246),
Array('id' => 3, 'user_id' => 249),
Array('id' => 3, 'user_id' => 250)
);
$c = array_map(function ($arr) { return $arr['affiliate_id']; }, $a);
$d = array_map(function ($arr) { return $arr['user_id']; }, $b);
$e = array_intersect($c, $d);
print_r($e);
try in_array() with loop
$a = firstarray;
$b = second array;
$i =0;
foreach($b as $k=>$v) {
if(!empty($a[$i])) {
if(in_array($v['user_id'], $a[$i])) {
$c[]['affiliate_id'] = $v['user_id'];
}
}
$i++;
}
print_r($c);
output :-
Array
(
[0] => Array
(
[affiliate_id] => 190
)
)
Use the following code:
<?php
$arr1 = array(array('id' => 1, 'affiliate_id' => 190),
array('id' => 2, 'affiliate_id' => 946));
$arr2 = array(array('id' => 1, 'user_id' => 190),
array('id' => 2, 'user_id' => 246),
array('id' => 3, 'user_id' => 249),
array('id' => 4, 'user_id' => 250));
$count = 0;
foreach ($arr1 as $k1 => $v1) {
if (in_array($v1['affiliate_id'], $arr2[$count]))
{
$arr3[]['affiliate_id'] = $v1['affiliate_id'];
}
$count++;
}
echo '<pre>'; print_r($arr3);
Output
Array
(
[0] => Array
(
[affiliate_id] => 190
)
)

Categories