-3

別の配列の値に基づいて配列を並べ替える最良の方法は何ですか?

並べ替える配列:

Array
(
    [0] => Array
        (
            [full_name] => Graham Smith
        )

    [1] => Array
        (
            [full_name] => Gerry Jones

        )

    [2] => Array
        (
            [full_name] => Frank Brown
        )

)

注文配列:

(
    [0] => Jones
    [1] => Brown
    [2] => Smith
)
4

2 に答える 2

0

どのソリューションが最適かはわかりませんが、このソリューションの方が理解しやすいことがわかりました。

$array = array(
    array("full_name" => "Jack"),
    array("full_name" => "Bob"),
    array("full_name" => "Graham Smith"),
    array("full_name" => "Gerry Jones"),
    array("full_name" => "Frank Brown")
);

$order = array(
    "Jones",
    "Brown",
    "Smith"
);

$array_ordered = array();
$x = 0;
foreach ($order as &$find) {
    foreach ($array as $key => $row) { 
        $pos = strpos($row['full_name'], $find);
        if ($pos)  {
            $array_ordered[$x] = $array[$key];
        }
    }
    $x++;
}
print_r($array_ordered);
于 2013-05-30T09:06:14.940 に答える