Adding elements from first array to second array - php

I have 2 arrays with me
First array
Array ( [2019-04-29] => Array ( [0] => 366 [1] => 82 [2] => 44 ) [2019-04-30] => Array ( [0] => 330 [1] => 115 [2] => 55 ) )
Second array
Array ( [0] => 492 [1] => 500 )
Need to merge these arrays with output like this.
Array ( [0] => Array ( [0] => 492 [1] => 366 [2] => 82 [3] => 44 ) [1] => Array ( [0] => 500 [1] => 330 [2] => 115 [3] => 55 ) )
Please help.

You can use array_walk and array_merge
$arr1 = Array (
'2019-04-29' => Array (
'0' => 366,
'1' => 82,
'2' => 44
) ,
'2019-04-30' => Array (
'0' => 330,
'1' => 115,
'2' => 55
)
);
$arr2 = Array ( '0' => 492 ,'1' => 500 ) ;
$index = 0;
$res=[];
array_walk($arr1, function($v,$k) use (&$res,$arr2,&$index){
$res[] = array_merge(array($arr2[$index]),$v);
$index++;
});
echo '<pre>';
print_r($res);
Result
Array
(
[0] => Array
(
[0] => 492
[1] => 366
[2] => 82
[3] => 44
)
[1] => Array
(
[0] => 500
[1] => 330
[2] => 115
[3] => 55
)
)

You can do this with a foreach loop and array_merge, after using array_values to reindex $array1 to numeric indexes starting with 0.
$array1 = array_values($array1);
foreach ($array2 as $k => &$v) {
$v = array_merge(array($v), $array1[$k]);
}
print_r($array2);
Output:
Array
(
[0] => Array
(
[0] => 492
[1] => 366
[2] => 82
[3] => 44
)
[1] => Array
(
[0] => 500
[1] => 330
[2] => 115
[3] => 55
)
)
Demo on 3v4l.org

You can use array_walk and array_merge combination with traditional incrementer
array_walk($arr1, function (&$item, $key) use ($arr2,&$i) { // $i should change at memory address
$item = array_merge($item, [$arr2[$i]]);
$i++;
});
Output
Array
(
[2019-04-29] => Array
(
[0] => 366
[1] => 82
[2] => 44
[3] => 492
)
[2019-04-30] => Array
(
[0] => 330
[1] => 115
[2] => 55
[3] => 500
)
)
Demo.

Related

PHP trimensional array

I was wondering if someone could help me with this looping array in PHP as I am still learning the language.
I need to loop into the 3-dimensional (3D) array below in order to:
Check the [room_type_id] values and if its true to a specific value, I would return [check_in_times] array value for this same parent array.
For example:
if [room_type_ids] = 261
I would need to return [check_in_times] = 1
if [room_type_ids] = 36
I would need to return [check_in_times] = 2
My array is:
$myarray = Array (
[0] => Array (
[check_in_times] => Array (
[0] => 1
)
[room_type_ids] => Array (
[0] => 261
[1] => 281
[2] => 283
[3] => 292
[4] => 296
[5] => 365
[6] => 381
[7] => 387
[8] => 389
[9] => 730
)
[season_ids] => Array (
[0] => 0
)
)
[1] => Array (
[check_in_times] => Array (
[0] => 2
)
[room_type_ids] => Array (
[0] => 36
[1] => 38
[2] => 300
[3] => 318
[4] => 336
[5] => 889
[6] => 897
[7] => 728
[8] => 745
[9] => 747
)
[season_ids] => Array (
[0] => 0
)
)
)
Thank you!
Something like:
$room = 261;
$checks = 0;
foreach ($myarray as $subset)
{
if (in_array($room, $subset['room_type_ids']))
{
$checks = $subset['check_in_times'][0];
break;
}
}

Find array which has duplicate array

I have array like below, you can see 71 and 33 are duplicate value so I need find those array which has duplicate values
Array
(
[0] => Array
(
[0] => 71
[1] => 33
[2] => 46
)
[1] => Array
(
[0] => 71
)
[4] => Array
(
[0] => 71
[1] => 33
)
)
Expected output: I want array something like below
Array
(
[71] => Array
(
[0] => 0
[1] => 1
[2] => 4
)
[33] => Array
(
[0] => 0
[1] => 4
)
)
You could loop over the main array and values to store index a new array. Finally, you could reduce it to keep only duplicates using array_filter():
$array = array(
0 => array(
0 => 71,
1 => 33,
2 => 46
),
1 => array(
0 => 71
),
4 => array(
0 => 71,
1 => 33
)
);
// loop over the main array:
foreach ($array as $index => $arr_values) {
// loop over values:
foreach ($arr_values as $value) {
$out[$value][] = $index;
}
}
// finally, remove entries with only one values:
$out = array_filter($out, function($values) { return count($values) > 1 ; });
print_r($out);
Outputs:
Array
(
[71] => Array
(
[0] => 0
[1] => 1
[2] => 4
)
[33] => Array
(
[0] => 0
[1] => 4
)
)

php:array sum without foreach

I have an array like this
Array
(
[1_DAY_2017] => Array
(
[SAMSUNG] => Array
(
[0] => 549
[1] => 199
[2] => 999
)
[XIAOMI] => Array
(
[0] => 199
[1] => 2999
[2] => 499
)
)
[2_DAY_2017] => Array
(
[SAMSUNG] => Array
(
[0] => 699
[1] => 999
)
[LENOVO] => Array
(
[0] => 280
[1] => 2550
[2] => 849
)
)
[3_DAY_2017] => Array
(
[OPPO] => Array
(
[0] => 500
[1] => 599
)
[SAMSUNG] => Array
(
[0] => 799
)
)
[4_DAY_2017] => Array
(
[SAMSUNG] => Array
(
[0] => 1299
[1] => 499
[2] => 799
[3] => 2500
)
[OPPO] => Array
(
[0] => 299
[1] => 349
[2] => 499
)
)
[5_DAY_2017] => Array
(
[XIAOMI] => Array
(
[0] => 500
[1] => 270
[2] => 340
)
[VIVO] => Array
(
[0] => 4599
[1] => 299
)
)
[6_DAY_2017] => Array
(
[VIVO] => Array
(
[0] => 240
[1] => 1899
[2] => 759
[3] => 530
)
[OPPO] => Array
(
[0] => 999
)
)
[7_DAY_2017] => Array
(
[OPPO] => Array
(
[0] => 300
[1] => 252
[2] => 1290
)
[LENOVO] => Array
(
[0] => 570
[1] => 1300
[2] => 666
)
)
)
From this i want to get an array
$output= [SAMSUNG => 9341, XIAOMI => 4807]
Here each item contains sum of the items in nested array.
Currently my solution conatins more than 2 for each loops but is there any way to optimize this??
You can use array_sum, array_map and array_column functions to get the result
array_sum(array_map('array_sum', array_column($a, 'SAMSUNG')))
With single array_reduce function:
// $arr is your initial array
$result = array_reduce($arr, function($r, $v){
if (isset($v['SAMSUNG'])) $r['SAMSUNG'] += array_sum($v['SAMSUNG']);
if (isset($v['XIAOMI'])) $r['XIAOMI'] += array_sum($v['XIAOMI']);
return $r;
}, ['SAMSUNG' => 0, 'XIAOMI' => 0]);
$sumArray = array();
foreach ($YourArray as $k=>$subArray) {
foreach ($subArray as $id=>$value) {
$sumArray[$id]+=$value;
}
}
print_r($sumArray);

How do I flatten an array from three dimension into two dimensions?

How would i flatten the following array. I want to get rid of 0 and 1, and just have 0-5 as they keys. I am using php by the way.
Array
(
[0] => Array
(
[0] => Array
(
[id] => 182
)
[1] => Array
(
[id] => 183
)
[2] => Array
(
[id] => 185
)
[3] => Array
(
[id] => 184
)
[4] => Array
(
[id] => 186
)
)
[1] => Array
(
[0] => Array
(
[id] => 171
)
)
)
$flatArray = array();
foreach ($array as $l1Array){
foreach ($l1Array as $k => $v){
$flatArray[]=$v;
}
}

PHP remove entry if array value equals to 0

This is what i get when i print_r my array. it's a multi-dimensional array which contains the following values.
[7] => Array
(
[0] => 1
[1] => 34
[2] => 181
[3] => 50
)
[9] => Array
(
[0] => 1
[1] => 2
[2] => 1
[3] => 47
)
[2] => Array
(
[0] => 20
[1] => 0
[2] => 1621
[3] => 45
)
[3] => Array
(
[0] => 120
[1] => 0
[2] => 121
[3] => 45
)
I would like to remove all entries in which the key [1] equals to 0. After doing the modifications, My final array should like this
[7] => Array
(
[0] => 1
[1] => 34
[2] => 181
[3] => 50
)
[9] => Array
(
[0] => 1
[1] => 2
[2] => 1
[3] => 47
)
Any ideas ?
foreach to the rescue:
foreach($arr as $key => $entry) {
if(isset($entry[1]) && $entry[1] === 0) {
unset($arr[$key]);
}
}
And an array_filter example:
$arr = array_filter($arr, function($entry) {
return $entry[1] !== 0;
});
(assumes at least php 5.3, though you can get around that by creating a named function and passing that as the second parameter to array_filter)
If you want only remove array with value 0 whatever the key, you can use array_filter .
<?php
$array = array(1,2,3,4,5,0,'',null);
print_r(array_filter($array));
?>
Output :
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
You can remove empty,null and 0 value from array using this code
Code:-
$array = array('one', 'two', '', 'three', null,'four','0');
$filteredarray = array_values( array_filter($array) );
print_r($filteredarray);
Output:-
Array
(
[0] => one
[1] => two
[2] => three
[3] => four
)
Thank You!! All The Best!!

Categories