Count level 3 of an array - php

Is it possible without iterating the array?
$arr = array(
'section1' => array(
5 => array(1, 2, 3),
25 => array(4, 5),
34 => array(10, 12),
),
'section2' => array(
45 => array(1, 42, 3),
64 => array(10, 2, 5, 95),
),
'section3' => array(
5 => array(1, 2, 3, 5, 2),
25 => array(4, 5, 14),
34 => array(17),
),
);
$count = 0;
foreach($arr as $section)
foreach($section as $subsection)
foreach($subsection as $entries)
$count++;
echo $count; // 23
It works but I dont want to iterate trough the entire array just to count some elements...

You can use count().
If you need a total of all elements from all levels:
count($arr, COUNT_RECURSIVE);
If you only need to count the ones on the third level:
foreach($arr as $section)
foreach($section as $subsection)
$count += count($subsection);

Anything's possible right? ;-)
I'd see two ways you could do this without iterating and one would be to evaluate the print_r($main_array) value perhaps counting the "(" to reach the desired depth.
--
The other way would be store the data as JSON, so you can "walk" the tree, plus it's native in javascript so very efficient with dot notation.
"somefield":["anotherfield":["yetanother":value], "woohoo":["yahoo":value]]] (excuse me if not well-formed just on the fly)
Then you parse JSON and reference like: myval = somefield.anotherfield.yetanother; // myval = value

Here's my aproach to this conundrum!
<?php
$arr = array(
'section1' => array(
5 => array(1, 2, 3),
25 => array(4, 5),
34 => array(10, 12),
),
'section2' => array(
45 => array(1, 42, 3),
64 => array(10, 2, 5, 95),
),
'section3' => array(
5 => array(1, 2, 3, 5, 2),
25 => array(4, 5, 14),
34 => array(17),
),
);
function count_all($array)
{
$count = 0;
foreach ($array as $k => $v)
{
if (is_array($v))
{
$count += count_all($v);
}
else {
$count++;
}
}
return $count;
}
echo count_all($arr);

Related

PHP - check if multi array keys exist

How I can check if multi array keys exist?
Example:
$array = array(
array('first_id' => 2, 'second_id' => 4, 'third_id' => 6),
array('first_id' => 3, 'second_id' => 5, 'third_id' => 7)
);
And now I want to check if in array exist row with params:
first_id = 3,
second_id = 5,
third_id = 6.
in this example, I should get no results, becase third_id = 6 is not exist (it exist but with first_id = 2 and second_id = 4).
How I can check it in easy way in PHP?
Thanks.
PHP's native array equality check will return true for arrays that have the same keys and values, so you should just be able to use in_array for this - it will take care of the "depth" automatically:
$set = [
['first_id' => 2, 'second_id' => 4, 'third_id' => 6],
['first_id' => 3, 'second_id' => 5, 'third_id' => 7]
];
$tests = [
['first_id' => 3, 'second_id' => 5, 'third_id' => 7],
['first_id' => 3, 'second_id' => 5, 'third_id' => 6],
['first_id' => 2, 'second_id' => 4, 'third_id' => 6],
['first_id' => 2, 'second_id' => 5, 'third_id' => 6],
];
foreach ($tests as $test) {
var_dump(in_array($test, $set));
}
bool(true)
bool(false)
bool(true)
bool(false)
See https://eval.in/936215
If it's important that the array keys are also in the right order, add the third paramater true to the in_array call. This will use strict equality, rather than loose, and require the arrays to be ordered identically. See the information about equality here: http://php.net/manual/en/language.operators.array.php
You can use isset, array_search, and array_filter
for only one liner try this..
$array = array(
array('first_id' => 2, 'second_id' => 4, 'third_id' => 6),
array('first_id' => 3, 'second_id' => 5, 'third_id' => 7)
);
$first_id = 2;
$second_id = 4;
$third_id = 6;
//check and get array index if exist
$index = array_keys(array_filter($array, function($item) use ($first_id,
$second_id, $third_id) { return $item['first_id'] === $first_id &&
$item['second_id'] === $second_id && $item['third_id'] === $third_id; }));
//print out that array index
print_r($array[$index[0]]);

output of php array in a particular format

am trying to get output of following array in one format. its not getting
<?php
$distance_covered = array( '1_JAN_2017' => array('DRIVER_1' => array(2, 5, 3),'DRIVER_2' => array(3, 2, 6, 9)),
'2_JAN_2017' => array('DRIVER_1' => array(3, 9), 'DRIVER_3' => array(1, 4, 8)),
'3_JAN_2017' => array('DRIVER_4' => array(9), 'DRIVER_1' => array(2, 7, 5, 2)),
'4_JAN_2017' => array('DRIVER_1' => array(5, 3, 3, 2), 'DRIVER_4' => array(4, 9, 8, 5)),
'5_JAN_2017' => array('DRIVER_2' => array(8, 5), 'DRIVER_5' => array(3, 9, 7)),
'6_JAN_2017' => array('DRIVER_5' => array(2, 1, 7, 5), 'DRIVER_4' => array(1, 9, 6)),
'7_JAN_2017' => array('DRIVER_4' => array(5, 2, 9), 'DRIVER_3' => array(4, 1, 6)), );
The above is my array
i want output in the following format
Output: Array ( [DRIVER_1] => 51, [DRIVER_2] => 33, [DRIVER_3] => 24, [DRIVER_4] => 67, [DRIVER_5] => 34 )
this is the sum of distance travelled by each driver in all trips
i tried code like this,anybody knows please help
$res = array();
foreach($distance_covered as $value) {
foreach($value as $key => $number) {
(!isset($res[$key])) ?
$res[$key] = $number :
$res[$key] += $number;
}
}
print_r($res);
?>
This one works for me
$res = array();
foreach($distance_covered as $value)//the array which you have given us
{
foreach($value as $key => $number) //loop over array of date
{
if(!isset($res[$key]))//check if the key exist in over defined array if no then run this
{
$res[$key] = array_sum($number);// Sum all distances of that driver
continue;//set the key and continue the foreach...
}
$res[$key] += array_sum($number);// Sum all distances of that driver
}
}
print_r($res);
die;
And the Output is
Array
(
[DRIVER_1] => 51
[DRIVER_2] => 33
[DRIVER_3] => 24
[DRIVER_4] => 67
[DRIVER_5] => 34
)
This should work:
$res = array();
foreach($distance_covered as $value) {
foreach($value as $key => $number) {
foreach ($number as $n) {
if (isset($res[$key])) {
$res[$key] += $n;
} else {
$res[$key] = $n;
}
}
}
}
print_r($res);
Just traverse through array of arrays.
$distance_covered = array(
'1_JAN_2017' => array('DRIVER_1' => array(2, 5, 3),'DRIVER_2' => array(3, 2, 6, 9)),
'2_JAN_2017' => array('DRIVER_1' => array(3, 9), 'DRIVER_3' => array(1, 4, 8)),
'3_JAN_2017' => array('DRIVER_4' => array(9), 'DRIVER_1' => array(2, 7, 5, 2)),
'4_JAN_2017' => array('DRIVER_1' => array(5, 3, 3, 2), 'DRIVER_4' => array(4, 9, 8, 5)),
'5_JAN_2017' => array('DRIVER_2' => array(8, 5), 'DRIVER_5' => array(3, 9, 7)),
'6_JAN_2017' => array('DRIVER_5' => array(2, 1, 7, 5), 'DRIVER_4' => array(1, 9, 6)),
'7_JAN_2017' => array('DRIVER_4' => array(5, 2, 9), 'DRIVER_3' => array(4, 1, 6)), );
// Counting.
$merged = [];
foreach ($distance_covered as $day => $stats) {
foreach ($stats as $driver => $distances) {
if (!isset($merged[$driver])) {
$merged[$driver] = 0;
}
$merged[$driver] += array_sum($distances);
}
}
// Display.
echo "<pre>";
print_r($merged);
echo "</pre>";
You are close, but...
$res = array ();
foreach ( $distance_covered as $value ) {
foreach ( $value as $key=> $driver ) {
if ( isset($res[$key]) == false ){
$res[$key]=0;
}
$res[$key] += array_sum($driver);
}
}
print_r($res);
The first foreach simply splits the data down to the days.
The second one returns elements like $key = 'DRIVER_1' and $driver = array(3, 9).
If this is the first time you've encountered this driver, then you need to ensure that the element in $res exists, so set it to 0.
Once you know there is an element there, you can add in the sum of the values ( 3 & 9 in this case ) using the += array_sum($driver) bit. The += is simply adding to rather than having to say a=a+b, you can say a+=b.
[sarcastic voice] I can't believe everybody overlooked this convoluted function-based one-liner...
Code: (Demo)
var_export(array_map('array_sum', array_merge_recursive(...array_values($distance_covered))));
Output:
array (
'DRIVER_1' => 51,
'DRIVER_2' => 33,
'DRIVER_3' => 24,
'DRIVER_4' => 67,
'DRIVER_5' => 34,
)
*this is virtually guaranteed to process slower than any other posted answer.
Remove the first level associative keys (date strings) with array_values()
Unpack the array of arrays with the "splat operator" (...) and feed to array_merge_recursive() to group values
Sum the subarray values by calling array_sum() on each subarray with array_map()
(This is merely an exercise of thinking outside the box.)
Beyond that no one suggested using a null coalescing operator, so I'll post what that can look like:
$driver_totals = [];
foreach ($distance_covered as $daily_log) {
foreach ($daily_log as $driver => $distances) {
$driver_totals[$driver] = ($driver_totals[$driver] ?? 0) + array_sum($distances);
}
}
var_export($driver_totals);
And if you have a special scenario where you only need to know the distance for a single specific driver, you can call upon array_column() like this:
$target_driver = 'DRIVER_4';
$total_distance = 0;
foreach (array_column($distance_covered, $target_driver) as $distances) {
$total_distance += array_sum($distances);
}
echo "{$target_driver} drove for a distance of {$total_distance}";
*Notice that the order of the drivers within each date array is inconsequential because array_column() is smart enough to find the desired distance subarray.
Finally, if you declare a whitelist array of all possible drivers, you can:
control the order of the drivers in the output
avoid the iterated isset() conditions
ensure that drivers without any distance records are included in the output
Code:
$roster = ['DRIVER_6', 'DRIVER_5', 'DRIVER_4', 'DRIVER_3', 'DRIVER_2', 'DRIVER_1'];
$driver_totals = array_fill_keys($roster, 0);
foreach ($distance_covered as $daily_log) {
foreach ($daily_log as $driver => $distances) {
$driver_totals[$driver] += array_sum($distances);
}
}
var_export($driver_totals);

Count element in multidimensional array

I have one problem with PHP array. I have the following array:
$arr = array(
1 => array(1, 2),
2 => array(1,2,3),
3 => array(4,array(4,4,4))
);
I want to know how many element in total.
Ex:
echo count($arr); // result: 3
but I want: 7
I want to do this without loop.
Do any one know, please help?
Take one of this
$arr = array(
1 => array(1, 2),
2 => array(1, 2, 3),
3 => array(4, array(4, 4, 4))
);
// iterate over values to find out their size/
var_dump(array_reduce($arr, function($count, $inner_array)
{ return $count + sizeof($inner_array); }, 0));
// merge all value to one big array
var_dump(count(call_user_func_array('array_merge', $arr)));
// create new array with counts of items
var_dump(array_sum(array_map('sizeof', $arr)));
<?php
$arr = array(
1 => array(1, 2),
2 => array(1,2,3),
3 => array(4,array(4,4,4))
);
$sum = 0;
foreach($arr as $a => $b) {
$sum += count($b);
}
echo $sum;
?>
$arr = array(
1 => array(1, 2),
2 => array(1,2,3),
3 => array(4,array(4,4,4))
);
$count = 0;
foreach ($arr as $level) {
$count+= count($level);
}
echo $count;
If your array as follows you can use another method for get 7
$arr = array(
1 => array(1, 2),
2 => array(1,2,3),
3 => array(4,8)
);
echo (count($arr, COUNT_RECURSIVE) - count($arr));

PHP: How to get both key and value from an associative array?

I have this associative array structure:
$multiArray = array(
'key1' => array(1, 2, 3, 4),
'key2' => array(5, 6, 7, 8),
'key3' => array(9, 10, 11, 12)
);
When I call $multiArray['key1'], I get the value (which is normal):
// Example 1
$multiArray['key1'];
//$multiArray only has [1, 2, 3, 4]
Is there a way that when I call I want $multiArray['key1'] that I can have ['key1' => array(1,2,3,4)] or the other two keys, depending on the situation?
I could structure $multiArray like so, but I was wondering if there is a better way.
// Example 2
$multiArray = array(
'keyA' => array('key1' => array(1, 2, 3, 4)),
'keyB' => array('key2' => array(5, 6, 7, 8)),
'keyC' => array('key3' => array(9, 10, 11, 12))
);
$multiArray['keyA'];
// $multiArray is now what I want: ['key1' => [1, 2, 3, 4]]
You could do something like this:
function getArray ($multiArray, $key) {
return array($key => $multiArray[$key]);
}
or
$result = array('key1' => $multiArray['key1']);
A little more context as to how you're using it would be useful though. It sounds like you already know the requested key at the point of use. If not you may need to use array_keys.
I think what you may be looking for is foreach:
$myArray = array('key1' => array(1, 2, 3), 'key2' => array(4, 5, 6));
$multiArray = array();
foreach ($myArray as $key => $value) {
// $key would be key1 or key2
// $value would be array(1, 2, 3) or array(4, 5, 6)
$multiArray[] = array($key => $value);
}

How can I add a $key=>$value pair to an associative array using a foreach loop in PHP?

I have two associative arrays I wish to combine with a foreach loop:
$arr1 = array( 'wikipedia.org' => 11, 'bing.com' => 9, 'google.com' => 8, 'blekko.com' => 7, 'groove.com' => 6, 'blo.com' => 5, 'ekko.com' => 4, 'rokko.com' => 3, 'always.com' => 2, 'popo.com' => 1);
$arr2 = array( 'google.com' => 20, 'blekko.com' => 19, 'wikipedia.org' => 8, 'bing.com' => 7, 'blo.com' => 6, 'ekko.com' => 5, 'groove.com' => 4, 'popo.com' => 3, 'always.com' => 2, 'rokko.com' => 1);
I use a new array
$combined = $arr1;
with a foreach loop
foreach($arr2 as $key=>$value)
{
array_push($combined,$value);
}
... which adds the value but not the key. I think I know why, but cannot find a way to add the key and the value. This works for a single line, but frustratingly nor in a foreach loop!
$combined=array_merge(array('blovk.com'=>'44'),$combined);
$aggregatedResults[$key] = $value;
It should be that simple...

Categories