sorting an array in the order of the value of another array in php -
is there better way sort $datas array in order of $tabdocids values ?
foreach ( $tabdocids $ordered_id ) { foreach ( $datas $doc ) if ($doc->docid == $ordered_id) $ordered [] = $doc; } $datas=$ordered;
one way rome...
#for collect $ordered = array_flip($tabdocids);//too keep order $tabdocids array_map(function ($doc) use ($tabdocids,&$ordered){ if(in_array($doc->docid,$tabdocids)){ $ordered [$doc->docid] = $doc; } },$datas); $datas=array_values($ordered);
[updated after comment @kris roofe] sorted.
or without sorting
$datas = array_filter($datas,function ($doc) use ($tabdocids){ return (bool)in_array($doc->docid,$tabdocids); });
Comments
Post a Comment