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

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -