Skip to content
Advertisement

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 is right.

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. So

PHP mixed associative array how to get values in foreach loop

Well I have something like meaning that “a3” and “a4” are keys without values. I need to go through a foreach loop to get $key => $value pairs. Should be something with checking of isset($value) but it doesn’t work. UPDATE: Question is closed. Thanks to all. As it was written key without value is not a key, but value with

PHP array slice from position + attempt to return fixed number of items

I’m looking for an efficient function to achieve the following. Let’s say we have an array: Slicing from a position should always return 5 values. 2 before the position index and 2 values after the position index – and of course, the position index itself. If a position index is at the beginning of the array i.e. 0 (example 2),

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 to params. Your given code simply generates an unused return value: Thus, you will still have to set it:

Advertisement