merge and count in multidimensional array - php

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

Related

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!!

Sum specefic values in multidimensional array 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

Counting the values of an array with nesting

I have the following array, which is a load of arrays nested in a bigger array:
Array
(
[0] => Array
(
[vote_for] => 15
)
[1] => Array
(
[vote_for] => 15
)
[2] => Array
(
[vote_for] => 15
)
[3] => Array
(
[vote_for] => 5
)
[4] => Array
(
[vote_for] => 5
)
[5] => Array
(
[vote_for] => 2
)
[6] => Array
(
[vote_for] => 2
)
[7] => Array
(
[vote_for] => 2
)
[8] => Array
(
[vote_for] => 2
)
[9] => Array
(
[vote_for] => 2
)
[10] => Array
(
[vote_for] => 2
)
[11] => Array
(
[vote_for] => 2
)
[12] => Array
(
[vote_for] => 2
)
[13] => Array
(
[vote_for] => 2
)
[14] => Array
(
[vote_for] => 2
)
)
I want to do the equivalent of array_count_values on this array, such that I get 15 => 3, 5 => 2 and 2 => 10. How do I un-nest the arrays to do this?
You may want to try reforming your array to count:
$count_array = array();
foreach ($arr as $v) {
$count_array[] = $v['vote_for'];
}
// Now get the counts
$the_count = array_count_values($count_array);
I think array map will also work for this
$count_array = array_map(function($item) { return $item['vote_for']; }, $array);
$the_count = array_count_values($count_array);

How to split multiple values of keys into separate keys in an Array?

I have this Array
Array (
[0] => Array (
[0] => Array ( [value] => Cooking, [occurence] => 1 )
[1] => Array ( [value] => Music, [occurence] => 1 )
[2] => Array ( [value] => Football,Cooking, [occurence] => 1 )
[3] => Array ( [value] => Travel, [occurence] => 1 )
[4] => Array ( [value] => Cooking,Reading, [occurence] => 2 )
[5] => Array ( [value] => Football,Travel, [occurence] => 1 )
[6] => Array ( [value] => Football, [occurence] => 1 )
[7] => Array ( [value] => Music,Cooking, [occurence] => 1 )
[8] => Array ( [value] => Reading,Travel, [occurence] => 1 )
)
)
The [2], [4], [5], [7] and [8] have 2 values for the key [value].
What I want to do is to split the 2 values of these keys in different keys.
The new values should not go to new Arrays, but they will be added to the similar existing Arrays.
For example, if I break the [2] (Football,Cooking) the result will be that the occurence of [6] (Football) will be incremented by 1 and the [occurence] of [0] (Cooking) will be incremented by 1 also.
Thank you !
Yann
$newdata = array()
foreach($array as $data) { // $array being the inner array with the 9 elements
$keys = explode(',', $data['value']);
foreach ($keys as $subkey) {
$newdata[$subkey]++;
}
}
which would give you something like
$newdata = array(
'Cooking' => 4,
'Football' => 3
etc...
);
Not sure how you want your structure to look afterwards, but at least this'll do the inventory for you.

PHP: Splitting an array into a deeper array

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

Categories