Sum specefic values in multidimensional array PHP - php

I have this array and I want to sum up all Hits which figures at the $day[$key][2]
Array
(
[1] => Array
(
[0] => 01/07/13
[1] => 4
[2] => 4
[3] => 3060
[4] => 1
)
[2] => Array
(
[0] => 02/07/13
[1] => 270
[2] => 757
[3] => 13812810
[4] => 4
)
[3] => Array
(
[0] => 03/07/13
[1] => 5
[2] => 123
[3] => 3894971
[4] => 2
)
[4] => Array
(
[0] => 04/07/13
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
and so on .I've tried this and it doesn't seem to work!
foreach ($day as $key => $value){
$day[$key][2] += $day[$key][2];
}
Any errors in my code ? Thanks

Do it with:
$result = array_reduce($array, function(&$cur, $x)
{
return $cur+=$x[2];
}, 0);
You code also has correct logic, but you shouldn't try to modify existing array values. Just sum up into some result variable.

Something like this ?
$hits=0;
foreach($arr as $k=>$arr)
{
$hits+=$arr[2];
}
echo $hits; //"prints" 1362
Demo

Related

Convert multi-array in 2-D array php

I want to convert a multi array in 2-D array. I have following result of multi array.
Array
(
[1] => Array
(
[0] => Array
(
[0] => Id
[1] => Name
[2] => Fname
[3] => School
[4] => Photo
)
)
[2] => Array
(
[0] => Array
(
[0] => 32
[1] => kamal
[2] => hjhbg
[3] => hnp
[4] => B612_16.jpg
)
)
[3] => Array
(
[0] => Array
(
[0] => 33
[1] => dg
[2] => fa
[3] => f
[4] => bg.jpg
)
)
[4] => Array
(
[0] => Array
(
[0] => 35
[1] => mohit
[2] => bc
[3] => jhbvj
[4] => B612.jpg
)
)
)
Now I need to convert this array in below format.
Array
(
[0] => Array
(
[0] => Id
[1] => Name
[2] => Fname
[3] => School
[4] => Photo
)
[1] => Array
(
[0] => 32
[1] => kamal
[2] => hjhbg
[3] => hnp
[4] => B612_16.jpg
)
[2] => Array
(
[0] => 33
[1] => dg
[2] => fa
[3] => f
[4] => bg.jpg
)
[3] => Array
(
[0] => 35
[1] => mohit
[2] => bc
[3] => jhbvj
[4] => B612.jpg
)
)
Try this
function array_to1d($a) {
$out = array();
foreach ($a as $b) {
foreach ($b as $c) {
if (isset($c)) {
$out[] = $c;
}
}
}
return $out;
}
echo "<pre>"; print_r(array_to1d($array)); // $array your array name
The shortest solution would be using array_walk() here:
array_walk($array, function(&$v) {
$v = $v[0];
});
You just have to do
$two_d_array = array_values($three_d_array);

Multiplying 2 array multidimensional and remove key

I have an multidimensional array which contain some numbers, and i want to multiplying value of array which contain key 0 with each other key inside one area array and erase key 0.
Array
Array
(
[0] => Array
(
[0] => 3
[1] => 5
[2] => 5
[3] => 6
[4] => 7
)
[1] => Array
(
[0] => 2
[1] => 7
[2] => 4
[3] => 2
[4] => 8
)
[2] => Array
(
[0] => 4
[1] => 2
[2] => 3
[3] => 2
[4] => 5
)
)
Here's the result i want
Array
(
[0] => Array
(
[0] => 15
[1] => 15
[2] => 18
[3] => 21
)
[1] => Array
(
[0] => 14
[1] => 8
[2] => 4
[3] => 16
)
[2] => Array
(
[0] => 8
[1] => 12
[2] => 8
[3] => 20
)
)
I was already combine it using foreach and for, but it still not working for me, any idea how to do this?
The solution using array_map and array_slice functions:
// $arr is your initial array
foreach ($arr as &$v) {
$multiplier = $v[0];
$v = array_map(function($val) use($multiplier){
return $multiplier * $val;
}, array_slice($v, 1));
}
print_r($arr);
The output:
Array
(
[0] => Array
(
[0] => 15
[1] => 15
[2] => 18
[3] => 21
)
[1] => Array
(
[0] => 14
[1] => 8
[2] => 4
[3] => 16
)
[2] => Array
(
[0] => 8
[1] => 12
[2] => 8
[3] => 20
)
)

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];
}
}
}

Split an array into mutiple arrays based on a value - PHP

What I've tried so far : I have an array as shown below, what i wanted to do is, create multiple arrays based on the value BEGIN_DAY:
Array
(
[0] => Array
(
[0] => BEGIN_DAY
[1] => 15
)
[1] => Array
(
[0] => 20130701
[1] => 4
[2] => 4
[3] => 3060
[4] => 1
)
[2] => Array
(
[0] => 20130702
[1] => 270
[2] => 757
[3] => 13812810
[4] => 4
)
[3] => Array
(
[0] => 20130703
[1] => 5
[2] => 123
[3] => 3894971
[4] => 2
)
[4] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
[5] => Array
(
[0] => END_DAY
[1] => 15
)
[6] => Array
(
[0] => BEGIN_DAY
[1] => 16
)
[7] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
and so on ...
I want to split this array into multiple different arrays when ever it begin with BEGIN_DAY so it should look like this:
The first :
Array
(
[0] => Array
(
[0] => BEGIN_DAY
[1] => 15
)
[1] => Array
(
[0] => 20130701
[1] => 4
[2] => 4
[3] => 3060
[4] => 1
)
[2] => Array
(
[0] => 20130702
[1] => 270
[2] => 757
[3] => 13812810
[4] => 4
)
[3] => Array
(
[0] => 20130703
[1] => 5
[2] => 123
[3] => 3894971
[4] => 2
)
[4] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
[5] => Array
(
[0] => END_DAY
[1] => 15
)
The second :
[0] => Array
(
[0] => BEGIN_DAY
[1] => 16
)
[1] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
What I've tried so far :
$days=array();
$split_by='BEGIN_DAY';
foreach ($day_all as $key => $value) {
if($day_all[$key][0]==$split_by){
$days=array_slice($day_all, 0,$split_by);
}
}
var_dump($days);
Much Appreciated!
$sk = "first";
foreach ($array as $key=>$value)
{
if(in_array(BEGIN_DAY, $value)&&$key!=0)
{
$sk = "second";
}
$result[$sk][] = $value;
}
echo "<pre>";
print_r($result);
$first = $result['first'];
$second = $result['second'];
Something like this :
$yourArray = ...; //is your array as described above
$finalArray = [];
$tempArray = [];
for($idx = 0; idx<$yourArray .length; idx++){
$tempArray = $yourArray[$idx]
if($yourArray[$idx][0] == 'BEGIN_DAY'){
$finalArray[] = $tempArray;
$tempArray = [];
}
}
The final array will contain the arrays. Each time the BEGIN_DAY is found, the array will be inserted into the finalArray and a new one is created.
To improve the code, check the existence of the cell too, to avoid index exception.

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