I am trying to change an indexed array of arrays into a new array structure. My data is as follows: I would like to recreate a new array thats groups them into the same year. The best I can come up with is designating the year as the key so any row that has that year gets added to that
Tag: arrays
Laravel problem with data array from database
I cant figure out how assign variables in controller from array with database datas. this line: dd($user) return this : how can assign variables from array ? Answer You can use extract to extract array keys as PHP variables. Demo: https://3v4l.org/1IRRF
Adding unknown amount of objects to Array via function
So, first off. I’m a C# programmer, so my PHP code is probably strongly influenced by how you would write in C#. Anyway, I’m trying to solve a problem where I’ve got a two-dimensional grid with two …
Convert a json to multi-dimensional PHP array
I have the following json sent to my API endpoint {“key”:”levels”,”value”:”[{role_id:1, access_level_id:3}, {role_id:2, access_level_id:1}, {role_id:3, access_level_id:2}]”,”description”:””} at the backend, I receive it as a Laravel request as follows: And it returns the following expected result array ( ‘key’ => ‘levels’, ‘value’ => ‘[{role_id:1, access_level_id:3}, {role_id:2, access_level_id:1}, {role_id:3, access_level_id:2}]’, ‘description’ => NULL, ) But I need to convert the ‘value’ to
Access data in nested array with PHP
From a web service I am reading an xml file with the following structure I then use the following php code to parse through the data I would expect the code above to display Instead my output is I am real beginner in php and cant really figure this out… Any help please? Answer $productsArray[‘StoreDetails’] is a 2-dimensional array. $value
How to remove specific layer in multi-dimensional array in PHP?
I have a multidimensional array where I would like to remove a specific layer of the data. Basically, I would like to remove all the labels that are numeric aka the [0] => Array, [1] => Array, [2] => Array, and [3] => Array. Here’s the array I have currently: Here’s the array I need: Any help would be appreciated,
What’s the best way to merge to arrays based on key values in each array?
I have these two arrays that I am currently using two loops to merge. I was wondering if there was a better way to merge these arrays: $args = array( ‘Name’ => ‘Test Name’, ‘Email’ => ‘root@…
Grouped collection to associative array
I need to achive array in format showed at the end, I’m using laravel collections $query->get()->groupBy(‘COLOR’)->toArray(); array:2 [ “GREEN” => array:123 [ 0 => array:…
Sort Multiple Array to Numeric order (1,2,…,9,10,11) and Alphabetical order in PHP
How can I sort multidimensional array in numeric order first and then in alphabetical order in PHP. I have one array : { “id”: “220”, “pin_list”: [ { …
Search for an item in inner array
I have an assoc array looking like this (the data comes from user input, this is just an example) How would I look for a specific startingDate for example if I don’t want to loop over every ID. Basically I’m looking for a way to do something like $eventDates[*][“startingDate”] Answer You can achieve this a few different ways, one being