Skip to content
Advertisement

Tag: arrays

How to group subarrays by a column value?

I have the following array How can I group the array by id? Is there any native php functions are available to do this? While this approach works, I want to do this using a foreach, since with the above I will get duplicate items, which I’m trying to avoid? On the above example id have 2 items, so its

PHP rename the keys of an array

How can I rename keys in an array? Start with this array named $start_array, and change the keys for ‘date’ and ‘revenue’ so you get this $final_array: Here is my terrible attempt which works but is messy. Answer Try the above code.

codeigniter flatten array of query result

I’m using a query in Codeigniter to return the ids of all the rows that belong to a user. This returns Is it possible to return a flat array like ? Answer The CodeIgniter documentation (most particularly, query results) doesn’t list anything that will produce a flat array result (like the PDO::FETCH_COLUMN mode provided for PDOStatement::fetchAll). As such, array results

How to determine max length of a certain array column?

The array looks like this: Can I determine the longest string length of the 3rd column, without having to iterate over the array? In my example the longest string would be “FooBar” – 6 chars. If the inner array had only the string element, I could do max(array_map(‘strlen’, $arr)), but it has 3 items… Answer Add array_map(‘array_pop’, $arr) to the

Advertisement