Rgd Array推送增量为1

时间:2015-06-10 17:32:37

标签: php arrays

我有一个像这样的数组

 Array
(
    [0] => Grilled n salted
    [1] => Chilli con carne
    [2] => Chilli con carne
    [3] => Grilled n salted
    [4] => Homemade Spanish
    [5] => Yoghurt baked
    [6] => Grilled n salted
    [7] => Slow-cooked dory
)

我想从数组中,我能够形成一个新的数组

[0]
[name]Grilled n salted
[amt]3

[1]
[name]Chilli con carne
[amt]2

[2]
[name]Homemade Spanish
[amt]1

[3]
[name]Yoghurt baked
[amt]1

[4]
[name]Slow-cooked dory
[amt][1]

如何重新组合上面的数组以形成具有名称的数组,并且amt基于前一个数组的重复序列。在顶部

谢谢!

1 个答案:

答案 0 :(得分:1)

$c = array_count_values($arr);
foreach($c as $k => $v) $b[] = array('name' => $k, 'amt' =>$v);

结果

Array
(
    [0] => Array
        (
            [name] => Grilled n salted
            [amt] => 3
        )

    [1] => Array
        (
            [name] => Chilli con carne
            [amt] => 2
        )

    [2] => Array
        (
            [name] => Homemade Spanish
            [amt] => 1
        )

    [3] => Array
        (
            [name] => Yoghurt baked
            [amt] => 1
        )

    [4] => Array
        (
            [name] => Slow-cooked dory
            [amt] => 1
        )

)