php - cleaner way to pull dynamic nested elements in multidimensional array for sorting -
is there cleaner way extract nested value 3 level deep multidimensional array, want pull result stacked within 3rd level, hwoever, want maintain dynamic can grab elem 2nd or 4th level using array parameter determine this.
what im trying in end sort using element, cant find way conveniently indicate element chain except way had create myself:
public function keybysubelement($nestedarray, array $subelemstack){ //essentially loop below doing this, dynamic user can specify different nested levels in $subelemstack param. //$nestedvalue = $nestedarray[$subelemstack[0]][$subelemstack[1]]; foreach($subelemstack $nestedelement){ if(isset($nestedvalue) && is_array($nestedvalue)) { $nestedvalue = $nestedvalue[$nestedelement]; } else { $nestedvalue = $nestedarray[$nestedelement]; } } homecoming $nestedvalue; } e.g. utilize method:
assume next data
$searchresults = array( 0 => array( 'title' => 'one', array( 'ratings' => array( 'count' => '1' ) ) ), 1 => array( 'title' => 'two', array( 'ratings' => array( 'count' => '5' ) ) ), 2 => array( 'title' => 'three', array( 'ratings' => array( 'count' => '2' ) ) ), ); foreach($searchresults $k => $v){ $count = $this->keybysubelement($v, array('ratings','count')); $sortdata[$k] = $count; } this gives me this
array(4) { [0]=> int(1) [1]=> int(5) [2]=> int(2) } now have access sub-sub elements value, tied in top level parent key, can utilize sort top level array key using new array $sortdata reference key can reordered sub elements value want sort with. next going re-sort original array new key values or something.
i saw couple potential examples, wasn't able create them work. examples follows:
[php sort: user function][1]
e.g. 1) http://php.net/manual/en/function.sort.php#99419
e.g. 2) sort php multidimensional array sub-value
e.g. 3)
/** * sort 2 dimensional array based on 1 or more indexes. * * msort() can used sort rowset array on 1 or more * 'headers' (keys in 2th array). * * @param array $array array sort. * @param string|array $key index(es) sort array on. * @param int $sort_flags optional parameter modify sorting * behavior. parameter not work when * supplying array in $key parameter. * * @return array sorted array. */ public function msort($array, $key, $sort_flags = sort_regular) { if (is_array($array) && count($array) > 0) { if (!empty($key)) { $mapping = array(); foreach ($array $k => $v) { $sort_key = ''; if (!is_array($key)) { $sort_key = $v[$key]; } else { // @todo should fixed, sorted string foreach ($key $key_key) { $sort_key .= $v[$key_key]; } $sort_flags = sort_string; } $mapping[$k] = $sort_key; } asort($mapping, $sort_flags); $sorted = array(); foreach ($mapping $k => $v) { $sorted[] = $array[$k]; } homecoming $sorted; } } homecoming $array; } e.g. 4)
/** * @param $array * @param $cols * @return array */ public function array_msort($array, $cols) { $colarr = array(); foreach ($cols $col => $order) { $colarr[$col] = array(); foreach ($array $k => $row) { $colarr[$col]['_'.$k] = strtolower($row[$col]); } } $eval = 'array_multisort('; foreach ($cols $col => $order) { $eval .= '$colarr[\''.$col.'\'],'.$order.','; } $eval = substr($eval,0,-1).');'; eval($eval); $ret = array(); foreach ($colarr $col => $arr) { foreach ($arr $k => $v) { $k = substr($k,1); if (!isset($ret[$k])) $ret[$k] = $array[$k]; $ret[$k][$col] = $array[$k][$col]; } } homecoming $ret; }
since info construction homecoming rather ugly , not condusive sorting, first move reformat can sorted. example:
# create new key, 'ratings', , set contents of [0][ratings][count] in foreach ($searchresults &$s) { print_r($s); # utilize keybysubelement function retrieve value here # rather hardcoding $s['ratings'] = $s[0]['ratings']['count']; unset($s[0]); } print_r($searchresults); resulting info structure:
array ( [0] => array ( [title] => 1 [ratings] => 1 ) [1] => array ( [title] => 2 [ratings] => 5 ) [2] => array ( [title] => 3 [ratings] => 2 ) ) it's easy create sort function operate on array sort according value in 'ratings':
# create closure sort given key , in given direction # default order ascending function by_key($key, $dir = 'asc') { homecoming function ($a, $b) utilize ($key, $dir) { if ($a[$key] > $b[$key]) { if ($dir === 'asc') homecoming 1; homecoming -1; } elseif ($a[$key] < $b[$key]) { if ($dir === 'asc') homecoming -1; homecoming 1; } homecoming 0; }; } # sort ratings, descending, using uasort , custom search function: uasort( $searchresults, by_key('ratings','desc') ); # print results foreach ($searchresults $i) { echo $i['title'] . ', ' . $i['ratings'] . php_eol; } array order after sort:
two, 5 three, 2 one, 1 sort title:
uasort( $searchresults, by_key('title') ); output:
one, 1 three, 2 two, 5 php
No comments:
Post a Comment