I have two arrays with different length, but keys are similar.
My requirement is update $array1 with values of $array2 similar keys
$array1 = array("Jan"=>"0", "Feb"=>"0", "Mar"=>"0", "Apr"=>"0");
$array2 = array("Jan"=>"2", "Mar"=>"3");
Output:
$res = array("Jan"=>"2","Feb"=>"0","Mar"=>"3","Apr"=>"0");
You can achieve it by this code:
$array1 = array("Jan" => "0", "Feb" => "0", "Mar" => "0", "Apr" => "0");
$array2 = array("Jan" => "2", "Mar" => "3");
$array3 = array_replace($array1, $array2);
print_r($array3);
You can simply use + operator.
$array1 = array("Jan"=>"0","Feb"=>"0","Mar"=>"0","Apr"=>"0");
$array2 = array("Jan"=>"2", "Mar"=>"3");
print_r($array2 + $array1);
DEMO
Try this:
array_merge($array1, $array2);
Related
I want to merge two arrays with the same value -> user_id to one array.
You can see I have two arrays with different objects. I want merge them by user_id to one array with all objects.
For example:
$array1 = [
$array[0]->leads = 5643;
$array[0]->user_id= 15;
$array[0]->sales = 1433;
$array[1]->leads = 3264;
$array[1]->user_id= 9;
$array[1]->sales = 1254;
];
$array2 = [
$array[0]->user_id= 15;
$array[0]->processing = 2300;
$array[0]->deleted = 203;
$array[1]->user_id= 9;
$array[1]->processing = 103;
$array[1]->deleted = 80;
];
The following array is the target.
$result = [
$array[0]->user_id= 15;
$array[0]->processing = 2300;
$array[0]->leads = 5643;
$array[0]->deleted = 203;
$array[0]->sales = 1433;
$array[1]->user_id= 9;
$array[1]->processing = 103;
$array[1]->leads = 3264;
$array[1]->deleted = 80;
$array[1]->sales = 1254;
];
Iterate your array and build the combined array of objects using user_id as the key.
foreach ($array as $entry) {
// if an entry for this user id hasn't been created in the result, add this object
if (!isset($result[$entry->user_id])) {
$result[$entry->user_id] = $entry;
// otherwise, iterate this object and add the values of its keys to the existing entry
} else {
foreach ($entry as $key => $value) {
$result[$entry->user_id]->$key = $value;
}
}
}
Given the additional info you just gave (two separate arrays) the solution is basically the same, just merge the two arrays together first.
foreach (array_merge($array1, $array2) as $entry) { ...
(Working example at https://3v4l.org/ccdQc)
How about array_merge,
<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
or array_merge_recursive,
<?php
$ar1 = array("color" => array("favorite" => "red"), 5);
$ar2 = array(10, "color" => array("favorite" => "green", "blue"));
$result = array_merge_recursive($ar1, $ar2);
print_r($result);
?>
Example
$array = [
1 => [
'cost' => '25'
],
2 => [
'blah' => 'test'
]
];
$new_array = array_merge_recursive($array[1], $array[2]);
var_dump($new_array);
Output
array(2) {
["cost"]=>
string(2) "25"
["blah"]=>
string(4) "test"
}
Live Example
Repl
<?php
$array1= array("leads"=> 5643,"user_id"=>15,"sales" =>1433);
$array2= array("user_id"=> 15,"processing" => 2300,"deleted" => 203);
$array= (array_merge($array1,$array2));
print_r($array);
?>
I want to go from these arrays:
$array1 = ["x", "y", "z"];
$array2 = ["a","b"];
$array3 = ["1","2","3","4","5","6"];
To this array:
$arrayResult =
array(
array("x" => array("a" => array(1,2,3,4,5,6),
"b" => array(1,2,3,4,5,6)),
"y" => array("a" => array(1,2,3,4,5,6),
"b" => array(1,2,3,4,5,6)),
"z" => array("a" => array(1,2,3,4,5,6),
"b" => array(1,2,3,4,5,6)))
);
I tried to make this combined array with cartesian product approaches, but no satisfying result so far.
Here is another solution without using any loop:
$array1 = ["x", "y", "z"];
$array2 = ["a","b"];
$array3 = ["1","2","3","4","5","6"];
$result = array_combine(
$array1,
array_fill(
0,
count($array1),
array_combine(
$array2,
array_fill(0, count($array2), $array3)
)
)
);
print_r($result);
Here is the demo
use array_fill_keys twice to get the result
$result = array_fill_keys(
$array1,
array_fill_keys($array2, $array3)
);
Demo on eval.in
So I have something like:
$array = array("red", "yellow", "hello", "world");
$array2 = array("1", "2", "3", "4");
$myArray = array();
array_push($myArray, $array, $array2 );
$myArray = array_slice($myArray, 0, 2);
and I want $myArray to be ["red", "yellow"], and if $array was empty $myArray would be ["1", "2"]
Does that make any sense? Right now array_slice is counting the arrays being pushed into $myArray, not the content inside them. How would I go about doing this?
Where you use array_push you probably want to use array_merge:
$array = array("red", "yellow", "hello", "world");
$array2 = array("1", "2", "3", "4");
$myArray = array_merge($array, $array2);
$myArray = array_slice($myArray, 0, 2);
Explanation:
array_push pushes the elements to the end of the existing array, so
$array = array('a');
array_push($array, 'b');
// results in $array = array('a', 'b');
Thus in your code, just after the array_push-call
$myArray = array(array('red', 'yellow', ...), array('1', '2', ...))
array_merge merges two or more arrays
$myArray = array_merge($array, $array2);
// results in $myArray = array('red', 'yellow', ..., '1', '2', ...)
Try using array_merge() instead of array_push()
$array = array("red", "yellow", "hello", "world");
$array2 = array("1", "2", "3", "4");
$myArray = array_merge($array, $array2 );
$myArray = array_slice($myArray, 0, 2);
array_push() is adding each array as an element of $myArray instead of combining them with it.
Is there any function similar to "array_intersect" but it is in mode case-insensitive and ignoring tildes?
The array_intersect PHP function compares array elements with === so I do not get the expected result.
For example, I want this code :
$array1 = array("a" => "gréen", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result);
Outputs gréen and red. In default array_intersect function just red is proposed (normal cause ===).
Any solution ?
Thank you in advance
$result = array_intersect(array_map('strtolower', $array1), array_map('strtolower', $array2));
<?php
function to_lower_and_without_tildes($str,$encoding="UTF-8") {
$str = preg_replace('/&([^;])[^;]*;/',"$1",htmlentities(mb_strtolower($str,$encoding),null,$encoding));
return $str;
}
function compare_function($a,$b) {
return strcmp(to_lower_and_without_tildes($a), to_lower_and_without_tildes($b));
}
$array1 = array("a" => "gréen", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_uintersect($array1, $array2,"compare_function");
print_r($result);
output:
Array
(
[a] => gréen
[0] => red
)
Assume that I have two arrays as follow:
$array1 = array(1, 3, 5);
$array2 = array('x'=> 1, 'y'=> 2, 'z'=> 5);
How to check that the two arrays are exactly the same in most efficient and proper way and it doesn't care the keynames of the *$array2.
I want to make a function which should return true if values are exactly the same, and false if any of the ones are different both in value(s) and number of elements.
Thanks for your time and reading.
In the simplest case you can just use array_diff. It ignores the keys in your second array, but also the order of the values. It would return an empty set if the arrays are equal:
if (count(array_diff($array1, $array2)) == 0) {
// equal
You could also compare the arrays directly, after stripping keys from the second:
if ($array1 == array_values($array2)) {
That would additionally compare the order of contained values.
array_values($array1) === array_values($array2)
Assuming that arrays have same order.
Try this
$array1 = array(1, 3, 5);
$array2 = array('x'=> 1, 'y'=> 2, 'z'=> 5);
$array2 = array_values($array2);
echo $array1 == $array2 ? 'true' : 'false';
array_diff will do the job for you:
<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
if(empty($result)){
// arrays contain the same values!
}
?>
Create a class containing an array and make that class implement the Comparable interface, for example http://php.net/manual/language.oop5.interfaces.php#69467
like this:
<?php
$array1 = array ("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array ("a" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
if(count($result) == 0)
{
.......
}
?>