So I was reading up on sorting arrays in PHP and it makes sense, but they only gave easy examples, so I'm not sure how I would sort something the code below. I want to sort the arrays inside the "School Supplies" and "Kitchen Needs" by the title of the item. How would I do such a thing?
Thanks a lot!
Dan
Array
(
[School Supplies] => Array
(
[0] => Array
(
[item_id] => 1
[title] => Backpack
[parent_id] => 0
[amazon_url] =>
)
[1] => Array
(
[item_id] => 2
[title] => Calculator
[parent_id] => 0
[amazon_url] =>
)
[2] => Array
(
[item_id] => 3
[title] => Erasers
[parent_id] => 0
[amazon_url] =>
)
)
[Kitchen Needs] => Array
(
[0] => Array
(
[item_id] => 13
[title] => Bottle Opener
[parent_id] => 0
[amazon_url] =>
)
[1] => Array
(
[item_id] => 14
[title] => Disposable Plates
[parent_id] => 0
[amazon_url] =>
)
[2] => Array
(
[item_id] => 15
[title] => Disposable Silverware
[parent_id] => 0
[amazon_url] =>
)
)
)
function cmp($x, $y) {
return strcmp($x['title'], $y['title']);
}
usort($array['School Supplies'], 'cmp');
usort($array['Kitchen Needs'], 'cmp');
Related
My PHP array ($array) is as follows:
Array
(
[channels] => Array
(
[0] => Array
(
[position] => 5
[id] => 11
[name] => AFK
)
[1] => Array
(
[position] => 1
[id] => 22
[name] => ARK
)
[2] => Array
(
[position] => 2
[id] => 33
[name] => ESO
)
[3] => Array
(
[position] => 4
[id] => 44
[name] => semi-afk
)
[4] => Array
(
[position] => 0
[id] => 55
[name] => SPACE
)
[5] => Array
(
[position] => 3
[id] => 66
[name] => Tanks & Ships
)
)
[instant_invite] =>
[id] => 123
[members] => Array
(
[0] => Array
(
[username] => Chartographer
[status] => online
[nick] => Chaz Rambone
[avatar_url] => https://cdn.discordapp.com/embed/avatars/0.png
[avatar] =>
[discriminator] => 3270
[id] => 124
)
[1] => Array
(
[username] => Chukers
[status] => online
[mute] =>
[suppress] =>
[deaf] =>
[channel_id] => 789
[game] => Array
(
[name] => The Elder Scrolls Online
)
[avatar_url] => https://cdn.discordapp.com/embed/avatars/1.png
[avatar] =>
[self_deaf] =>
[discriminator] => 9851
[self_mute] =>
[id] => 456
)
)
[name] => TEST
)
I want to sort by "position" starting at 0 and ASC.
I tried some of the examples found here but not getting it yet. Usort and a few others are not working for me. Sure it's syntax but not sure
if ($array->channels) {
usort($array->channels, function($a, $b) {
return $a->position > $b->position ? 1 : -1;
});
As was mentioned in the comments, you're trying to use object notation, but you're referencing an array. The documentation has very helpful examples.
usort($array['channels'], function($a, $b) {
return $a['position'] > $b['position'] ? 1 : -1;
});
See this fiddle example.
I want to convert this array in a single dimensional flat array without losing the sort order.
Array
(
[0] => Array
(
[id] => 1
[title] => Computer
[parent_id] => 0
[children] => Array
(
[0] => Array
(
[id] => 4
[title] => keyboard
[parent_id] => 1
[children] => Array
(
[0] => Array
(
[id] => 6
[title] => Mouse
[parent_id] => 4
[children] => Array
(
[0] => Array
(
[id] => 7
[title] => webcam
[parent_id] => 6
)
)
)
)
)
)
)
[1] => Array
(
[id] => 43
[title] => Mobile
[parent_id] => 0
[children] => Array
(
[0] => Array
(
[id] => 5
[title] => bar phones
[parent_id] => 43
)
[1] => Array
(
[id] => 47
[title] => Touchscreen
[parent_id] => 43
[children] => Array
(
[0] => Array
(
[id] => 41
[title] => Samsung
[parent_id] => 47
)
[1] => Array
(
[id] => 44
[title] => Micromax
[parent_id] => 47
)
[2] => Array
(
[id] => 45
[title] => Huawei
[parent_id] => 47
)
)
)
)
)
[2] => Array
(
[id] => 46
[title] => Camera
[parent_id] => 0
)
[3] => Array
(
[id] => 42
[title] => Heater
[parent_id] => 0
)
)
Give it try with below function:
function makeOneDimensionArray(array $array, &$res = array())
{
foreach($array as $arr)
{
$res[] = array(
'id' => $arr['id'],
'title' => $arr['title'],
'parent_id' => $arr['parent_id']
);
if(isset($arr['children']))
{
makeOneDimensionArray($arr['children'], $res);
}
}
return $res;
}
$finalArr = makeOneDimensionArray($your_array);
print_r($finalArr);
I am trying to merge 2 arrays a single array on a multidimensional array where a given key-value = a value
the first array looks like this:
Array
(
[0] => Array
(
[id] => 4
[subcategories] => Array
(
[0] => Array
(
[id] => 5
[category_order] => 0
[parent_id] => 4
[name] => Audio Equipment
)
[1] => Array
(
[id] => 6
[category_order] => 0
[parent_id] => 4
[name] => Home Entertainment
)
[2] => Array
(
[id] => 7
[category_order] => 0
[parent_id] => 4
[name] => Photography
)
[3] => Array
(
[id] => 8
[category_order] => 0
[parent_id] => 4
[name] => Portable Audio
)
[4] => Array
(
[id] => 9
[category_order] => 0
[parent_id] => 4
[name] => Televisions
)
)
)
)
and the second like this:
Array
(
[0] => Array
(
[id] => 10
[parent_id] => 5
[name] => Amplifiers & Receivers
)
[1] => Array
(
[id] => 11
[parent_id] => 5
[name] => Audio Systems
)
[2] => Array
(
[id] => 12
[parent_id] => 5
[name] => Cassette Decks
)
[3] => Array
(
[id] => 13
[parent_id] => 5
[name] => CD Players
)
[4] => Array
(
[id] => 14
[parent_id] => 5
[name] => Radios
)
[5] => Array
(
[id] => 15
[parent_id] => 5
[name] => HiFi Speakers
)
)
What I want to do is add each of the second arrays to a sub array of the first multidimensional array where the parent_id of the second array = the id of the subcategories array of the first array so it will look like this:
array
(
[0]=> Array
(
[id] => 4
[subcategories] => Array
(
[0] => Array
(
[id] => 5
[category_order] => 0
[parent_id] => 4
[name] => Audio Equipment
[subsubcategories] = array
(
[id] => 10
[parent_id] => 5
[name] => Amplifiers & Receivers
)
)
Something like this should work just rename the array names because you didn't provide them. But I think you'll get the idea :) Mainly you loop through all subcategories with foreach loop or another you'll get the parent id and can access the main array with that parent id and save the sub categories info in there.
foreach( $sub_array as $item ) {
$main_array[ category_id ][ $item[ 'parent_id' ] ][ 'subsubcategories' ] = $item;
}
I have the following array on a hierarchy structure - It's basically an array with categories and its children under the 'child' key:
Array
(
[category_id] => 1
[parent_id] => 0
[name] => Commercial
[child] => Array
(
[0] => Array
(
[category_id] => 48
[parent_id] => 1
[name] => lights
[child] => Array
(
)
)
[1] => Array
(
[category_id] => 12
[parent_id] => 1
[name] => beacons
[child] => Array
(
[0] => Array
(
[category_id] => 91
[parent_id] => 12
[name] => blue beacons
[child] => Array
(
)
)
)
)
)
)
What I am trying to do is write a recursive function to reorganize this array as an ONE LEVEL array only. Instead of having its children inside the 'child' key, I want it to be part of the array root level. Like this:
[0] => Array
(
[category_id] => 1
[parent_id] => 0
[name] => Commercial
)
[1] => Array
(
[category_id] => 48
[parent_id] => 1
[name] => lights
)
[2] => Array
(
[category_id] => 12
[parent_id] => 1
[name] => beacons
)
[3] => Array
(
[category_id] => 91
[parent_id] => 12
[name] => blue beacons
)
Any ideas?
Thanks!
The following recursive function should serve your purpose:
function one_level_array($arr){
$ret = array();
$ret[] = array_diff_key($arr, array("child" => ""));
if(array_key_exists("child", $arr)){
foreach($arr["child"] as $child){
$ret = array_merge($ret, one_level_array($child));
}
}
return $ret;
}
DEMO
This question already has answers here:
Sort an array of associative arrays by column value
(23 answers)
Closed 4 months ago.
I have a PHP array that I need to sort. I have included the example array below. I need to put the top 10 number of URLS plus their perspective counts in a different array. I know I could run into problem if there aren't 10 top matches ... if that happens then a random matching would be fine.
Any suggestions?
I have tried sort(myarray) but that just sorts the first object in the array I need it to sort the second.
Any ideas?
Array
(
[0] => Array
(
[name] => http://bit.ly/2oUTzf
[count] => 1
)
[1] => Array
(
[name] => http://tiny.cc/wyNbi
[count] => 1
)
[2] => Array
(
[name] => http://ow.ly/Almo
[count] => 1
)
[3] => Array
(
[name] => http://bit.ly/7bQ8sY
[count] => 1
)
[4] => Array
(
[name] => http://kissa.be/w4V-
[count] => 5
)
[5] => Array
(
[name] => http://ow.ly/xzwI
[count] => 1
)
[6] => Array
(
[name] => http://twa.lk/L6FZX
[count] => 1
)
[7] => Array
(
[name] => http://tinyurl.com/Alyssa10
[count] => 1
)
[8] => Array
(
[name] => http://www.hiderefer.com/0cz7kNgA.htm
[count] => 1
)
[9] => Array
(
[name] => http://tinyurl.com/Joanie515
[count] => 1
)
[10] => Array
(
[name] => http://ow.ly/uJvB
[count] => 1
)
[11] => Array
(
[name] => http://tinyurl.com/
[count] => 1
)
[12] => Array
(
[name] => http://www.hiderefer.com/wJBUhh3G.htm
[count] => 1
)
[13] => Array
(
[name] => http://short.to/xcxc
[count] => 1
)
[14] => Array
(
[name] => http://bit.ly/t79FA
[count] => 2
)
[15] => Array
(
[name] => http://tinyurl.com/yzy33yl
[count] => 1
)
[16] => Array
(
[name] => http://p.gs/zksz6
[count] => 1
)
[17] => Array
(
[name] => http://bit.ly/7E1cc8
[count] => 1
)
[18] => Array
(
[name] => http://bit.ly/6hbugu
[count] => 1
)
[19] => Array
(
[name] => http://tii.libsyn.com/index.php
[count] => 6
)
[20] => Array
(
[name] => http://tinyurl.com/nlzzwq
[count] => 1
)
[21] => Array
(
[name] => http://bit.ly/7gAdXi
[count] => 1
)
[22] => Array
(
[name] => http://localtweeps.com
[count] => 1
)
[23] => Array
(
[name] => http://localtweeps.com.
[count] => 3
)
[24] => Array
(
[name] => http://scribd.com/doc/22365778
[count] => 1
)
[25] => Array
(
[name] => http://quick-weight-loss-secrets.com/
[count] => 1
)
[26] => Array
(
[name] => http://tinyurl.com/ykd5qm5
[count] => 1
)
[27] => Array
(
[name] => http://bit.ly/5DQ6SO
[count] => 1
)
[28] => Array
(
[name] => http://bit.ly/4z6Kww
[count] => 1
)
[29] => Array
(
[name] => http://bit.ly/40sm9N
[count] => 1
)
[30] => Array
(
[name] => http://bit.ly/8mh7DO
[count] => 5
)
[31] => Array
(
[name] => http://tinyurl.com/krt5yf
[count] => 1
)
[32] => Array
(
[name] => http://bit.ly/7GsthV
[count] => 1
)
[33] => Array
(
[name] => http://bit.ly/1QJzvM
[count] => 1
)
[34] => Array
(
[name] => http://yfrog.com/1durkj
[count] => 1
)
[35] => Array
(
[name] => http://budurl.com/dxwc
[count] => 9
)
[36] => Array
(
[name] => http://digg.com/d1qiCr
[count] => 1
)
[37] => Array
(
[name] => http://bit.ly/eVSIo
[count] => 1
)
[38] => Array
(
[name] => http://yfrog.com/37badgj
[count] => 2
)
[39] => Array
(
[name] => http://tinyurl.com/qh8sos
[count] => 1
)
[40] => Array
(
[name] => http://tinyurl.com/mz7l8d
[count] => 3
)
[41] => Array
(
[name] => http://tinyurl.com/nratac
[count] => 1
)
[42] => Array
(
[name] => http://tinyurl.com/yk587jx
[count] => 1
)
[43] => Array
(
[name] => http://www.bethel.edu/alumni/homecoming/09/events/
[count] => 1
)
[44] => Array
(
[name] => http://www.waytofit.net
[count] => 1
)
[45] => Array
(
[name] => http://twitpic.com/rdcy8
[count] => 1
)
[46] => Array
(
[name] => http://retwt.me/1C1Vd
[count] => 14
)
[47] => Array
(
[name] => http://www.starbucks.com/card
[count] => 1
)
[48] => Array
(
[name] => http://tinyurl.com/yhkbfqe
[count] => 13
)
[49] => Array
(
[name] => http://bit.ly/playspy
[count] => 1
)
[50] => Array
(
[name] => http://bit.ly/57rHLO
[count] => 12
)
You need to write a custom sorting function - like this:
function MyCustomSort($a, $b)
{
if ($a->count == $b->count) {
return 0;
}
return ($a->count < $b->count) ? -1 : 1;
}
Then you pass that function into a sort - like this:
usort($myArray, "MyCustomSort");
You could also write a function to help you sort by website - like this:
function MyCustomSort($a, $b)
{
if ($a->name == $b->name) {
return 0;
}
return ($a->name < $b->name) ? -1 : 1;
}
You may use decorate-sort-undecorate pattern.
<?php
$arr = Array
(
[0] => Array
(
[name] => http://bit.ly/2oUTzf
[count] => 1
)
[1] => Array
(
[name] => http://tiny.cc/wyNbi
[count] => 1
)
[2] => Array
(
[name] => http://ow.ly/Almo
[count] => 1
)
[3] => Array
(
[name] => http://bit.ly/7bQ8sY
[count] => 1
)
...
);
// actual sorting below
$arr= array_map(create_function('$a', 'return array($a["count"], $a);'), $arr); // transform into array of arrays consisted of sort key and item
sort($arr); // sort array of arrays
$arr = array_map('end', $arr); // take only last element from each array
print_r($arr);
How does it work?
Instead of sorting array of your items you sort array of arrays whose last element is item and first is the key by which you want to sort. After sorting you keep only the item.
You can use just sort for sorting array of arrays because PHP compares two same length arrays by comparing its elements one by one.
Sorting by multiple fields
You may use more then one sort key, for example sort by count and if counts are identical take url into account. You can do this by decorating with multiple keys with order of importance like so:
$arr = array_map(create_function('$a', 'return array($a["count"], $a["name"], $a);'), $arr);
Why it's fast
This way might be faster that using usort because it calls your custom code only n-times for sorting array of length n. Comparisons during sort are done using built in comparator so should be fast. In usort method custom comparator is called multiple times (more than n times) during sort and it may slow down things.
function sortByCount($a, $b)
{
if ($a['count'] == $b['count']) {
return 0;
}
return ($a['count'] < $b['count']) ? -1 : 1;
}
usort($myarray, "sortByCount");