I have two array:
$arr1 = array(
'attributes' => array(
'fruit' => 'banana',
),
);
$arr2 = array(
'attributes' => array(
'color' => 'red',
),
);
$result = array_merge($arr1, $arr2);
The result is:
Array ( [attributes] => Array ( [color] => red ) )
But my expected result:
Array ( [attributes] => Array ( [color] => red [fruit] => banana ) )
What I am doing wrong? Should I use array_merge
or maybe will be better and easier just to use array_push and use only ('color' => 'red') ?
array_merge_recursive() is a great fit here.
$resultArray = array_merge_recursive($arr1, $arr2);
try this:
$result = array('attributes' => array_merge($arr1['attributes'], $arr2['attributes']));
print_r($result);
Related
I want to covert below array into single array.
This is the my array code as below:
Array
(
[0] => Array
(
[label] =>
[value] =>
)
[1] => Array
(
[value] => 4
[label] => 0.5
)
[2] => Array
(
[value] => 5
[label] => 0.6
)
)
Want below result:
Array(
'4' => '0.5',
'5' => '0.6',
);
Try this:
$single_array = array();
foreach($array as $keys => $values){
$single_array[$values['label']] = $values['value'];
}
Use can use filter like not empty or null as you want.
Try this:
$array = array(
array('value' => '', 'label' => ''),
array('value' => 4, 'label' => 0.5),
array('value' => 5, 'label' => 0.6)
);
$new_array = array();
foreach($array as $item_array){
if(!empty($item_array['value'])){
$new_array[$item_array['value']] = $item_array['label'];
}
}
print_r($new_array);
You can make use of following functions :
array_column() — Return the values from a single column in the input array
array_combine() — Creates an array by using one array for keys and another for its values
array_filter() — Filters elements of an array using a callback function, If no callback is supplied, all entries of array equal to FALSE (see converting to boolean) will be removed.
Script
akshay#db-3325:/tmp$ cat test.php
<?php
$array = array(
array('value' => '', 'label' => ''),
array('value' => 4, 'label' => 0.5),
array('value' => 5, 'label' => 0.6)
);
/* make use of array_column and array_combine */
print_r( array_combine( array_column($array, 'value'), array_column($array, 'label') ) );
/* to remove empty elements use array_filter */
print_r( array_filter( array_combine( array_column($array, 'value'), array_column($array, 'label') ) ) );
?>
Output
akshay#db-3325:/tmp$ php test.php
Array
(
[] =>
[4] => 0.5
[5] => 0.6
)
Array
(
[4] => 0.5
[5] => 0.6
)
Please help me to understand how can I succeed to filter a multidimensional with a help of other array value as keys for the first array.
$multidimensional = Array (
[0] => Array('var1' => val1),
[1] => Array('var2' => val2),
[2] => Array('var3' => val3),
[3] => Array('val4' => val4)
);
$filter = Array(1, 3);
The final result should be:
$multidimensional = Array (
[1] => Array('var2' => val2),
[3] => Array('val4' => val4)
);
It should be something similar as array_slice or other method how to easily perform such task. Thank you in advance!
You can use the array_intersect_key function:
$result = array_intersect_key($multidimensional, array_flip($filter));
To expand upon my comment with a small example
<?php
$arrayOne = [
1 => ['foo' => 'bar'],
2 => ['foo' => 'bar'],
3 => ['foo' => 'bar'],
4 => ['foo' => 'bar'],
];
$arrayTwo = [1 => [], 3 => []];
print_r(array_intersect_key($arrayOne, $arrayTwo));
see array_intersect_key on php.net
Another variation using array_diff_key and array_flip functions:
$multidimensional = array_diff_key($multidimensional, array_diff_key($multidimensional, array_flip($filter)));
print_r($multidimensional);
The output:
Array
(
[1] => Array
(
[var2] => val2
)
[3] => Array
(
[val4] => val4
)
)
http://php.net/manual/en/function.array-diff-key.php
Suppose i have 2 arrays:
$array1 =( [0] => Array ( [user] => 'aaa' [count] => '123' )
[1] => Array ( [user] => 'bbb' [count] => '456' )
[2] => Array ( [user] => 'ccc' [count] => '789' ) );
$array2= ( [0] => aaa)
[1] => ccc );
I would like to search values from second array in first array and create the third array that will contain all elements from first array, like this:
$array3 =( [0] => Array ( [user] => 'aaa' [count] => '123' )
[1] => Array ( [user] => 'ccc' [count] => '789' ) );
Please help. Thank you in advance
(sorry for bad english)
$array1 = array(
array( 'user' => 'aaa', 'count' => 123 ),
array( 'user' => 'bbb', 'count' => 456 ),
array( 'user' => 'ccc', 'count' => 789 ),
);
$array2 = array('aaa', 'ccc');
var_dump(array_filter($array1, function($el) use($array2) {
return in_array($el['user'], $array2);
}));
It preserves keys, you can reset them if needed. Or with foreach loop
$out = array();
foreach($array1 as $el)
{
if (in_array($el['user'], $array2))
$out[] = $el;
}
var_dump($out);
Can try using foreach(). Example here...
$array3 = array();
foreach($array2 as $search){
foreach($array1 as $val){
if($search == $val['user']){
$array3[] = $val;
}
}
}
print_r($array3);
I have the following 2 arrays that I'm trying to merge recursively so that I don't lose data, but I also don't want any data repeated.
$a = array(
'group1' => array(
'names' => array(
'g1name1',
'g1name2'
)
),
'group2' => array(
'names' => array(
'g2name1'
)
)
);
$b = array(
'group1' => array(
'names' => array(
'g1name1',
'g1name3'
),
'extras' => array(
'g1extra1'
)
),
'group3' => array(
'names' => array(
'g3name1'
)
)
);
I'm using array_merge_recursive($a, $b); which works fine for group2 and group3 because they exist in either $a or $b, but group1 is giving me a problem because it has some duplicate data in both $a and $b. This is what I'm getting after I merge recursively. Notice that in names, g1name is listed twice, once from $a and once from $b.
'group1' =>
array
'names' =>
array
0 => string 'g1name1'
1 => string 'g1name2'
2 => string 'g1name1'
3 => string 'g1name3'
'extras' =>
array
0 => string 'g1extra1'
1 => string 'g1extra2'
'group2' => ....
'group3' => ....
I tried array_unique like this array_unique(array_merge_recursive($a, $b)) but it's giving me strange results (doesn't fix the repeat problem and deletes group2 and group3 entirely).
Use array_replace_recursive instead of array_merge_recursive.
The the following should work for you:
array_walk($arr, function(&$data, $key) {
foreach ($data as &$arr) {
$arr = array_values(array_unique($arr));
}
});
Result:
Array
(
[group1] => Array
(
[names] => Array
(
[0] => g1name1
[1] => g1name2
[2] => g1name3
)
[extras] => Array
(
[0] => g1extra1
)
)
[group2] => Array
(
[names] => Array
(
[0] => g2name1
)
)
[group3] => Array
(
[names] => Array
(
[0] => g3name1
)
)
)
Sadly, array_replace_recursive (PHP 5.3.0)Docs does not work (Demo).
Luckily Tim Cooper pointed that out and made some suggestion. I was not very creative with my new suggestion, so it's highly influenced by his solution:
$result = array_merge_recursive($a, $b);
array_walk($result, function(&$v) {
$v = array_map('array_unique', $v);
});
New Demo
How do I write PHP code to manually create this array?
Array (
[0] => Array
(
[Id] => 2
[Fruit] => Apple
)
[1] => Array
(
[Id] => 5
[Fruit] => Orange
)
)
$arr = array();
$arr[] = array("Id" => 2, "Fruit" => "Apple");
$arr[] = array("Id" => 5, "Fruit" => "Orange");
$array = array();
$array[] = array(
'id'=>3,
'Fruit'=>'Orange'
);
$array[] = array(
'id'=>7,
'Fruit'=>'Banana'
);
$array =array(
array('Id'=>2, 'Fruit'=>'Apple'),
array('Id'=>5, 'Fruit'=>'Orange')
);
NB: array keys are case-sensitive.