使用特定值分隔数组

时间:2017-11-22 10:12:00

标签: php arrays

我承诺了一个旅游网站的项目。在酒店页面客户要求hotles将列出相应的地方.Exp(A和B是两个地方,其ID分别为1和2。当列出A下的酒店第一列表酒店之后B)。我有一个PHP数组我试图将这个数组与地方分开,这里有3个不同的地方是1,9,10 一个数组只有place == 1和第二个数组只有place == 9和3rd array place == 10.

这是我的阵列:

Array(

    [0] => Array
        (
            [id] => 4
            [name] => ARCADIA REGENCY
            [place] => 1

        )

    [1] => Array
        (
            [id] => 5
            [name] => HAVELI BACKWATER RESORT
            [place] => 1

        )

    [2] => Array
        (
            [id] => 6
            [name] => RAMADA ALLEPPEY
            [place] => 1

        )


    [3] => Array
        (
            [id] => 21
            [name] => ORCHID HIGHLANDS
            [place] => 9

        )

    [4] => Array
        (
            [id] => 22
            [name] => PLUM JUDY
            [place] => 9

        )

    [5] => Array
        (
            [id] => 23
            [name] => THE PANORAMIC GETAWAY
            [place] => 9

        )

    [6] => Array
        (
            [id] => 24
            [name] => TEA CASTLE
            [place] => 9

        )




    [7] => Array
        (
            [id] => 52
            [name] => PERIYAR MEADOWS
            [place] => 10

        )

    [8] => Array
        (
            [id] => 53
            [name] => THE ELEPHANT COURT
            [place] => 10

        )
)

我希望将此数组与地点分开

1 个答案:

答案 0 :(得分:0)

$arr = array();

foreach($old_arr as $key => $item)
{
    if(array_key_exists('place', $item))
        $arr[$item['place']][$key] = $item;
}

ksort($arr, SORT_NUMERIC);

但是,如果没有Place,它将丢弃内部数组。