Get values from second array where keys match - php

I have two arrays, the following:
$arr1 = array("Key1"=>1, "Key2"=>2, "Key3"=>3);
My second array is the following:
$arr2 = array("Key2", "Key3");
What I would like to get is the values where Key2 and Key3 matches. I would also like those values to be returned as an array so I end up with the following:
array(2,3)
Thanks for any help.

Just use of 3 three array functions to achieve this.
$arr1 = array("Key1"=>1, "Key2"=>2, "Key3"=>3);
$arr2 = array("Key2", "Key3");
$arr3 = array_values(array_intersect_key($arr1, array_flip($arr2)));
print_r($arr3);
The output:
Array ( [0] => 2 [1] => 3 )

$arr1 = array("Key1"=>1, "Key2"=>2, "Key3"=>3);
$arr2 = array("Key2", "Key3");
$result = array();
foreach($arr1 as $key => $value) {
if(in_array($key, $arr2)) {
array_push($result, $arr1[$key]);
}
}
var_dump($result);
or as mentioned in the comments:
$arr1 = array("Key1"=>1, "Key2"=>2, "Key3"=>3);
$arr2 = array("Key2", "Key3");
$result = array_intersect_key($arr1, array_flip($arr2));
var_dump($result);

Related

How to return the array when it's similar

I has two array look like this.
array1 = [ array("A"=> array("0"=>"1",
"1"=>"2",
"2"=>"3"),
"B"=>"1"),
array("A"=> array("0"=>"1",
"1"=>"2"),
"B"=>"2")
];
$array2 = array("A"=> array("0"=>"1",
"1"=>"2"),
"B"=>"2",
"C"=>"POP",
"D"=>null);
Now i try to compare $array1 and $array2.
you will see $array2 is same with $array1 in "A" and "B"
This is my result i hope it's to be
$result = array("A"=> array("0"=>"1",
"1"=>"2"),
"B"=>"2");
And last i use php(laravel)
I try to use
array_intersect_assoc($array1, $array2);
but it gave a nothing
Why don't you define your own array_intersect_assoc as this:
function array_intersect_assoc_with_arrays($arr1, $arr2) {
$ret = [];
$commonkeys = array_intersect(array_keys($arr1), array_keys($arr2)); // get all the keys that appear in both array
foreach ($commonkeys as $key) {
if (json_encode($arr1[$key]) == json_encode($arr2[$key])) //convert inner array to string so we can compare them
$ret[$key] = $arr1[$key]; // if equal - set them in the response
}
return $ret;
}
Now you can use it with:
$array1 = [array("A"=> array("0"=>"1", "1"=>"2", "2"=>"3"), "B"=>"1"), array("A"=> array("0"=>"1", "1"=>"2"), "B"=>"2")];
$array2 = array("A"=> array("0"=>"1", "1"=>"2"), "B"=>"2", "C"=>"POP", "D"=>null);
$res= [];
foreach($array1 as $arr) { // for each sub-array get all the familiar fields
$res = array_merge(array_intersect_assoc_with_arrays($arr, $array2), $res);
}
Which will generate the following output:
Array
(
[A] => Array
(
[0] => 1
[1] => 2
)
[B] => 2
)
Hope that helps!

php array_push and array_intersect

I want to add value to array and then I want to use these arrays in array intersect. Codes are in bellow. Where am I doing mistake?
$array =['1,2,3,4','3,4,5','2,3'];
$arr2 = [];
$common = [];
for($i=0; $i<count($array); $i++)
{
$arr1 = [];
if($i==0)
{
array_push($arr1, $array[$i]);
array_push($arr2, $array[$i]);
$common = array_intersect($arr1,$arr2);
}
else
{
array_push($arr1, $array[$i]);
$common = array_intersect($arr1,$common);
}
print_r($common);
}
Output is :
Array (
[0] => 1,2,3,4
)
Array ( )
Array ( )
I want to be this :
Array (
[0] => 1,2,3,4
)
Array(
[0] => 3,4
)
Array(
[0] => 3
)
Thanks,
Try This
<?php
$array =['1,2,3,4','3,4,5','2,3'];
$arr1 = [];
for($i=0; $i<count($array); $i++)
{
$j='arr'.$i;
$j= [];
if($i==0){
array_push($j, $array[$i]);
}
else{
$a = explode(',',$array[$i-1]);
$b = explode(',',$array[$i]);
$c = array_intersect($a,$b);
$d= implode(',',$c);
array_push($j, $d);
}
echo "<pre>"; print_r($j);
}
You are misusing array_intersect. This method does works on values in an array not on a single value. To use it the way You want You should split your values by comma and insert them as separate values. For example:
value: '1,2,3,4' should be inserted as:
$array = ['1', '2', '3', '4'];
Solution (without loops etc):
<?php
$array =['1,2,3,4','3,4,5','2,3'];
$arr1 = array();
$arr2 = array();
$common = array();
$arr1 = explode(',', $array[0]);
$arr2 = explode(',', $array[1]);
$common =array_intersect($arr1, $arr2);
print_r($common);
$arr3 = explode(',', $array[2]);
$common2 = array_intersect($common, $arr3);
print_r($common2);
?>

How to add 2 arrays and sort it

How can I turn:
$array1 = array(34=>"key1",54=>"key3",12=>"key2");
$array2 = array(44=>"key4",12=>"key2",1=>"key1");
into:
$array = ("key3"=>54,"key4"=>44,"key1"=>35,"key2"=>24);
How to add the value of the keys and sort by value?
You could flip the arrays, merge and sum matching key values, followed by a reverse sort:
$a1 = array(34=>"key1",54=>"key3",12=>"key2");
$a2 = array(44=>"key4",12=>"key2",1=>"key1");
function addRev($a1,$a2) {
$a1 = array_flip($a1);
$a2 = array_flip($a2);
$added = array();
foreach (array_keys($a1 + $a2) as $key) {
$added[$key] = #($a1[$key] + $a2[$key]);
}
arsort($added);
return $added;
}
print_r(addRev($a1, $a2));
Result:
Array
(
[key3] => 54
[key4] => 44
[key1] => 35
[key2] => 24
)
try this, I add explanations in comments
$array1 = array(34=>"key1",54=>"key3",12=>"key2");
$array2 = array(44=>"key4",12=>"key2",1=>"key1");
// fliping arrays
$a1r = array_flip($array1);
$a2r = array_flip($array2);
var_dump($a1r);
var_dump($a2r);
// adding the 2 arrays, final result in $a1r
foreach ($a2r as $key => $value) {
if (!isset($a1r[$key])) {
$a1r[$key] = 0;
}
$a1r[$key] += $value;
}
var_dump($a1r);
You can use array_merge() function for "adding" arrays.
http://php.net/manual/en/function.array-merge.php
For sorting, you can check this link:
Sorting an associative array in PHP

Merge multiple arrays from one array

How to merge multiple arrays from a single array variable ? lets say i have this in one array variable
Those are in one variable ..
$array = array(array(1), array(2));
Array
(
[0] => 1
)
Array
(
[0] => 2
)
how to end up with this
Array
(
[0] => 1
[1] => 2
)
This is the PHP equivalent of javascript Function#apply (generate an argument list from an array):
$result = call_user_func_array("array_merge", $input);
demo: http://3v4l.org/nKfjp
Since PHP 5.6 you can use variadics and argument unpacking.
$result = array_merge(...$input);
It's up to 3x times faster than call_user_func_array.
This may work:
$array1 = array("item1" => "orange", "item2" => "apple", "item3" => "grape");
$array2 = array("key1" => "peach", "key2" => "apple", "key3" => "plumb");
$array3 = array("val1" => "lemon");
$newArray = array_merge($array1, $array2, $array3);
foreach ($newArray as $key => $value) {
echo "$key - <strong>$value</strong> <br />";
}
array_merge can do the job
$array_meged = array_merge($a, $b);
after the comment
If fixed indexs you can use:
$array_meged = array_merge($a[0], $a[1]);
A more generic solution:
$array_meged=array();
foreach($a as $child){
$array_meged += $child;
}
$resultArray = array_merge ($array1, $array1);
$result = array();
foreach ($array1 as $subarray) {
$result = array_merge($result, $subarray);
}
// Here it is done
Something good to read:
http://ca2.php.net/manual/en/function.array-merge.php
Recursive:
http://ca2.php.net/manual/en/function.array-merge-recursive.php
$arr1 = array(0=>1);
$arr2 = array(0=>2);
$merged = array_merge($arr1,$arr2);
print_r($merged);
array_merge is what you need.
$arr = array_merge($arr1, $arr2);
Edit:
$arr = array_merge($arr1[0], $arr1[1]);

PHP convert one dimensional array into multidimensional

I have one array as
$tmpArr = array('A', 'B', 'C');
I want to process this array and want new array as
$tmpArr[A][B][C] = C
I.e last element becomes the value of final array.
Can anyone suggest the solution? Please help. Thanks in advance
Iterate the array of keys and use a reference for the end of the chain:
$arr = array();
$ref = &$arr;
foreach ($tmpArr as $key) {
$ref[$key] = array();
$ref = &$ref[$key];
}
$ref = $key;
$tmpArr = $arr;
$tmpArr = array('A', 'B', 'C');
$array = array();
foreach (array_reverse($tmpArr) as $arr)
$array = array($arr => $array);
Output:
Array
(
[A] => Array
(
[B] => Array
(
[C] => Array
(
)
)
)
)
$tmpArr[$tmpArr[0]][$tmpArr[1]][$tmpArr[2]] = $tmpArr[2];
Is that what you want?

Categories