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
)
)
Related
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.
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);
I want to pop from array2 and want to push in array1.
But according to some custom requirement.
Now in array1 there is 1st key's readingOrder is 1 and 2nd key's readingOrder is 4.
So i want to push between this two key from array2's first two key.And same process for all other.
and my final array must be like array3.
for example in array1 key[0] readingOrder is 1 and key[1]'s 4. Now i want to push another two key from array2.
for array1 key[2] reading order is 6. so before this key i want to push another one key from array2 and same for further...
array1 is like below
Array
(
[0] => Array
(
[readingOrder] => 1
[id] => 78
)
[1] => Array
(
[readingOrder] => 4
[id] => 76
)
[2] => Array
(
[readingOrder] => 6
[id] => 80
)
)
array2 is like below
Array
(
[0] => Array
(
[id] => 81
[readingOrder] => 2
)
[1] => Array
(
[id] => 82
[readingOrder] => 5
)
[2] => Array
(
[id] => 84
[readingOrder] => 7
)
[3] => Array
(
[id] => 85
[readingOrder] => 8
)
[4] => Array
(
[id] => 86
[readingOrder] => 9
)
[5] => Array
(
[id] => 87
[readingOrder] => 10
)
[6] => Array
(
[id] => 88
[readingOrder] => 11
)
)
Output array3:
Array
(
[0] => Array
(
[readingOrder] => 1
[id] => 78
)
[1] => Array
(
[id] => 81
[readingOrder] => 2
)
[2] => Array
(
[id] => 82
[readingOrder] => 5
)
[3] => Array
(
[readingOrder] => 4
[id] => 76
)
[4] => Array
(
[id] => 84
[readingOrder] => 7
)
[5] => Array
(
[readingOrder] => 6
[id] => 80
)
[6] => Array
(
[id] => 85
[readingOrder] => 8
)
[7] => Array
(
[id] => 86
[readingOrder] => 9
)
[8] => Array
(
[id] => 87
[readingOrder] => 10
)
[9] => Array
(
[id] => 88
[readingOrder] => 11
)
)
Thanks..
You can build your array like that:
$current = 1;
$arr3 = [];
while ( $arr1 && $arr2 ) {
if ( $arr1[0]['readingOrder'] > $current )
$arr3[] = array_shift($arr2);
else
$arr3[] = array_shift($arr1);
$current++;
}
$arr3 = array_merge($arr3, $arr1, $arr2);
print_r($arr3);
Note that this code is destructive for $arr1 and $arr2. If you want to preserve them, copy them before and use the copies instead.
You can do this with usort. First you need to merge the arrays:
$a1 = [
[
'readingOrder' => 1,
'id' => 78
],
[
'readingOrder' => 4,
'id' => 76
],
[
'readingOrder' => 6,
'id' => 80
]
];
$a2 = [
[
'readingOrder' => 2,
'id' => 81
],
[
'readingOrder' => 5,
'id' => 82
],
[
'readingOrder' => 7,
'id' => 84
],
[
'readingOrder' => 8,
'id' => 85
]
];
$a3 = array_merge($a1, $a2);
Then you need to use usort:
usort($a3, function($a,$b) {
if ($a['readingOrder'] == $b['readingOrder']) return 0;
return $a['readingOrder'] < $b['readingOrder'] ? -1 : 1;
});
In PHP 7 you can now use the spaceship operator, which would make the code more clean. Like so:
usort($a3, function($a,$b) {
return $a[0] <=> $b[0];
});
This will then return:
(
[0] => Array
(
[readingOrder] => 1
[id] => 78
)
[1] => Array
(
[readingOrder] => 2
[id] => 81
)
[2] => Array
(
[readingOrder] => 4
[id] => 76
)
[3] => Array
(
[readingOrder] => 5
[id] => 82
)
[4] => Array
(
[readingOrder] => 6
[id] => 80
)
[5] => Array
(
[readingOrder] => 7
[id] => 84
)
[6] => Array
(
[readingOrder] => 8
[id] => 85
)
)
I'm getting values for my flot chart via ajax.
At the backend script my test array looks like this:
Array
(
[0] => Array
(
[0] => 6
[1] => 1
)
[1] => Array
(
[0] => 7
[1] => 7
)
[2] => Array
(
[0] => 8
[1] => 37
)
[3] => Array
(
[0] => 9
[1] => 44
)
)
The value with the offset [0] represents the hour.
Now I need 24 array objects for every hour. How to maintain this without touching the given elements?
e.g.
Array
(
[0] => Array
(
[0] => 0
[1] => 0
)
[6] => Array
(
[0] => 6
[1] => 1
)
[...]
[7] => Array
(
[0] => 7
[1] => 7
)
[8] => Array
(
[0] => 8
[1] => 37
)
[9] => Array
(
[0] => 9
[1] => 44
)
Thanks in advance.
You want to "re-key" this result set according to the first subarray value, and then merge it into an array of 24 hour elements.
Try this:
$data = [
[6, 1],
[7, 7],
[8, 37],
[9, 44]
];
$hours = array_fill(0, 25, [0, 0]);
$data = array_combine(array_column($data, 0), $data);
$hours = array_replace($hours, $data);
print_r($hours);
This:
Creates a 24 element array $hours containing sub-arrays containing default values [0, 0]
Re-keys your $data, pulling out and using the first value of each sub-array to do so. This assumes you are using PHP 5.5.
Finally, replace the modified $data into $hours
This yields:
Array
(
[0] => Array
(
[0] => 0
[1] => 0
)
[1] => Array
(
[0] => 0
[1] => 0
)
[2] => Array
(
[0] => 0
[1] => 0
)
[3] => Array
(
[0] => 0
[1] => 0
)
[4] => Array
(
[0] => 0
[1] => 0
)
[5] => Array
(
[0] => 0
[1] => 0
)
[6] => Array
(
[0] => 6
[1] => 1
)
[7] => Array
(
[0] => 7
[1] => 7
)
[8] => Array
(
[0] => 8
[1] => 37
)
[9] => Array
(
[0] => 9
[1] => 44
)
[10] => Array
(
[0] => 0
[1] => 0
)
[11] => Array
(
[0] => 0
[1] => 0
)
[12] => Array
(
[0] => 0
[1] => 0
)
[13] => Array
(
[0] => 0
[1] => 0
)
[14] => Array
(
[0] => 0
[1] => 0
)
[15] => Array
(
[0] => 0
[1] => 0
)
[16] => Array
(
[0] => 0
[1] => 0
)
[17] => Array
(
[0] => 0
[1] => 0
)
[18] => Array
(
[0] => 0
[1] => 0
)
[19] => Array
(
[0] => 0
[1] => 0
)
[20] => Array
(
[0] => 0
[1] => 0
)
[21] => Array
(
[0] => 0
[1] => 0
)
[22] => Array
(
[0] => 0
[1] => 0
)
[23] => Array
(
[0] => 0
[1] => 0
)
)
Hope this helps :)
You can try also this:
// yours test array
$times = array(
array(6,1),
array(7,7),
array(8,37),
array(9,44)
);
// array with all hours
$fullTimes = array_fill(0, 24, array(0,0));
foreach ($times as $time) {
$fullTimes[$time[0]] = $time;
}
Solution for me:
while ($stmt->fetch()) {
$x = $hour;
$y = $zugriffe;
$data1[$x] = array ($x, $y);
}
for ($i=0;$i<=24;$i++){
if (!isset($data1[$i])){
$data1[$i] = array ($i,0);
}
}
sort($data1);
Assume the array's name is $a, you can do like this:
for($i = 0; $i < 24; $i++) {
if(isset($a[$i])) continue;
else {
$a[$i] = array($i,"something you like here");
}
}
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!!