How to get value count from multidimentional array? PHP / Laravel
Get the solution ↓↓↓I am trying to count values of this array:
array:5 [в–ј
"layout/theme.liquid" => array:5 [в–¶]
"sections/featured-product.liquid" => array:1 [в–¶]
"sections/header.liquid" => array:2 [в–¶]
"templates/article.liquid" => array:1 [в–¶]
"templates/product.liquid" => array:1 [в–¶]
]
My goal is to count inside this array how many there are arrays? The answer is 10, but stuck on trying to write the code properly.
The array example is being shown bydd(count($jsonLdAssets));
Answer
Solution:
use aforeach
loop and count all of them:
$main_array => array:5 [в–ј
"layout/theme.liquid" => array:5 [в–¶]
"sections/featured-product.liquid" => array:1 [в–¶]
"sections/header.liquid" => array:2 [в–¶]
"templates/article.liquid" => array:1 [в–¶]
"templates/product.liquid" => array:1 [в–¶]
];
$total_count=0;
foreach ($main_array as $arr) {
$total_count+=count($arr);
}
dd($total_count);
//output : 10
Answer
Solution:
You can do this :
$countTotal = 0;
foreach ($jsonLdAssets as $asset) {
$countTotal = $countTotal + count($asset);
}
dd($countTotal);
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: err_ossl_pem_no_start_line
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.