Home » Php » php – How to move array elements to top if compared with string?

php – How to move array elements to top if compared with string?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I have an array $result getting from mysql as follows

    Array
(
    [0] => Array
        (
            [p_title] => Apple The New iPad (White, 64GB, WiFi)
        )
    [1] => Array
        (
            [p_title] => Apple ipad Mini/ipad Mini Retina Belkin Fastfit Bluetooth Wireless Key
        )
    [2] => Array
        (
            [p_title] => Apple ipad Air (16GB, WiFi + Cellular)
        )
)

and suppose I am getting sort by value in $sort_by variable.
for ex. currently,

$sort_by="Apple ipad";

so I want to move each array elements who have p_title “Apple ipad” to top.

so my output array should be;

Array
(
    [0] => Array
        (
            [p_title] => Apple ipad Air (16GB, WiFi + Cellular)
        )
    [1] => Array
        (
            [p_title] => Apple ipad Mini/ipad Mini Retina Belkin Fastfit Bluetooth Wireless Key
        )
    [2] => Array
        (
            [p_title] => Apple The New iPad (White, 64GB, WiFi)
        )
)

I am ready to edit the code either in mysql query or in php.

How to&Answers:

Use usort():

 function sortx($a, $b) {
    if(strpos($a['p_title'],'Apple ipad')!==false){
        return -1;
    }
    return 1;
}

usort($array, 'sortx');

Whenever the preceding value will contain this string, it will be pushed towards the beginning of the array.

If you want to use variable in usort() function, you need to use objects:

class SortTitles{
    public $string;
    function sortx($a, $b) {
        if(strpos($a['p_title'],$this->string)!==false){
            return -1;
        }
        return 1;
    }
    public function sort_titles($array){
        usort($array, 'self::sortx');
        return $array;
    }

}
$sort = new SortTitles;
$sort->string = 'Apple ipad';
$array = $sort->sort_titles($array);

Answer:

function sortByTitle($title, $array)
{
  $result = $array;
  for($i=0;$i<count($array);$i++)
  {
    if(isset$array[$i][p_title]) and strpos($array[$i][p_title],$title)!==false)
    {
      unset($result[$i]);
      array_unshift($result,$array[$i]);
    }
  }
  return $result;
}

The array_unshift() function inserts new elements to an array. The new
array values will be inserted in the beginning of the array.

Answer:

This code returns correct result:

    $result = array(
        array('p_title' => 'Apple The New iPad (White, 64GB, WiFi)'),
        array('p_title' => 'Apple ipad Mini/ipad Mini Retina Belkin Fastfit Bluetooth Wireless Key'),
        array('p_title' => 'Apple ipad Air (16GB, WiFi + Cellular)'),
    );
    $patterned = array();
    $another = array();
    $sort_by='Apple ipad';
    foreach ($result as $data) {
        foreach ($data as $key => $value) {
            if (strpos($value, $sort_by) === 0) {
                $patterned[][$key] = $value;
            } else {
                $another[][$key] = $value;
            }
        }
    }
    sort($patterned);
    $sorted_restult = array_merge($patterned, $another);