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!!
Related
I have an array
Array
(
[array_name_1] => Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 1
[4] => 1
)
[array_name_2] => Array
(
[5] => 1
[6] => 1
[7] => 1
[8] => 1
[9] => 1
[10] => 1
)
)
I want to merge and calculate the number of arrays above so that it becomes
array
(
[array_name_1] => 5
[array_name_2] => 6
)
can anyone help to provide a solution? thank you
Simply use foreach loop
$finalArr = [];
foreach($yourMainArr as $key => $arr){
$finalArr[$key] = count($arr);
}
print_r($finalArr);
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
)
)
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
)
)
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
I have the following array:
Array(
[0] => 0,0
[1] => 0,1
[2] => 0,2
[3] => 0,3
[4] => 1,0
[5] => 1,1
[6] => 1,2
[7] => 2,0
[8] => 2,1
[9] => 2,2
[10] => 2,3
)
And I would like to split it into the following structure:
Array(
[0] => Array (
[0] => 0
[1] => 1
[2] => 2
[3] => 3
)
[1] => Array (
[0] => 0
[1] => 1
[2] => 2
[3] => 3
)
[2] => Array (
[0] => 0
[1] => 1
[2] => 2
[3] => 3
)
[3] => Array (
[0] => 0
[1] => 1
[2] => 2
[3] => 3
)
i.e., in the "X,Y" value, X corresponds to the position of value "Y" in the new array. I can't seem to get my head around the logic, its my first using 'foreach' in such a way.
Any help would be greatly appreciated pointing me in the right direction.
The input and output arrays in your example don't quite match up, but I'm guessing you're looking for this:
$array = array(…as above…);
$newArray = array();
foreach ($array as $value) {
list($x, $y) = explode(',', $value);
$newArray[$x][] = $y;
}