php compare 2 array and manipulation increase value - php

I have 2 array
a=array(a=>1,b=>2,c=>2,d=>2,e=>2,f=>2)
and
b=array(a,b,d)
I want to make function compare_plus(array a, array b) like if array a have key== array b val then in increase value of array a at this key by 1.
Example with above array a and b:
c=compare_plus(a,b) =>> c=(a=>2,b=>3,c=>2,d=>3,f=>2)

$a = array('a' => 1, 'b' => 2, 'c' => 2, 'd' => 2, 'e' => 2, 'f' => 2);
$b = array('a', 'b', 'd');
$c = compare_plus($a, $b);
print_r($c);
function compare_plus($arr, $plusarr){
foreach($plusarr as $key)
$arr[$key]++;
return $arr;
}
Codepad Demo

If you want to add only to existing keys and not create additional ones, you'll need something like this:
$a = array("a" => 1, "b" => 2, "c" => 2, "d" => 2, "e" => 2, "f" => 2);
$b = array("a", "b", "d", "g", "apple");
$c = compare_plus($a, $b);
print_r($c);
function compare_plus($arr, $plusarr){
foreach($plusarr as $key)
if (array_key_exists($key, $arr))
$arr[$key]++;
return $arr;
}
/* // Output:
Array
(
[a] => 2
[b] => 3
[c] => 2
[d] => 3
[e] => 2
[f] => 2
)
*/
To add the additional keys from $b to $c, simply remove if (array_key_exists($key, $arr)).
http://codepad.org/aquc5DKA

Related

PHP asort by value ASC, value 0 should go last

I have the following array:
$values = [
'a' => 0,
'b' => 1,
'c' => 0,
'd' => 3,
'e' => 2
];
I need to be able to sort it like so:
$values = [
'b' => 1,
'e' => 2,
'd' => 3,
'a' => 0,
'c' => 0
];
The fastest solution I managed to come up with is:
$zero = array_filter($values, fn($val) => $val === 0);
$non_zero = array_diff_assoc($values, $zero);
asort($non_zero);
$result = array_merge($non_zero, $zero);
Sorting rules:
keys need to be preserved
key order does not matter for the same value
My question is: is there a better way to do this?
Here is a sandbox version
Thank you!
UPDATE
It looks like this one might work too:
asort($values);
uasort($values, function($a, $b){
return $a === 0 ? 1 : -1;
});
Sandbox for this version
Any better ideas?
I managed to come up with this solution which looks decent:
$values = [
'i' => 2,
'a' => 0,
'b' => 1,
'f' => 1,
'c' => 0,
'd' => 3,
'g' => 3,
'e' => 2,
'h' => 3,
];
uasort($values, function($a, $b){
if ($a === 0) {
return 1;
}
if ($b === 0 || $a === $b) {
return 0;
}
return $a < $b && $b !== 0 ? -1 : 1;
});
And it appears to be returning the expected output on all cases:
Array
(
[b] => 1
[f] => 1
[i] => 2
[e] => 2
[d] => 3
[g] => 3
[h] => 3
[c] => 0
[a] => 0
)

PHP: add missing keys between arrays [duplicate]

This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
Closed 5 months ago.
I have following array keys values:
$arrData = array
(
array(
'a' => 'test',
'c' => 1,
'd' => 2,
'e' => 'B'
),
array(
'c' => 1,
'd' => 2,
'e' => 'B'
),
array(
'b' => 'test2',
'c' => 1,
'd' => 2,
'e' => 'B'
)
);
So here I need to merged array into single with combining missing keys with single value array.
Can someone please help to get following output in single array?
$arrData = array
(
array(
'a' => 'test',
'b' => 'test2',
'c' => 1,
'd' => 2,
'e' => 'B'
)
);
Thanking in advance!
Just merge them and then sort on the key:
$arrData = array_merge(...$arrData);
ksort($arrData);
Instead of ... you can use:
$arrData = call_user_func_array('array_merge', $arrData);
If you really want the result to be multi-dimensional, then:
$arrData = [$arraData];
//or
$arrData = array($arrData);
You can use array_reduce (or a simple foreach loop) to merge each of the subsequent array values with the first one:
$out = array_reduce($arrData, function ($c, $v) { return array_merge($c, $v); }, array());
print_r($out);
$out = array();
foreach ($arrData as $arr) {
$out = array_merge($out, $arr);
}
print_r($out);
Output (for both examples):
Array (
[a] => test
[c] => 1
[d] => 2
[e] => B
[b] => test2
)
If you want to keep the keys in alphabetical order, you can use ksort:
ksort($out);
print_r($out);
Array (
[a] => test
[b] => test2
[c] => 1
[d] => 2
[e] => B
)
Demo on 3v4l.org
Using array_walk and ksort
$res=[];
array_walk($arrData, function($v,$k) use(&$res){
$res = array_merge($res,$v);
});
ksort($res);
OR
You can use foreach and array_column
$keys = ['a','b','c','d','e'];
$res=[];
foreach($keys as $val){
$res[$val] = array_column($arrData, $val)[0];
}
print_r($res);
Live Demo
<?php
$arrData = array
(
array(
'a' => 'test',
'c' => 1,
'd' => 2,
'e' => 'B'
),
array(
'c' => 1,
'd' => 2,
'e' => 'B'
),
array(
'b' => 'test2',
'c' => 1,
'd' => 2,
'e' => 'B'
)
);
$result_array = array();
foreach($arrData as $ad){
foreach($ad as $key=>$value){
if(!array_key_exists($key,$result_array)){
$result_array[$key] = $value;
}
}
}
print_r($result_array);
?>

How to Extract and combine array from array

I have print_r result like this :
Array
(
[0] => A, B, C, D
[1] => 15,20,24,19
)
how to make them like this :
Array
(
[A] => 15
[B] => 20
[C] => 24
[D] => 19
)
Great thanks for help :)
Try this:
$a = array('A', 'B', 'C', 'D');
$b = array(15, 20, 24, 19);
$c = array();
foreach ($a as $index => $value) {
if (isset($b[$index])) {
$c[$value] = $b[$index];
}
}
var_dump($c);
You need explode() and array_combine(). Assuming your initial array, $arr:
$new_arr = array_combine(explode(', ', $arr[0]), explode(',', $arr[1]));
See demo
Try to explode() your array index by comma and combine both array with keys and values using array_combine()
$a = explode(',',$arr[0]);
$b = explode(',',$arr[1]);
$new = array_combine($a,$b);
print_r($new); //Array ( [A] => 15 [ B] => 20 [ C] => 24 [ D] => 19 )
array_combine is the way
<?php
$myArray = array(
array('A', 'B', 'C', 'D'),
array(15, 20, 24, 19)
);
$combinedArray = array_combine($myArray[0], $myArray[1]);

PHP - get array records by index range

HI there,
Is there any PHP native function which returns the range of records from the array based on the start and end of the index?
i.e.:
array(0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd');
and now I would like to only return records between index 1 and 3 (b, c, d).
Any idea?
Couldn't you do that with e.g. array_slice?
$a = array(0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd');
array_slice($a, 1, 3);
there is a task for array_slice
array array_slice ( array $array , int $offset [, int $length [, bool $preserve_keys = false ]] )
example:
$input = array("a", "b", "c", "d", "e");
$output = array_slice($input, 2); // returns "c", "d", and "e"
$output = array_slice($input, -2, 1); // returns "d"
$output = array_slice($input, 0, 3); // returns "a", "b", and "c"
// note the differences in the array keys
print_r(array_slice($input, 2, -1));
print_r(array_slice($input, 2, -1, true));
By using array_intersect_key
$myArray = array(0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd');
$arrayRange = array('1', '2', '3');
// this can also be used if you have integer only array values
// $arrayRange = range(1,3);
$newArray = array_intersect_key($myArray, array_flip($arrayRange));
print_r($newArray); // output: Array ( [1] => b [2] => c [3] => d )
$array1 = array(1,2,3,4,5,6,23,24,26,21,12);
foreach(range ($array1[0],$array1[5]) as $age){
echo "Age: {$age}<br />";
}
you should get the following output:
Age: 1
Age: 2
Age: 3
Age: 4
Age: 5
Age: 6

if $a and $b are bothe arrays the what would be the result of $a+$b? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
+ operator for array in PHP?
If $a and $b are both arrays, what is the result of $a + $b?
http://www.php.net/manual/en/language.operators.array.php
Union of $a and $b.
The + operator appends elements of
remaining keys from the right handed
array to the left handed, whereas
duplicated keys are NOT overwritten.
<?php
$a = array(1, 2, 3);
$b = array(4, 5, 6);
$c = $a + $b;
print_r($c);
results in this for me:
Array
(
[0] => 1
[1] => 2
[2] => 3
)
BUT:
<?php
$a = array('a' => 1, 'b' => 2, 'c' => 3);
$b = array('d' => 4, 'e' => 5, 'f' => 6);
$c = $a + $b;
print_r($c);
results in:
Array
(
[a] => 1
[b] => 2
[c] => 3
[d] => 4
[e] => 5
[f] => 6
)
So it would appear that the answer here depends on how your arrays are keyed.
My test
$ar1 = array('1', '2');
$ar2 = array('3', '4');
$test = $ar1 + $ar2;
print_r($test);
Array
(
[0] => 1
[1] => 2
)
Now try this experiment
$a = array( 0 => 1,
1 => 2,
4 => 3
);
$b = array( 2 => 4,
4 => 5,
6 => 6
);
$c = $a + $b;
var_dump($c);
If you do something like $result = $a + $b; then $result will be assigned to the first argument, in this case $a.

Categories