i have a function that i got from stack-overflow:-
function testFunction($a, $b) {
$map = array();
foreach($a as $name => $value){
if(!isset($b[$name]) || $b[$name] != $value) {
$map[$value] = 1;
}
}
return array_keys($map);
}
$array1 = array("Peter"=>"35", "Ben"=>"21", "Joe"=>"43");
$array2 = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"57");
print_r(testFunction($array2, $array1));
here is the result i get:
Array ( [0] => 37 [1] => 57 )
because the 37 in second array is different than 21 in first array... and the 57 is different than the 43.
my question is: how can i add a key to the values? for example, I want this:
Array ("Ben"=>"37", "Joe"=>"57");
You can do it like below:-
<?php
function testFunction($a, $b) {
$map = array();
foreach($a as $name => $value){
if(!isset($b[$name]) || $b[$name] != $value) {
$map[$name] = $value; // make key value array
}
}
return $map;
}
$array1 = array("Peter"=>"35", "Ben"=>"21", "Joe"=>"43");
$array2 = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"57");
print_r(testFunction($array2, $array1));
Output:- https://eval.in/735195
Short and optimized solution using array_intersect_key and array_diff functions:
$array1 = ["Peter"=>"35", "Ben"=>"21", "Joe"=>"43"];
$array2 = ["Peter"=>"35", "Ben"=>"37", "Joe"=>"57", 'I'=>0];
// getting all $array2 items with coincident keys
$common_key_items = array_intersect_key($array2, $array1);
// getting value difference between arrays with same keys
$result = array_diff($common_key_items, $array1);
print_r($result);
The output:
Array
(
[Ben] => 37
[Joe] => 57
)
Related
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!
I have two arrays:
Array
(
[1] = 1575255
[2] = 1575258
)
Array
(
[Aantal kleuren opdruk] = Array
(
[1575252] = 1 kleur
[1575253] = 2 kleuren
[1575254] = 3 kleuren
[1575255] = 4 kleuren
)
[Opdrukpositie] = Array
(
[1575256] = Borst
[1575258] = Borst en rug
[1575257] = Rug
)
)
How can I compare the value of array 2 with the value of array 1 in their current order?
using array_column :
$arr = [];
foreach ($arr1 as $key => $value) {
$arr[] = array_column($arr2 ,$value);
}
print_r($arr);
assuming that the first array is $arr1 and the second is $arr2
EDIT
the one line solution using array_column and array_map
$arr = array_map(function($value) use ($arr2) {return array_column($arr2, $value);} ,$arr1);
print_r($arr);
OK so the first array has this. Take it as $a:
$a = ["1575255", "1575258"];
$second = ["Aantal kleuren opdruk" => [
"1575252" => "1 kleur",
"1575253" => "2 kleuren",
"1575254" => "3 kleuren",
"1575255" => "4 kleuren"
],
"Opdrukpositie" => [
"1575256" => "Borst",
"1575258" => "Borst en rug",
"1575257" => "Rug"
]
];
foreach($second as $val){
foreach($val as $key => $v){
if(in_array($key, $a)){
echo $v."<br>";
}
}
}
I have an array containing multiple arrays like
$A = array();
$A[0] = array("1","2","3","4","5");
$A[1] = array("1","6","7","8");
$A[2] = array("0","1","3");
I want to sort the values in multiple arrays in the order of frequency and put them into another array let's say $B.
The values in $B is "1","1","1","3","3","0","2","4","5","6","7","8".
$A = array();
$A[0] = array("1","2","3","4","5");
$A[1] = array("1","6","7","8");
$A[2] = array("0","1","3");
//Merging above array in to one array
$merged = array_values(call_user_func_array('array_merge', $A));
//Getting occurrence count
$counts = array_count_values($merged);
//Sort by count
arsort($counts);
//Adding to required array
$B = array();
foreach ($counts as $k => $v)
for($i=1;$i<=$v;$i++)
$B[] = $k;
echo "<pre>";
print_r($B);
echo "</pre>";
Result
Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 3
[4] => 3
[5] => 0
[6] => 8
[7] => 7
[8] => 5
[9] => 2
[10] => 4
[11] => 6
)
First merge all arrays
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$resultado = array_merge($array1, $array2);
see -> http://php.net/manual/es/function.array-merge.php
Second sort the big array
arsort($resultado );
see -> http://php.net/manual/es/array.sorting.php
Use a hash table to count the frequency of each number, and then store them in the decreasing order of frequency in array $B, like this:
$hash_table = array();
foreach($A as $array){
foreach($array as $value){
if(empty($hash_table[$value])){
$hash_table[$value] = 1;
}else{
$hash_table[$value] += 1;
}
}
}
arsort($hash_table);
$B = array();
foreach($hash_table as $key => $value){
for($i = 0; $i < $value; ++$i){
$B[] = $key;
}
}
var_dump($B); // to see the contents of array $B
If order of same occurance count items isn't important, you can use:
// Merge all arrays
$counts = array_count_values(call_user_func_array('array_merge', $A));
// Sort by occurance
arsort($counts);
// Add [value] to new array [occurance] times
$B = array();
array_walk($counts, function($occurances, $value) use (&$B){
for($i=0;$i<$occurances;$i++) $B[] = $value;
});
echo implode(',', $B);
Output
1,1,1,3,3,0,8,7,5,2,4,6
Array print in order of count and index:
$temp = array();
foreach($A as $b){
foreach($b as $c){
if(isset($tmep[$c])){
$tmep[$c]++;
}else{
$tmep[$c] = 1;
}
}
}
function SortArrayByKeyThanValue (&$pArray, $pSortMethodForKey = SORT_ASC, $pSortMethodForValue = SORT_DESC){
# check user input: sorting is not necessary
if (count($pArray) < 2)
return;
# define $k and $v as array_multisort() needs real variables, as user input is put by reference
$k = array_keys ($pArray);
$v = array_values($pArray);
array_multisort(
$v, $pSortMethodForValue,
$k, $pSortMethodForKey
);
$pArray = array_combine($k, $v);
}
SortArrayByKeyThanValue($tmep);
$B = array();
array_walk($tmep, function($occurances, $value) use (&$B){
for($i=0;$i<$occurances;$i++) $B[] = $value;
});
echo implode(',', $B);
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
i am stuck at this stage of my project.
i am trying to get common values from four multidimensional arrays using array_intersect. can anyone help me with this issue ?
here are all four array:
$arr=array(array(8159),array(8140),array(8134),array( 8168),array(8178),array( 8182),array( 8183));
$arr1=array(array(8159),array(8140),array(8134),array(8165),array(8166),array(8167),array( 8168));
$arr2=array(array(566),array(265),array(8134),array(655),array(8166),array(665),array( 8168),array(656),array( 989),array( 989));
$arr3=array(array(8159),array(8140),array(8134),array(8165),array(8166),array(8167),array( 8168));
$res= array_intersect($arr,$arr1,$arr2,$arr3);
print_r($res);
If subarray contain one element always you could chage that value using array_map and current function.
$arr=array(array(8159),array(8140),array(8134),array( 8168),array(8178),array( 8182),array( 8183));
$arr1=array(array(8159),array(8140),array(8134),array(8165),array(8166),array(8167),array( 8168));
$arr2=array(array(566),array(265),array(8134),array(655),array(8166),array(665),array( 8168),array(656),array( 989),array( 989));
$arr3=array(array(8159),array(8140),array(8134),array(8165),array(8166),array(8167),array( 8168));
$arr = array_map('current', $arr); // getting first value of subarray
$arr1 = array_map('current', $arr1);
$arr2 = array_map('current', $arr2);
$arr3 = array_map('current', $arr3);
print_r($arr3);
// Array
// (
// [0] => 8159
// [1] => 8140
// [2] => 8134
// [3] => 8165
// [4] => 8166
// [5] => 8167
// [6] => 8168
// )
$res= array_intersect($arr,$arr1,$arr2,$arr3);
print_r($res);
// Array
// (
// [2] => 8134
// [3] => 8168
// )
Please check this
$arr=array(array(8159),array(8140),array(8134),array( 8168),array(8178),array( 8182),array( 8183));
$arr1=array(array(8159),array(8140),array(8134),array(8165),array(8166),array(8167),array( 8168));
$arr2=array(array(566),array(265),array(8134),array(655),array(8166),array(665),array( 8168),array(656),array( 989),array( 989));
$arr3=array(array(8159),array(8140),array(8134),array(8165),array(8166),array(8167),array( 8168));
foreach($arr as $value)
{
$a1[] = $value[0];
}
foreach($arr1 as $value)
{
$a2[] = $value[0];
}
foreach($arr2 as $value)
{
$a3[] = $value[0];
}
foreach($arr3 as $value)
{
$a4[] = $value[0];
}
$res= array_intersect($a1,$a2,$a3,$a4);
print_r($res);