I have a multi dimensional array of items which all have a date. The array follows the format: $relatedItems[$year][$month][$day][]. Is there a way of sorting this so that it is in date order. I've tried krsort() bu i think that will just sort the year key e.g. [2014]. Sorry if this is vague I'm new to php.
Array
(
[2014] => Array
(
[01] => Array
(
[13] => Array
(
[0] => Array
(
[type] => news-blog
[title] => Jungle News
[date] => 13th January 2014
[url] => /sport/jungle-ultra/news/jungle-news/
[description] => Description goes here
)
)
[23] => Array
(
[0] => Array
(
[type] => gallery
[title] => New Gallery
[url] => /sport/jungle-ultra/galleries/new-gallery/
[images] => Array
(
[0] => Array
(
[title] => 10.jpg
[src] => /files/cache/978f98c61f63757334fbeec79fabe482_f65.jpg
)
[1] => Array
(
[title] => 9.jpg
[src] => /files/cache/1526727d35b28930b49c69b065a229de_f64.jpg
)
[2] => Array
(
[title] => 8.jpg
[src] => /files/cache/d1b67fefed5e7b7644ad9ec1fe8559ae_f63.jpg
)
[3] => Array
(
[title] => 7.jpg
[src] => /files/cache/80160e395233a15d1b1df4df67ec565d_f62.jpg
)
[4] => Array
(
[title] => 6.jpg
[src] => /files/cache/48575e2d53c487f99633a1105e7a322b_f61.jpg
)
[5] => Array
(
[title] => 4.jpg
[src] => /files/cache/9fe01089fc9656562fb7f082499439fc_f60.jpg
)
)
)
)
[27] => Array
(
[0] => Array
(
[type] => video
[title] => Test
[url] => /sport/jungle-ultra/video/test/
[thumb] => /files/5313/8996/9327/speaker-header-small.jpg
)
)
[24] => Array
(
[0] => Array
(
[type] => video
[title] => Rich reaches half way point
[url] => /sport/jungle-ultra/video/rich-reaches-half-way-point/
[thumb] => /files/7013/9039/4602/video-cover-test.jpg
)
)
[21] => Array
(
[0] => Array
(
[type] => audio
[title] => Day 1 - Update from Alexander Island
[url] => /sport/jungle-ultra/audio/day-1-update-alexander-island/
[thumb] => /files/7013/9039/4602/video-cover-test.jpg
)
)
)
)
)
You are correct in using ksort but you need to use a recursive function. Try using this:
function sort_by_date($dates){
if(is_array($dates)){ // If it is an array
ksort($dates); // Sort the array keys
foreach($dates as $date){ // Loop through the keys
sort_by_date($date); // And call this recursive function on each of the child arrays
}
}
}
Usage:
$array = array(...); // Your date array
sort_by_date($array); // New function for sorting
Related
Been trying to work with two arrays and extract information from the arrays. I can do this with the foreach but I have linked file references (an ID) which targets data in another array.
Array 1 -
Array
(
[type] => land
[id] => 0b1b522e-0cd2-4880-b3ac-a5d86bc2e837
[relationships] => Array
(
[address] => Array
(
[data] => Array
(
[type] => address
[id] => bb89e6c3-9114-4192-a0a7-82c08402d36e
)
)
[details] => Array
(
[data] => Array
(
[type] => details
[id] => f8f0e489-bb86-4857-a815-ab6338d90f26
)
)
[lettingsListing] => Array
(
[data] =>
)
[primaryImage] => Array
(
[data] => Array
(
[type] => media
[id] => 58574088-01a2-45da-a66e-cd3ce1741377
)
)
[images] => Array
(
[data] => Array
(
[0] => Array
(
[type] => media
[id] => 58574088-01a2-45da-a66e-cd3ce1741377
)
[1] => Array
(
[type] => media
[id] => 32d9d605-d55d-48b7-aa62-5e22f762f165
)
[2] => Array
(
[type] => media
[id] => c879656a-a34f-4c93-b6c7-49d3c7b11804
)
[3] => Array
(
[type] => media
[id] => 434f8d79-df19-474d-a275-6c9a5fb0985b
)
[4] => Array
(
[type] => media
[id] => cefccee0-cd30-4c69-9f6f-2bd76116a619
)
[5] => Array
(
[type] => media
[id] => c0377f6c-4279-4176-a5bd-0190b7fd97a8
)
[6] => Array
(
[type] => media
[id] => 6869c65a-bb55-4b9f-8dc1-71ffa5eb84dc
)
[7] => Array
(
[type] => media
[id] => c9b13725-059a-4c96-a8fd-77c9fc9fe05f
)
[8] => Array
(
[type] => media
[id] => edbdddb2-afc7-43ab-9a79-94e0e8e63597
)
[9] => Array
(
[type] => media
[id] => 320de2d7-6ecd-4ec7-9250-f72e228be8a7
)
[10] => Array
(
[type] => media
[id] => 63daaccf-d8d2-4b55-aece-5a01e379fba0
)
[11] => Array
(
[type] => media
[id] => 5d2c253a-03f5-4ea0-b06d-dd17c80d5f3b
)
)
)
)
)
The images in the array are like:
[0] => Array
(
[type] => media
[id] => 58574088-01a2-45da-a66e-cd3ce1741377
)
The ID then corresponds to another array (array 2)
Array
(
[type] => media
[id] => 58574088-01a2-45da-a66e-cd3ce1741377
[attributes] => Array
(
[name] => p048205_01
[order] => 1
[is_featured] =>
[feature_index] =>
[title] =>
[is_image] => 1
[url] => ***urlhere***
)
)
)
So my question is how to get the associated data ([url]) with each ID from the other array. I thought about array_merge but that did not help. My only other thinking is to do a foreach inside the current loop but I have heard it best to not to that?
Create a new array to hold the image urls from what you called array2, but which has the id as the key, so you can jump straight to the URL from your other foreach loop.
$img_urls = []
foreach( $array2 as $a ) {
$img_urls[$a['id']] = $a;
}
Or if you only want the url in this new array
$img_urls = []
foreach( $array2 as $a ) {
$img_urls[$a['id']] = $a['attributes']['url'];
}
As RiggsFolly said, you need to manipulate the second array with images like this:
$new_arr_images = [];
foreach ($arr_images as $value) {
$new_arr_images[$value['id']] = $value['attributes']['url'];
}
// then loop through first array and populate it with image urls based on the ID
I get this array in response from webservice, so How do I read it in a foreach cycle? or some easy way to read it. The [group] they are more than 12 [id]
there is.
Array
( [response] => Array (
[single] => Array ( [parameters] => Array ( [0] => Array ( [name] => msgCode [value] => 0101 )
[1] => Array ( [name] => msgDesc [value] => OK )
[2] => Array ( [name] => status [value] => 1)
[3] => Array ( [name] => message [value] => Normal) ) )
[group] => Array ( [id] => N4BD767 [parameters] => Array ( [0] => Array ( [name] => idFee [value] => 000 ) echo
[1] => Array ( [name] => typeFee [value] => Cuota)
[2] => Array ( [name] => entryDate [value] => 2014-12-17T14:06:47-03:00 )
[3] => Array ( [name] => expirationDate [value] => 2015-12-05T00:00:00-03:00)
[4] => Array ( [name] => amountOrigin [value] => 221980)
[5] => Array ( [name] => surcharges [value] => 1856)
[6] => Array ( [name] => entity [value] => ONLINE)
[7] => Array ( [name] => feeStatus [value] => inicial )
[8] => Array ( [name] => tranNumber [value] => 27) ) ) ) )
Okay guys, this is what I did and it work fine. With no foreach.
$output = array();
array_walk_recursive($result, function($item,$key) use (&$output){
array_push($output,$key,$item);
});
echo var_dump($output);
I know this is considered a somewhat basic PHP Array question but i'm running on 35 hours no sleep and I really just need to finish this up as quickly as posibble so I can get to sleep...sorry just being honest!
In PHP I have this variable $design_values
If I print_r($design_values); this ARRAY it spits out what is show below. It is "Desing" database records.
In this case there is 2 Design records which make up the first 2 Array keyys the 0 and 1
In the application there can be any number of Designs from 0 up to any number.
Now under the 2 Design Records are 24 more Array keys for each of the 2 Design Arrays.
These 24 Array keys are numbered 0 to 23.
Now under each of the 24 Array keys, is 2 keys. One named name and the other named value.
I need to take the Array $design_values and create a new Array of that array. The new Array should be formatted much better amd easy to work with.
So the name and value keys should make up a key => value. The New array should look more like this....
The reason the current Array is a complete nightmare is because that is the Format I get it in from an existing library which returns this Data from an API call.
If someone can help me to manipulate this Array into the desired Array I will be grateful! I have been messing with it for 2 hours with no luck.
Desired New Array Format :
Array
(
[0] => Array
(
['assigned_user_name'] => 'Jason Administrator',
['modified_by_name'] => 'Jason Administrator',
['created_by_name'] => 'Jason Administrator',
['id'] => '4c5c3c08-2b14-9f9c-6cee-542c56cac7b1',
['date_entered'] => '2014-10-01 19:29:32',
....continued for all 24 record items
),
[1] => Array
(
['assigned_user_name'] => 'Jason Administrator',
['modified_by_name'] => 'Jason Administrator',
['created_by_name'] => 'Jason Administrator',
['id'] => '4c5c3c08-2b14-9f9c-6cee-542c56cac7b1',
['date_entered'] => '2014-10-01 19:29:32',
....continued for all 24 record items
)
)
Current Array Format :
Array
(
[0] => Array
(
[0] => Array
(
[name] => assigned_user_name
[value] => Jason Administrator
)
[1] => Array
(
[name] => modified_by_name
[value] => Jason Administrator
)
[2] => Array
(
[name] => created_by_name
[value] => Jason Administrator
)
[3] => Array
(
[name] => id
[value] => 4c5c3c08-2b14-9f9c-6cee-542c56cac7b1
)
[4] => Array
(
[name] => name
[value] => test
)
[5] => Array
(
[name] => date_entered
[value] => 2014-10-01 19:29:32
)
[6] => Array
(
[name] => date_modified
[value] => 2014-10-01 19:29:32
)
[7] => Array
(
[name] => modified_user_id
[value] => 1
)
[8] => Array
(
[name] => created_by
[value] => 1
)
[9] => Array
(
[name] => description
[value] =>
)
[10] => Array
(
[name] => deleted
[value] => 0
)
[11] => Array
(
[name] => assigned_user_id
[value] => 1
)
[12] => Array
(
[name] => chann_channelqms_id_c
[value] =>
)
[13] => Array
(
[name] => channelqms
[value] =>
)
[14] => Array
(
[name] => design_name
[value] =>
)
[15] => Array
(
[name] => design_number
[value] =>
)
[16] => Array
(
[name] => overall_height
[value] =>
)
[17] => Array
(
[name] => overall_width
[value] =>
)
[18] => Array
(
[name] => show_to_customer
[value] => 1
)
[19] => Array
(
[name] => uploadfile
[value] => 2014-09-29_21-57-50.png
)
[20] => Array
(
[name] => nam_channelletterqms_nam_channelletterqms_designs_name
[value] => Test
)
[21] => Array
(
[name] => price_c
[value] =>
)
[22] => Array
(
[name] => shipping_c
[value] =>
)
[23] => Array
(
[name] => totalprice_c
[value] =>
)
)
[1] => Array
(
[0] => Array
(
[name] => assigned_user_name
[value] => Jason Administrator
)
[1] => Array
(
[name] => modified_by_name
[value] => Jason Administrator
)
[2] => Array
(
[name] => created_by_name
[value] => Jason Administrator
)
[3] => Array
(
[name] => id
[value] => 86f21f44-4b21-1826-3592-542c59e4be66
)
[4] => Array
(
[name] => name
[value] => fdtgrfdhg
)
[5] => Array
(
[name] => date_entered
[value] => 2014-10-01 19:41:54
)
[6] => Array
(
[name] => date_modified
[value] => 2014-10-19 19:30:45
)
[7] => Array
(
[name] => modified_user_id
[value] => 1
)
[8] => Array
(
[name] => created_by
[value] => 1
)
[9] => Array
(
[name] => description
[value] =>
)
[10] => Array
(
[name] => deleted
[value] => 0
)
[11] => Array
(
[name] => assigned_user_id
[value] => 1
)
[12] => Array
(
[name] => chann_channelqms_id_c
[value] =>
)
[13] => Array
(
[name] => channelqms
[value] =>
)
[14] => Array
(
[name] => design_name
[value] => design name
)
[15] => Array
(
[name] => design_number
[value] => 313
)
[16] => Array
(
[name] => overall_height
[value] => 22
)
[17] => Array
(
[name] => overall_width
[value] => 22
)
[18] => Array
(
[name] => show_to_customer
[value] => 1
)
[19] => Array
(
[name] => uploadfile
[value] => 2014-09-29_21-57-50.png
)
[20] => Array
(
[name] => nam_channelletterqms_nam_channelletterqms_designs_name
[value] => Test
)
[21] => Array
(
[name] => price_c
[value] =>
)
[22] => Array
(
[name] => shipping_c
[value] =>
)
[23] => Array
(
[name] => totalprice_c
[value] =>
)
)
)
If you can't change it at the source then here is one way (PHP >= 5.5.0 needed for array_column):
foreach($design_values as $key => $values) {
$result[$key] = array_combine(
array_column($values, 'name'),
array_column($values, 'value'));
}
Or possibly, and easier:
foreach($design_values as $key => $values) {
$result[$key] = array_column($values, 'value', 'name');
}
Our use the PHP Implementation of array_column
You should probably create the array you want when making the first array, but if you don't have control over that and just want to conver then something like this should work:
$newArray = array();
foreach($oldArray as $row){
$tmp = array();
foreach($row as $values){
$tmp[$values['name']] = $values['value'];
}
$newArray[] = $tmp;
}
print_r($newArray);
i have big problem, because i don't know how get values from this array where value is be key into new array. This is my source array
Array
(
[0] => Array
(
[ID] => 250602
[NAME] => qwe
)
[1] => Array
(
[ID] => 250603
[NAME] => wer
)
[2] => Array
(
[ID] => 250629
[NAME] => sdf
)
[3] => Array
(
[ID] => 250629
[NAME] => xcv
)
[4] => Array
(
[ID] => 250629
[NAME] => fghfgh
)
[5] => Array
(
[ID] => 250601
[NAME] => pggd
)
[6] => Array
(
[ID] => 250601
[NAME] => dfgdfg
)
[7] => Array
(
[ID] => 250606
[NAME] => dfgdfg
)
)
When id is the same it will be created a new table that will look like for id = 250629
[NAME] => Array
(
[0] => sdf
[1] => xcv
[2] => fghfgh
)
How about foreach loop like this?
<?php
$final_array=array();
foreach($arrays as $sub_arr){ //it will traverse loop for all sub-arrays
$final_array[$sub_arr['ID']][]=$sub_arr['NAME'];
}
print_r($final_array); //you should see expected output.
?>
It will product below output for your given data:
Array
(
[250602] => Array
(
[0] => qwe
)
[250603] => Array
(
[0] => wer
)
[250629] => Array
(
[0] => sdf
[1] => xcv
[2] => fghfgh
)
[250601] => Array
(
[0] => pggd
[1] => dfgdfg
)
[250606] => Array
(
[0] => dfgdfg
)
)
Working Demo
Like this
$by_name = array();
foreach($your_array as $item)
$by_name[$item['ID']] []= $item['name'];
This makes use of php's lazy array initialization ([]= creates a new array implicitly).
If you get your array from mysql, you might also consider GROUP_CONCAT.
I want to transform this array:
Array
(
[0] => Array
(
[group] => site
[key] => date_format
[value] => %d %B %Y - %H:%M:%S
)
[1] => Array
(
[group] => site
[key] => description
[value] => blah
)
[2] => Array
(
[group] => site
[key] => keywords
[value] =>
)
[3] => Array
(
[group] => pages
[key] => permalink
[value] => <page>(/<subpage>)
)
[4] => Array
(
[group] => system
[key] => plugins
[value] => a:1:{i:0;s:5:"pages";}
)
[5] => Array
(
[group] => site
[key] => title
[value] => some title
)
)
... to this ...
Array
(
[system] => Array
(
[plugins] => a:1:{i:0;s:5:"pages";}
)
[site] => Array
(
[date_format] => %d %B %Y - %H:%M:%S
[description] => blah
[keywords] =>
[title] => some title
)
[pages] => Array
(
[permalink] => <page>(/<subpage>)
)
)
Is there any ways to do this without foreach and any other loop?
No you cannot.
And this micro-optimization would be pointless. Just write a simple foreach loop and stop trying to invent a square wheel.