Skip to content

Tag: arrays

Sort array by 2nd level array count

I have an array that looks like this: I need to be able to count the 2nd level contents and then sort the top level arrays based on these totals. So for example my resultant array would look something like this: I then ideally want to sort by descending order and show only the top 5 results. At present my

How to explode a string and calculate a sum?

I want $total to be output as just a sum of a number but everytime i sum up $total it outputs multiple numbers expected output: Answer There were a few syntax errors in your codes. Other than that, your calculation looks fine, and great job with explode();. Code: Output: You might check out to see if the math…

Echo two multidimensional array with different table

So I have this kind of project to have post divided into several provinces. I have two multidimensional array with table1 and table2, I have been trying to echo it with foreach function and etc but still error. This is my array : I don’t have any idea how to retrieve more than 2 table in my array with. …

using PHP’s null coalescing operator on an array

I am using PHP’s null coalescing operator described by http://php.net/manual/en/migration70.new-features.php. I noticed the following doesn’t produce my expected results which was to add a new phone index to $params whose value is “default”. Why not? Answer You don’t add anything…