I have a string that will be exploded to get an array, and as we know, the output array key will start from 0 as the key to the first element, 1 for the 2nd and so on. Now how to force that array to start from 1 and not 0? It’s very simple for a typed array as we
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
Sort an array of alphabetic and numeric string-type elements ASC, but with numeric elements after alphabetic elements
I have an array of values which are either all-letters or all-numbers and need to sort them in an ascending fashion. Additionally, I want all-numeric values to be moved to the end of the array so that they occur after all of the non-numeric values. If I run sort() on it I get: but I’d like to see: I tried
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
Insert elements from one array (one-at-a-time) after every second element of another array (un-even zippering)
What would be an elegant way to merge two arrays, such that the resulting array has two items from the first array followed by a single item from the second array, repeating in this fashion? Desired result: I’m trying to do it using a for loop with multiple counters, but I don’t know that the array lengths will be. I’m
Flip associative array and store new values in subarrays to prevent losing duplicated values
I have a flat associative array which may contain duplicate values. I need to restructure the data to store the original values as new keys and the original keys pushed into subarrays associated with the new keys. Answer
PHP $_SESSION for multiple users at once
I’m wondering about how the $_SESSION array works. If I have a lot of users using my site do I need to set a subarray for each user? For instance right now I have $_SESSION[‘userid’] = $userid; $…
replace array keys with given respective keys
I have an array like below I have another array having keys to replace with key information. I need to replace all keys of array $old with respective values in array $keyReplaceInfo. Output should be like this I had to do it manually as below. I am expecting better option. can anyone suggest better way to accomplish this? I know
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