array merge php with same index - php

I have this situation:
$qty = array(1) {[0]=> array(1) { ["qty"]=> string(5) "35254" }
$price = array(1) {[0]=> array(1) { ["price"]=> string(5) "1000" }
How can I get this?
$res = array(1) {[0]=> array(1) { ["qty"]=> string(5) "35254" ["price"]=> string(5) "1000"}
Thanks for the answers

May be it's that you want:
$res = array();
foreach($qty as $k => $v){
$res[$k] = array_merge($qty[$k],$price[$k]);
}
The result :
array(1) {[0] => array(2) { 'qty' => string(5) "35254" 'price' => string(4) "1000" } }

$qty = array("qty"=>"35254" );
$price = array ( "price"=> "1000" );
$combine = array_merge($qty,$price);
var_dump($combine);

try with
$res = array_merge_recursive($qty, $price);
print_r($res);

Not as pretty but with the same result.
$result = array_map(function ($e1,$e2) {
return array_merge_recursive($e1, $e2);
}, $qty,$price);
$result =
array(1) {
[0]=>
array(2) {
["qty"]=>
string(5) "35254"
["price"]=>
string(4) "1000"
}
}
and for indexed arrays
$a = ['a', 'b', 'c'];
$n = [1, 2, 3];
$result = array_map(function ($e1,$e2) {
return [$e1, $e2];
}, $a,$n);
$result = [
0 => ['a', 1],
1 => ['b', 2],
2 => ['c', 3]
];

Related

PHP references in uasort function

I have
$products = array(
"product1" => [
"a" => ["total" => 1],
"b" => ["total" => 3],
"c" => ["total" => 2],
],
"product2" => [
"d" => ["total" => 3],
"f" => ["total" => 2],
"e" => ["total" => 1],
],
"product3" => [
"g" => ["total" => 3]
],
);
theses are my products and my stocks for each warehouses (warehouse a has 1 item of product1...)
I want to sort each warehouse by stock for each products.
I've done that :
foreach ($products as &$stocks) {
uasort($stocks, function($elmt1, $elmt2) {
return $elmt2["total"] - $elmt1["total"];
});
}
where I print my new array :
array(3) {
["product1"]=>
array(3) {
["b"]=>
array(1) {
["total"]=>
int(3)
}
["c"]=>
array(1) {
["total"]=>
int(2)
}
["a"]=>
array(1) {
["total"]=>
int(1)
}
}
["product2"]=>
array(3) {
["d"]=>
array(1) {
["total"]=>
int(3)
}
["f"]=>
array(1) {
["total"]=>
int(2)
}
["e"]=>
array(1) {
["total"]=>
int(1)
}
}
["product3"]=>
&array(1) {
["g"]=>
array(1) {
["total"]=>
int(3)
}
}
}
It did my job but when I get a closer look I can see the "&" char in only one of the arrays.
Why ?
Due to the fact that $item - a reference to the last element of the array.
You can do this trick:
$array = ['a', 'b', 'c'];
foreach ($array as &$item) {
}
foreach ($array as $item) {
$item = 1;
}
var_dump($array);
Output:
array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
&int(1)
}
It's not terrible, as long as you do not start to use $item.
It is better to make a job with reference in different function
$array = ['a', 'b', 'c'];
$test = function () use (&$array) {
foreach ($array as &$item) {
}
};
$test();
var_dump($array);
Output:
array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
}
Or try this:
$products = array(
"product1" => [
"a" => ["total" => 1],
"b" => ["total" => 3],
"c" => ["total" => 2],
],
"product2" => [
"d" => ["total" => 3],
"f" => ["total" => 2],
"e" => ["total" => 1],
],
"product3" => [
"g" => ["total" => 3]
],
);
foreach ($products as $key=>$stocks) {
uasort($products[$key], function($elmt1, $elmt2) {
return $elmt2["total"] - $elmt1["total"];
});
}
var_dump($products);
Output:
array(3) {
["product1"]=>
array(3) {
["b"]=>
array(1) {
["total"]=>
int(3)
}
["c"]=>
array(1) {
["total"]=>
int(2)
}
["a"]=>
array(1) {
["total"]=>
int(1)
}
}
["product2"]=>
array(3) {
["d"]=>
array(1) {
["total"]=>
int(3)
}
["f"]=>
array(1) {
["total"]=>
int(2)
}
["e"]=>
array(1) {
["total"]=>
int(1)
}
}
["product3"]=>
array(1) {
["g"]=>
array(1) {
["total"]=>
int(3)
}
}
}

Count from two array php

I have two array, arrLevel1 and arrLevel2.
I want to count animal that can walk.
How can I do that array stucture like this?
Thx before. I already tried, but it failed.
arrLevel1:
array(4) {
[0]=>
array(1) {
["Walk"]=>
string(4) "Bird"
}
[1]=>
array(1) {
["Walk"]=>
string(3) "Cat"
}
[2]=>
array(1) {
["Fly"]=>
string(9) "ButterFLy"
}
[3]=>
array(1) {
["Fly"]=>
string(4) "Bird"
}
}
arrLevel2:
array(3) {
[0]=>
array(1) {
["Animal"]=>
string(3) "Fly"
}
[1]=>
array(1) {
["Animal"]=>
string(11) "Walk"
}
[2]=>
array(1) {
["Human"]=>
string(11) "Walk"
}
}
Just check them in a loop
For the first arrLevel1:
<?php
$arrLevel1 = array(array("walk"=>"Bird"),array("walk"=>"Cat"),array("Fly"=>"Butterfly"),array("Fly"=>"Bird"));
$x = 0;
foreach($arrLevel1 as $p){
if($p["walk"]!==null){
$x++;
}
}
var_dump($x);
?>
Hope thsi helps you
One way to do it is using array_reduce():
$array = array(array('Walk' => 'Bird'), array('Walk' => 'Cat'), array('Fly' => 'ButterFly'), array('Fly' => 'Bird'));
$count = array_reduce($array, function($carry, $item) {
return key($item) == 'Walk' ? $carry + 1 : $carry;
}, 0);
var_dump($count);
You can use a similar code for the second version:
$array = array(array('Animal' => 'Fly'), array('Animal' => 'Walk'), array('Human' => 'Walk'));
$count = array_reduce($array, function($carry, $item) {
return reset($item) == 'Walk' ? $carry + 1 : $carry;
}, 0);
var_dump($count);

PHP array sort and remove duplicates by two field values

I have an array structure like this
[0]=>array(3) {
["Number"]=> "L1"
["Location"]=> "Location-A"
["Qty"]=>"1"
}
[1]=>array(3) {
["Number"]=> "L1"
["Location"]=> "Location-B"
["Qty"]=> "5"
}
[2]=> array(3) {
["Number"]=> "L1"
["Location"]=> "Location-B"
["Qty"]=> "4"
}
[3]=>array(3) {
["Number"]=> "L2"
["Location"]=> "Location-B"
["Qty"]=> "5"
}
But i required below structure as ouput
[0]=>array(3) {
["Number"]=> "L1"
["Location"]=> "Location-A"
["Qty"]=>"1"
}
[1]=> array(3) {
["Number"]=> "L1"
["Location"]=> "Location-B"
["Qty"]=> "4"
}
[2]=>array(3) {
["Number"]=> "L2"
["Location"]=> "Location-B"
["Qty"]=> "5"
}
How can i remove duplicate value by Number and Location?
ksort only works for one value, i need to remove by two values , how can i achieve this PHP ?
$ordered = array();
foreach ($data as $da)
{
$ordered[$da['Number']] = $da;
$ordered[$da['Location']] = $da;
}
ksort($ordered);
Concatenate the two fields when creating your new array:
foreach ($data as $da) {
$result[$da['Number'] . '.' . $da['Location']] = $da;
}
$result = array_values($result); // Turn it back into indexed array
Try this..
<?php
$array = array(
0 => array('Number'=>'L1','Location'=>'Location-A','Qty'=>'1'),
1 => array('Number'=>'L1','Location'=>'Location-B','Qty'=>'5'),
2 => array('Number'=>'L1','Location'=>'Location-B','Qty'=>'4'),
3 => array('Number'=>'L2','Location'=>'Location-B','Qty'=>'5'),
);
$output = array_values(array_intersect_key($array,array_unique(array_map(function($arrayval) {
return $arrayval['Number'] . '.' .$arrayval['Location'];
}, $array))
));
print_r($output);
Output
Array ( [0] => Array ( [Number] => L1 [Location] => Location-A [Qty] => 1 )
[1] => Array ( [Number] => L1 [Location] => Location-B [Qty] => 5 )
[2] => Array ( [Number] => L2 [Location] => Location-B [Qty] => 5 ) )
Try this:
function array_unique_c($array, Closure $comparer) {
$result = array();
for($i = 0; $i < count($array); $i++) {
$duplicates = false;
for($n = $i + 1; $n < count($array); $n++) {
if ($comparer($array[$i], $array[$n])) {
$duplicates = true;
break;
}
}
if(!$duplicates) {
$result[] = $array[$i];
}
}
return $result;
}
Usage:
$uniqueArray = array_unique_c($a, function ($itemA, $itemB) {
return $itemA['Number'] == $itemB['Number'] && $itemA['Location'] == $itemB['Location'];
});
Output:
array(3) {
[0] => array(3) {
["Number"] => string(2) "L1"
["Location"] => string(10) "Location-A"
["Qty"] => string(1) "1"
}
[1] => array(3) {
["Number"]=> string(2) "L1"
["Location"]=> string(10) "Location-B"
["Qty"]=> string(1) "4"
}
[2]=> array(3) {
["Number"]=> string(2) "L2"
["Location"]=> string(10) "Location-B"
["Qty"]=> string(1) "5"
}
}
Easy way to do this job:
$data = [
["Number"=> "L1","Location"=> "Location-A","Qty"=>"1"],
["Number"=> "L2","Location"=> "Location-B","Qty"=>"6"],
["Number"=> "L3","Location"=> "Location-A","Qty"=>"8"],
["Number"=> "L2","Location"=> "Location-B","Qty"=>"5"],
["Number"=> "L3","Location"=> "Location-A","Qty"=>"2"],
["Number"=> "L1","Location"=> "Location-B","Qty"=>"4"],
["Number"=> "L1","Location"=> "Location-B","Qty"=>"1"],
["Number"=> "L2","Location"=> "Location-B","Qty"=>"3"],
];
foreach ($data as $k=>$v) {
$arr[md5($v['Number'].$v['Location'])] = $v;
}
$result = array_values($arr); //can be omitted
As you can see $arr is equal $result and you can ommit array_values() func

Combining multidimensional array (ones key with other's values)

Played for hours, but couldn't do this. Task looks very simple, though..I need recursively combine 2 arrays into one. Using first array's values as keys, and second array's leave values as they are. This is what I have:
array(2) {
[0]=>
array(4) {
[0]=>
string(9) "First"
[1]=>
string(6) "Something"
}
[1]=>
array(4) {
[0]=>
string(3) "More"
[1]=>
string(6) "Nomore"
}
}
Second array
array(2) {
[0]=>
array(4) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
}
[1]=>
array(4) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
}
}
What I'm trying to achive:
array(2) {
[0]=>
array(4) {
["First"]=>
string(1) "1"
["Something"]=>
string(1) "2"
}
[1]=>
array(4) {
["More"]=>
string(1) "1"
["Nomore"]=>
string(1) "2"
}
}
Another solution using array_combine
$first_array = array(
array('first', 'second', 'third'),
array('more1', 'more2', 'more3'),
);
$second_array = array(
array('val1', 'val2', 'val3'),
array('2val1', '2val2', '2val3')
);
$new_array = array();
foreach($first_array AS $k => $v) {
$new_array[$k] = array_combine($v,$second_array[$k]);
}
$firstArray = array(
array('first', 'second', 'third'),
array('more1', 'more2', 'more3'),
);
$secondArray = array(
array('val1', 'val2', 'val3'),
array('2val1', '2val2', '2val3')
);
$newArray = array();
for ($i=0; $i<count($firstArray); ++$i) {
$subArray1 = $firstArray[$i];
$subArray2 = $secondArray[$i];
$newArray[$i] = array();
for ($j=0; $j<count($subArray1); ++$j) {
$key = $subArray1[$j];
$value = $subArray2[$j];
$newArray[$i][$key] = $value;
}
}
var_dump($newArray);
Wouldn't be more elegant to do something like this ?
$newArray = array();
foreach ($firstArray as $key => $firstVal)
foreach ($secondArray as $key => $secondVal)
array_push($newArray, array_combine($firstVal, $secondVal));
This way you'll have the same result you wanted inside $newArray
with a bit simpler code.
I haven't tested that though, let me know if it works or breaks :)

Array values to check another array values in PHP?

I have an array $minus
array(3) { [0]=> string(6) "people"
[1]=> string(7) "friends"
[2]=> string(8) "siblings"
}
And I have an array $user
array(3) { ["people"]=> string(3) "100"
["friends"]=> string(2) "10"
["siblings"]=> string(2) "57"
}
I can get the values of $user by using the values of $minus like,
echo $user[$minus[0]] . ', ' . $user[$minus[1]] . ', ' . $user[$minus[2]];
// Would echo: 100, 10, 57
But how can I get the values of $user by using the values of $minus into a new array, the new array should be like,
array(3) { [0]=> string(3) "100"
[1]=> string(2) "10"
[2]=> string(2) "57"
}
I have tried using foreach loops but can never get it right?
foreach($minus as $key=>$value) {
$new_array[$key] = $user[$value];
}
Use array_map, PHP >= 5.3 only
$new_array = array_map(function($item) use ($user) {return $user[$item];}, $minus);
$new_array= array();
foreach ($minus as $key => $value){
$new_array[$key] = $user[$value];
}
print_r($new_array);
$new_array = array($user[$minus[0]], $user[$minus[1]], $user[$minus[2]]);
$minus = array(0 => "people",
1 => "friends",
2 => "siblings"
);
$user = array("people" => "100",
"friends" => "10",
"siblings" => "57"
);
$newArray = $minus;
array_walk($newArray,function(&$item, $key, $prefix) { $item = $prefix[$item]; },$user);
var_dump($newArray);

Categories