Remove child arrays recursively if all are certain value - php

I need to remove all months with 0 values, it must only remove it if all match 0.
Here is my array,
Array
(
[0] => Array
(
[0] => Month/Year
[1] => BRO002
[2] => SPI001
[3] => TYN001
)
[1] => Array
(
[0] => Jan
[1] => 0
[2] => 0
[3] => 1
)
[2] => Array
(
[0] => Feb
[1] => 0
[2] => 0
[3] => 0
)
[3] => Array
(
[0] => Mar
[1] => 0
[2] => 0
[3] => 0
)
[4] => Array
(
[0] => Apr
[1] => 0
[2] => 0
[3] => 0
)
[5] => Array
(
[0] => May
[1] => 0
[2] => 3
[3] => 0
)
[6] => Array
(
[0] => Jun
[1] => 0
[2] => 0
[3] => 0
)
[7] => Array
(
[0] => Jul
[1] => 0
[2] => 0
[3] => 0
)
[8] => Array
(
[0] => Aug
[1] => 0
[2] => 0
[3] => 0
)
[9] => Array
(
[0] => Sep
[1] => 11
[2] => 1
[3] => 2
)
[10] => Array
(
[0] => Oct
[1] => 0
[2] => 0
[3] => 0
)
[11] => Array
(
[0] => Nov
[1] => 0
[2] => 0
[3] => 0
)
[12] => Array
(
[0] => Dec
[1] => 0
[2] => 0
[3] => 0
)
)
The above array would turn into the following,
Array
(
[0] => Array
(
[0] => Month/Year
[1] => BRO002
[2] => SPI001
[3] => TYN001
)
[1] => Array
(
[0] => Jan
[1] => 0
[2] => 0
[3] => 1
)
[5] => Array
(
[0] => May
[1] => 0
[2] => 3
[3] => 0
)
[9] => Array
(
[0] => Sep
[1] => 11
[2] => 1
[3] => 2
)
)
Thank you.

Check each value:
$newArray = array();
foreach($array as $key => $val){
if(!(!$val[1] && !$val[2] && !$val[3])) {
$newArray[$key] = $array[$key];
}
}
print_r($newArray);

$array = array(
array('Jan', 0, 0, 1),
array('Feb', 0, 1, 0),
array('Mar', 0, 0, 0),
array('Apr', 1, 1, 1)
);
foreach($array as $i => $a) {
$save = false;
foreach($a as $k => $v) {
if($k == 0) continue;
if($v != 0) $save = true;
}
if(!$save) unset($array[$i]);
}
var_dump(array_values($array));

Without needing a second data structure:
foreach ( $data as $i => $month ) {
if ( $i == 0 ) { continue; }
if (
$month[1] == 0 &&
$month[2] == 0 &&
$month[3] == 0
) {
unset($data[$i]);
}
}

$Temp = array();
foreach ($Array as $Key=>$Value)
if ($Value[1]!=0 || $Value[2]!=0 || $Value[3]!=0)
$Temp[$Key] = $Value;
$Array = $Temp;

Related

Sum of two indexes which are having same values with multi dimension Array

I am working with multi dimensional array is like below with php,
$return_array= Array
(
[0] => Array
(
[0] => 3_Mar_2017
[1] => 0
[2] => 19
[3] => 7
[4] => 13
[5] => 3
[6] => 0
[7] => 42
)
[1] => Array
(
[0] => Yet to closed
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 1
[6] => 0
[7] => 1
)
[2] => Array
(
[0] => 3_Mar_2017
[1] => 0
[2] => 7
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 7
)
[3] => Array
(
[0] => 4_Apr_2017
[1] => 0
[2] => 8
[3] => 4
[4] => 0
[5] => 0
[6] => 0
[7] => 12
)
)
On 0th and 2nd indexs -> from sub array of that indexes -> 0th index are common "3_Mar_2017" ,I want to sum that two indexes and want result as shown below,
$final_return = Array
(
[0] => Array
(
[0] => 3_Mar_2017
[1] => 0
[2] => 26
[3] => 7
[4] => 13
[5] => 3
[6] => 0
[7] => 49
)
[1] => Array
(
[0] => Yet to closed
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 1
[6] => 0
[7] => 1
)
[2] => Array
(
[0] => 4_Apr_2017
[1] => 0
[2] => 8
[3] => 4
[4] => 0
[5] => 0
[6] => 0
[7] => 12
)
)
My tried code with loop as below,
$tem_array = array();
$final_return = array();
foreach ($return_array as $unique) {
if (!in_array($unique[0], $tem_array)) {
array_push($tem_array, $unique[0]);
$final_return[] = $unique;
} else {
$index = array_search($unique[0], $tem_array);
for ($i = 1; $i < count($unique); $i++) {
$final_return[$index][$i] = $final_return[$index][$i] + $unique[$i];
}
}
}
but if array size will large then ,may be it will take time is there any simple solution.
can any person help me to get this required result with minimum code ?
I will appreciate best answer.
hope this is what you are looking for
$temp1 = array(); $result = array();
foreach ($myArray as $temp) {
if (!in_array($temp[0], $temp1)) {
array_push($temp1, $temp[0]); $result[] = $temp;
} else {
$id = array_search($temp[0], $temp1); for ($i = 1; $i <= count($temp); $i++) {
$result[$id][$i] = $result[$id][$i] + $temp[$i];
}
}
}
your first array would look like
Array
(
[0] => Array
(
[0] => 13
[1] => 1
[2] => 2
[3] => 3
)
[1] => Array
(
[0] => 14
[1] => 5
[2] => 6
[3] => 7
)
[2] => Array
(
[0] => 13
[1] => 1
[2] => 2
[3] => 3
)
)
and the result be like
Array
(
[0] => Array
(
[0] => 13
[1] => 2
[2] => 4
[3] => 6
)
[1] => Array
(
[0] => 14
[1] => 5
[2] => 6
[3] => 7
)
)

Display a sum of two array values with same keys

I have two array with same keys this two array contain month wise data of my table. i want sum of this values and return same keys sum of values in one other array
Here is my two array
array1
Array ( [0] => Array ( [0] => Jan [1] => 0 )
[1] => Array ( [0] => Feb [1] => 22 )
[2] => Array ( [0] => Mar [1] => 0 )
[3] => Array ( [0] => Apr [1] => 9 )
[4] => Array ( [0] => May [1] => 1 )
[5] => Array ( [0] => Jun [1] => 0 )
[6] => Array ( [0] => Jul [1] => 0 )
[7] => Array ( [0] => Aug [1] => 0 )
[8] => Array ( [0] => Sep [1] => 0 )
[9] => Array ( [0] => Oct [1] => 0 )
[10] => Array ( [0] => Nov [1] => 0 )
[11] => Array ( [0] => Dec [1] => 0 )
)
array 2:
Array ( [0] => Array ( [0] => Jan [1] => 0 )
[1] => Array ( [0] => Feb [1] => 0 )
[2] => Array ( [0] => Mar [1] => 18 )
[3] => Array ( [0] => Apr [1] => 1 )
[4] => Array ( [0] => May [1] => 1 )
[5] => Array ( [0] => Jun [1] => 0 )
[6] => Array ( [0] => Jul [1] => 0 )
[7] => Array ( [0] => Aug [1] => 0 )
[8] => Array ( [0] => Sep [1] => 0 )
[9] => Array ( [0] => Oct [1] => 0 )
[10] => Array ( [0] => Nov [1] => 0 )
[11] => Array ( [0] => Dec [1] => 0 )
)
i also tried using this code
function sum_arrays($array1, $array2) {
$array = array();
foreach($array1 as $index => $value) {
$array[$index] = isset($array2[$index]) ? $array2[$index] + $value : $value;
}
return $array;
}
i want result like below
Array ( [0] => Array ( [0] => Jan [1] => 0 )
[1] => Array ( [0] => Feb [1] => 22 )
[2] => Array ( [0] => Mar [1] => 18 )
[3] => Array ( [0] => Apr [1] => 10 )
[4] => Array ( [0] => May [1] => 2 )
[5] => Array ( [0] => Jun [1] => 0 )
[6] => Array ( [0] => Jul [1] => 0 )
[7] => Array ( [0] => Aug [1] => 0 )
[8] => Array ( [0] => Sep [1] => 0 )
[9] => Array ( [0] => Oct [1] => 0 )
[10] => Array ( [0] => Nov [1] => 0 )
[11] => Array ( [0] => Dec [1] => 0 )
)
array_map approach (if 2 arrays have month items in same order):
// shortened array samples
$arr1 = [
["Jan", 0],
["Feb", 22],
["Mar", 0]
];
$arr2 = [
["Jan", 0],
["Feb", 0],
["Mar", 18]
];
$result = array_map(function($a, $b){
return [$a[0], $a[1] + $b[1]];
}, $arr1, $arr2);
print_r($result);
The output:
Array
(
[0] => Array
(
[0] => Jan
[1] => 0
)
[1] => Array
(
[0] => Feb
[1] => 22
)
[2] => Array
(
[0] => Mar
[1] => 18
)
)
Using The foreach Loop:
$sum = 0;
foreach($items as $item) {
$sum += $item['qty'];
}
echo $sum;
Using array_map():
echo array_sum(array_map(
function($item) {
return $item['qty'];
}, $items)
);
Using array_reduce():
echo array_reduce($items, function($carry, $item) {
$carry += $item['qty'];
return $carry;
});

Filling an array with missing values afterwards

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");
}
}

Changing a Key Value Depending on Another Key Value

I have a function like this:
foreach($data as $line) {
if(substr($line,0,1) == "A") {
if(!$first) {
$parts = explode(chr(9),$line);
//echo "<pre>"; print_r($parts); echo "</pre>";
When printed returns multiple arrays like this:
Array
(
[0] => A
[1] => 100_1
[2] => 0
[3] => 1188
[4] => 0
)
Array
(
[0] => A
[1] => 100_2
[2] => 0
[3] => 1188
[4] => 0
)
Array
(
[0] => A
[1] => 100_3
[2] => 0
[3] => 1186
[4] => 0
)
Array
(
[0] => A
[1] => 101_2
[2] => 0
[3] => 1188
[4] => 0
)
Array
(
[0] => A
[1] => 302
[2] => 0
[3] => 1161
[4] => 0
)
Is it possible to 'only' select the keys ([1]) which have values _1,2,3, (if they exist)
and replace the key ([2]) with the same value, so the result would be:
Array
(
[0] => A
[1] => 100_1
[2] => 1
[3] => 1187
[4] => 0
)
Array
(
[0] => A
[1] => 100_2
[2] => 2
[3] => 1188
[4] => 0
)
Array
(
[0] => A
[1] => 100_3
[2] => 3
[3] => 1186
[4] => 0
)
Array
(
[0] => A
[1] => 101_2
[2] => 2
[3] => 1188
[4] => 0
)
Array
(
[0] => A
[1] => 302
[2] => 0
[3] => 1161
[4] => 0
)
I tried to make lots of entries like this but they don't work (I cant get the key right):
foreach($parts as $key => $value) {
if ($value == '100_1') {$parts[$key] = '1';};
if ($value == '100_2') {$parts[$key] = '2';};
if ($value == '100_3') {$parts[$key] = '3';}}
But this would be pure madness as there are over 50 different values and arrays any help would be greatly received.
Something like:
foreach($data as $line) {
if(substr($line,0,1)=="A") {
if(!$first) {
$parts = explode(chr(9), $line);
list($num1, $num2) = explode('_', $parts[1]);
$parts[2] = isset($num2) ? $num2 : $parts[2];
}
}
}

Restructure (filter&flatten) each row of an array

Well, I have this array $city :
[2] => Array
(
[0] => Array
(
[0] => fr
[1] => paris
[2] => a8
)
[1] => 0
[2] => 116729
)
[3] => Array
(
[0] => Array
(
[0] => fr
[1] => marseille
[2] => b8
)
[1] => 0
[2] => 12898
)
[4] => Array
(
[0] => Array
(
[0] => fr
[1] => lyon
[2] => b9
)
[1] => 0
[2] => 8608
)
And so on...
I'm trying to transform it to :
[2] => Array
(
[0] => fr
[1] => paris
[2] => 0
[3] => 116729
)
[3] => Array
(
[0] => fr
[1] => marseille
[2] => 0
[3] => 12898
)
[4] => Array
(
[0] => fr
[1] => lyon
[2] => 0
[3] => 8608
)
And here's how i do it :
foreach ($city as $key => $value){
$city[$key][0] = $city[$key][0][0];
array_splice( $city[$key], 1, 0, $city[$key][0][1] );
}
But the issue here is that the result array :
[2] => Array
(
[0] => fr
[1] => r
[2] => 0
[5] => 0
)
[3] => Array
(
[0] => fr
[1] => r
[2] => 0
[5] => 0
)
[4] => Array
(
[0] => fr
[1] => r
[2] => 0
[5] => 0
)
What is the error in my code and can I fix it?
You can use array_map instead of foreach:
$result = array_map(
function ($v) { return array_merge(array_slice($v[0], 0, 2), array_slice($v, 1)); },
$array
);
Demo
After you do:
$city[$key][0] = $city[$key][0][0];
you can't reference the original $city[$key][0] because you've replaced it. The solution is to save it in a variable first:
foreach ($city as $key => &$value){
$subarray = $value[0];
$value[0] = $subarray[0];
array_splice($value, 1, 0, $subarray[1] );
}
I've also used a reference to avoid having to repeat $city[$key] in the loop.
Try this
foreach($city as $key => $value) {
$city[$key] = [
$value[0][0], //fr
$value[0][1], //lyon
$value[1], //0
$value[2], //8608
];
}
Here you go (provided that your array is called $new in the example below):
foreach ($item as $derp) {
if (is_array($derp)) {
foreach ($derp as $derrrrrr) {
$d[$i][] = $derrrrrr;
}
} else {
$d[$i][] = $derp;
}
}
$i++;
}
?>
Returns
Array
(
[0] => Array
(
[0] => fr
[1] => paris
[2] => a8
[3] => 0
[4] => 11678
)
[1] => Array
(
[0] => fr
[1] => paris
[2] => a8
[3] => 0
[4] => 11678
)
[2] => Array
(
[0] => fr
[1] => paris
[2] => a8
[3] => 0
[4] => 11678
)
)
EDIT
Use the below to ignore the index[2] in the first multi-dimensional array.
$d = array();
$i = 0;
foreach ($new as $item) {
foreach ($item as $derp) {
if (is_array($derp)) {
foreach ($derp as $key => $derrrrrr) {
if ($key !== 2) {
$d[$i][] = $derrrrrr;
}
}
} else {
$d[$i][] = $derp;
}
}
$i++;
}

Categories