arrays - How to sort multikey in php? -
this question has answer here:
- how can sort arrays , data in php? 7 answers
array ( [0] => stdclass object ( [id] => 18 [accno] => 1 [title] => hardware [description] => mobile. [manudate] => 8th july 1942 [muscat] => album [month] => 7 [date] => 8 ) [1] => stdclass object ( [id] => 20 [accno] => 2 [title] => food [description] => apple. [manudate] => 27th july 1942 [muscat] => album [month] => 7 [date] => 27 ) [2] => stdclass object ( [id] => 24 [accno] => 3 [title] => hardware [description] => computer. [manudate] => 2nd july 1942 [muscat] => album [month] => 7 [date] => 2 ) [3] => stdclass object ( [id] => 56 [accno] => 4 [title] => hardware [description] => printer [manudate] => 1942 [muscat] => album [month] => [date] => 0 ) [4] => stdclass object ( [id] => 105 [accno] => 5 [title] => object [description] => chair. [manudate] => 1942 [muscat] => album [month] => [date] => 0 ) )
this array input like
id date title description
0 8th july 1942 hardware mobile 1 27th august 1942 food apple 2 2nd july 1942 hardware computer 3 1942 hardware printer 4 1942 object chair
i want output
id date title description ************************************************************ 3 1942 hardware printer 4 1942 object chair 2 2nd july 1942 hardware computer 0 8th july 1942 hardware mobile 1 27th august 1942 food apple
how sort multikey in php? beginner in php. using following code in php out put not correctly. if 1 of datewise or monthwise sort, output come correctly. otherwise both (datewise or monthwise), output not come correctly. plz solution.
usort($value['year'], function ($a, $b) { if ($a->date == $b->date) return 0; return $a->date < $b->date ? -1 : 1; }); usort($value['year'], function ($a, $b) { if ($a->month == $b->month) return 0; return $a->month < $b->month ? -1 : 1; });
the following code job. strtotime parses text date unix timestamp.
usort($array, function($v1, $v2) { $d1 = strlen($v1->manudate) === 4 ? '01-01-' . $v1->manudate : $v1->manudate; $d2 = strlen($v2->manudate) === 4 ? '01-01-' . $v2->manudate : $v2->manudate; return strtotime($d1) - strtotime($d2); });
Comments
Post a Comment