There is a code:

$string = '524,525,481'; $string_array = explode(',', $string); $q = $db->query("SELECT `tid` FROM `tasks` WHERE `tfrom` = '2' ORDER BY `tid` DESC"); while($d = $db->fetch($q)) { echo $d['tid'].' '; } 

echo $d['tid'] displays:

529 525 524 486 481

And there is an array of $string_array , in which there are values. How to use it to display the numbers in this order?

524 525 481 529 486

    2 answers 2

     $string = '524,525,481'; $string_array = explode(',', $string); $q = $db->query("SELECT `tid` FROM `tasks` WHERE `tfrom` = '2' AND `tid` NOT IN (".$string.") ORDER BY `tid` DESC"); while($d = $db->fetch($q)) { $string_array[] = $d['tid']; } print_r($string_array); 
    • This is nonsense ... - ModaL
    • @ModaL - Yeah, but the decision according to the question ... - Opalosolo
    • still voted in favor. But I would like something better. - ModaL
    • one
      @ModaL, but where is better? By sorting your question has nothing to do. The second option is to add a condition to the output loop if (!in_array($d["tid"], $string_array)) { echo $d["tid"]; } if (!in_array($d["tid"], $string_array)) { echo $d["tid"]; } Another option that produces the same result: `$ string = '524,525,481'; $ string_array = explode (',', $ string); $ string2 = '529,525,524,486,481'; $ string_array2 = explode (',', $ string2); print_r (array_merge ($ string_array, array_diff ($ string_array2, $ string_array))); ` - Indifferent
     for($i=(count($string_array)-1); $i>=0; $i--) { if(($key = array_search(array_pop($string_array[$i],$new_string_array))!== false) { unset($new_string_array[$key]); array_unshift($new_string_array,$string_array[$i]); } }