Keep only value present in both Array PHP - php

I have two array, let's say
$array1 = array(0 => 10, 1 => 21, 2 => 34, 'somekey' => 45, 'otherkey' => 15);
$array2 = array(0 => 9, 1 => 10, 2 => 14, 'otherkey' => 15, 'somekey' => 43);
I need to return an array with only the values contained by both arrays, independent of their keys. In this case, the resulting array would contain value 10 at key 0, value 15 at key 1

Use array_intersect():
$array3 = array_intersect( $array1, $array2);
If you want to get rid of the keys, run that array through array_values():
$array3 = array_values( $array3);
This will set $array3 to:
Array
(
[0] => 10
[1] => 15
)

<?php
$array1 = array(0 => 10, 1 => 21, 2 => 34, 'somekey' => 45, 'otherkey' => 15);
$array2 = array(0 => 9, 1 => 10, 2 => 14, 'otherkey' => 15, 'somekey' => 43);
$array1 = array_values($array1);
$array2 = array_values($array2);
$array3 = array_merge($array1,$array2);
echo '<pre>';
print_r($array3);
echo '</pre>';
?>

Related

Associative array remove all values of 0

I have an Associative array in PHP and want to remove all values which have the associated value of 0
Array ( [item1] => 0 [item2] => 10 [item5] => 0 [item10] => 10 [item12] => 5 )
Thank you
Well, there are a lot of ways to achieve this, out of which two I have mentioned below:
Use array_filter.
Snippet:
<?php
$arr = [
'item1' => 0,
'item2' => 10,
'item5' => 0,
'item10' => 10,
'item12' => 5,
'item120' => false,
];
$filtered = array_filter($arr,function($value){
return $value !== 0;
});
print_r($filtered);
Demo: https://3v4l.org/fMsHt
Another way I would suggest is to use array_diff()
Snippet:
<?php
$arr = [
'item1' => 0,
'item2' => 10,
'item5' => 0,
'item10' => 10,
'item12' => 5,
'item120' => false,
];
print_r(array_diff($arr,[0]));
Demo: https://3v4l.org/3YHiX
you can do this simply using array_filter
$data = Array ( 'item1' => 0 ,'item2' => 10, 'item5' => 0, 'item10' => 10, 'item12' => 5 );
echo '<pre>';print_r(array_filter($data));

Transpose a multidimensional associative array into a multidimensional indexed array sorted against and an external associative key

I need to transpose a multidimensional associative array into a multidimensional indexed array sorted against and an external associative key. In the example below, I need a way to get from the 'input' to the 'expected output'.
I've tried array_match(), array_intersect() but I think I'm missing something. There must be an elegant solution to this but I cannot figure it out.
//Input
$array = array(
array('Volvo' => 22, 'BMW' => 13, 'Saab' => 5, 'Land Rover' => 11),
array('Nissan' => 10, 'Saab' => 4),
array('Land Rover' => 22, 'BMW' => 9, 'Nissan' => 2, 'Ford' => 17)
//...
);
//Expected output
$array_cars = array( // sorted list of unique car names
0 => 'BMW',
1 => 'Ford',
2 => 'Land Rover',
3 => 'Nissan',
4 => 'Saab',
5 => 'Volvo'
//...
);
$compiled_data = array( // 2D matrix, columns: $array, rows: $array_car
array(0 => 13, 2 => 9), // 'BMW'
array(2 => 17), // 'Ford'
array(0 => 11, 2 => 22), // 'Land Rover'
array(1 => 10, 2 => 2), // 'Nissan'
array(0 => 5, 1 => 4), // 'Saab'
array(1 => 22) // 'Volvo'
//...
);
Probably the simplest thing is to just iterate over all the values, sorting them into a car indexed array. You can then use ksort to sort the data:
$output = array();
foreach ($array as $key => $a) {
foreach ($a as $car => $v) {
$output[$car][$key] = $v;
}
}
ksort($output);
$array_cars = array_keys($output);
$compiled_data = array_values($output);
var_export($array_cars);
var_export($compiled_data);
Output:
array (
0 => 'BMW',
1 => 'Ford',
2 => 'Land Rover',
3 => 'Nissan',
4 => 'Saab',
5 => 'Volvo',
)
array (
0 =>
array (
0 => 13,
2 => 9,
),
1 =>
array (
2 => 17,
),
2 =>
array (
0 => 11,
2 => 22,
),
3 =>
array (
1 => 10,
2 => 2,
),
4 =>
array (
0 => 5,
1 => 4,
),
5 =>
array (
0 => 22,
),
)
Demo on 3v4l.org

change inner key in multidimensional array php

I want to ask about how to replace inner key in a multidimensional array.
I have an multidimensional array :
$array1=
array(array(5000, 6, 325, 3, 3, 517000000),
array( 20000, 5, 217, 5, 3, 1692000000)
);
The second array is
$array2=array(1,2,3,4,5,6);
I expected the new array is
Array(
[0] => Array
(
[1] => 5000
[2] => 6
[3] => 325
[4] => 3
[5] => 3
[6] => 517000000
)
[1] => Array
(
[1] => 20000
[2] => 5
[3] => 217
[4] => 5
[5] => 3
[6] => 1692000000
))
I have tried this code below by another post PHP Replace multidimensional array keys, but I can't assign the value of my array1
foreach($array2 as $array2 ){
for($k=0;$k<sizeof($array2);$k++){
for($l=0;$l<$count;$l++){
$last[$l][$array2] = $array1[$k][$l];
}
$i += $count;
}
}
Thank you
As #Rizier has suggested in his comment you can do this using array_map() and array_combine().
<?php
$array1=
array(array(5000, 6, 325, 3, 3, 517000000),
array( 20000, 5, 217, 5, 3, 1692000000)
);
$array2 = array(1, 2, 3, 4, 5, 6);
foreach($array1 as $arr1){
$array3[] = array_combine($array2, $arr1);
}
var_dump($array3);
output
array (size=2)
0 =>
array (size=6)
1 => int 5000
2 => int 6
3 => int 325
4 => int 3
5 => int 3
6 => int 517000000
1 =>
array (size=6)
1 => int 20000
2 => int 5
3 => int 217
4 => int 5
5 => int 3
6 => int 1692000000
try
<?php
$array1=
array(array(5000, 6, 325, 3, 3, 517000000),
array( 20000, 5, 217, 5, 3, 1692000000)
);
$newArray = array();
foreach($array1 as $arr){
array_unshift($arr,'');
unset($arr[0]);
$newArray[] = $arr;
}
print_r($newArray);
this will have same output you require.
hope it helps :)

Php not equal array combine

I can't understand how to combine these arrays.
$data = array("a", "b", "c")
$array = array(0 => Array(1 , 2, 3), 1 => Array(4, 5, 6))
I tried different functions such as merge, combine, map..
Result has to be:
array(
'a' => array(1, 4),
'b' => array(2, 5),
'c' => array(3, 6),
)
This should work for you:
<?php
$data = array("a", "b", "c");
$array = array(array(1 , 2, 3), array(4, 5, 6));
$result = array();
foreach($data as $key => $value) {
foreach($array as $innerKey => $innerValue)
$result[$value][] = $innerValue[$key];
}
print_r($result);
?>
Output:
Array (
[a] => Array ( [0] => 1 [1] => 4 )
[b] => Array ( [0] => 2 [1] => 5 )
[c] => Array ( [0] => 3 [1] => 6 )
)
A solution that uses the function array_column() available since PHP 5.5:
$data = array("a", "b", "c");
$array = array(0 => Array(1 , 2, 3), 1 => Array(4, 5, 6));
$result = array();
foreach($data as $i => $v) {
$result[$v] = array_column($array, $i);
}
If you are stuck with a previous version then use Rizier123's solution (it does the same thing with just a little more code.

How to use array_diff to get the missing item

I'm having trouble using array_diff correctly.
I've got 2 arrays:
$arr_1 = array(
0 => array('name' => 'Day Rate 2', 'from' => 1200, 'to' => 1400),
1 => array('name' => 'Day Rate 2', 'from' => 2000, 'to' => 2000),
);
$arr_2 = array(
0 => array('name' => 'Day Rate 2', 'from' => 0, 'to' => 1000),
1 => array('name' => 'Day Rate 2', 'from' => 1200, 'to' => 1400),
2 => array('name' => 'Day Rate 3', 'from' => 2000, 'to' => 4000),
);
I want to get the values in $arr_2 that are not present in $arr_1. I want it to return this:
0 => array('name' => 'Day Rate 2', 'from' => 0, 'to' => 1000)
To compare them, I first serialized the values of each item and created these two serialized arrays, which I can use to compare, using array_diff.
foreach ($arr_1 as $key => $val) {
$arr_1_simple[$key] = serialize(array($val['from'], $val['to']));
}
foreach ($arr_2 as $key => $val) {
$arr_2_simple[$key] = serialize(array($val['from'], $val['to']));
}
Array
(
[0] => a:2:{i:0;i:1200;i:1;i:1400;}
[1] => a:2:{i:0;i:2000;i:1;i:2000;}
)
Array
(
[0] => a:2:{i:0;i:0;i:1;i:1000;}
[1] => a:2:{i:0;i:1200;i:1;i:1400;}
[2] => a:2:{i:0;i:2000;i:1;i:4000;}
)
Since a:2:{i:0;i:1200;i:1;i:1400;} and a:2:{i:0;i:2000;i:1;i:4000;} are found in both $arr_1 and $arr_2,the odd one out is a:2:{i:0;i:0;i:1;i:1000;}, which is what I thought array_diff would return.
However, the result that I'm getting is:
print_r(array_diff($arr_2_simple, $arr_1_simple));
Array
(
[0] => a:2:{i:0;i:0;i:1;i:1000;}
[2] => a:2:{i:0;i:2000;i:1;i:4000;}
)
Can anyone tell me why a:2:{i:0;i:2000;i:1;i:4000;} is getting returned? I want all the items in $arr_2 that are not in $arr_1. How do I get this?
Your value in array 1
[1] => a:2:{i:0;i:2000;i:1;i:2000;}
does not match the value in array 2
[2] => a:2:{i:0;i:2000;i:1;i:4000;}
array_diff($arr1, $arr2). Here $arr1 is array to compare from and $arr2 is array to compare against. The function returns an array containing all the entries from $arr1 that are not present $arr2. Example
Case: 1 array_diff($arr1, $arr2)
$arr1 = [1, 2, 3];
$arr2 = [1, 3, 4];
print_r(array_diff($arr1, $arr2)); //Output: [2]
Case: 2 array_diff($arr2, $arr1)
$arr1 = [1, 2, 3];
$arr2 = [1, 3, 4];
print_r(array_diff($arr2, $arr1)); //output: [4]

Categories