【问题标题】:Array reduce with associative array使用关联数组减少数组
【发布时间】:2017-12-20 22:25:01
【问题描述】:

我有一个这样的数组

 $filter_array = Array
    (
        [0] => Array
            (
                [fv_id] => 1
                [fv_value] => Red
                [filter_id] => 1
                [filter_name] => Color
            )

        [1] => Array
            (
                [fv_id] => 2
                [fv_value] => Blue
                [filter_id] => 1
                [filter_name] => Color
            )

    )

我想通过在数组顶部添加 filter_namefilter_id 来减少数组,这对于所有数组都是相似的。

$newArray = array_reduce($filter_array,function($carry,$item){

            $allFilterValues[] = array(
                'fv_id' => $item['fv_id'],
                'fv_value' => $item['fv_value'],
            );

             $formated_array = array(
                'filter_id' => $item['filter_id'],
                'filter_name' => $item['filter_name'],
                'filter_values' => $allFilterValues
            );
            return $formated_array;
        });

但我只是在 filter_values

上获得最后一个数组迭代值
Array
(
    [filter_id] => 1
    [filter_name] => Color
    [filter_values] => Array
        (
            [0] => Array
                (
                    [fv_id] => 2
                    [fv_value] => Blue
                )

        )

)

但我希望数组是这样的。

Array
(
    [filter_id] => 1
    [filter_name] => Color
    [filter_values] => Array
        (
                [0] => Array
                (
                    [fv_id] => 1
                    [fv_value] => Red
                ),

                [1] => Array
                (
                    [fv_id] => 2
                    [fv_value] => Blue
                )

        )

)

【问题讨论】:

  • 如果你在filter_id上合并,你为什么放弃它作为一个数组键?您确定您拥有应有的键名吗?在我看来,您应该在每个子组之前使用 filter_idfilter_name 作为键。
  • array_reduce 回调的每次迭代中,您应该返回$carry

标签: php array-reduce


【解决方案1】:

array_reduce回调函数的每次迭代必须返回当前$carry值:

$newArray = array_reduce($filter_array,function($carry,$item){
    // create key so as to distinct values from each other
    $key = $item['filter_id'] . '-' . $item['filter_name'];

    // check if created key exists in `$carry`, 
    // if not - we init it with some data
    if (empty($carry[$key])) {
        $carry[$key] = [
            'filter_id' => $item['filter_id'],
            'filter_name' => $item['filter_name'],
            'filter_values' => []
        ];
    }

    // add values to `filter_values`
    $carry[$key]['filter_values'][] = [
        'fv_id' => $item['fv_id'],
        'fv_value' => $item['fv_value'],
    ];

    return $carry;
}, []);

// if you want to reindex `$newArray` from 0:
$newArray = array_values($newArray);

更新:当且仅当您的$filter_array'filter_id''filter_name'总是相同时,您可以简化代码:

$newArray = [];
$first = true;
foreach ($filter_array as $item) {
    if ($first) {
        $first = false;
        $newArray = [
            'filter_id' => $item['filter_id'],
            'filter_value' => $item['filter_name'],
            'filter_values' => []
        ];
    }

    $newArray['filter_values'][] = [
        'fv_id' => $item['fv_id'],
        'fv_value' => $item['fv_value'],
    ];
}
echo'<pre>',print_r($newArray),'</pre>';

【讨论】:

  • 还有其他数组函数比这更容易吗?
  • 我想没有。但是您可以将其重写为foreach
  • 你能告诉我为什么你在array_reduce的回调参数之后传递了[]?
  • 为简化代码创建了一个小更新。至于你的问题 - 我将[] 作为$carry 的初始值传递。当然,php可以决定你使用$carry的初始值,所以这只是我个人的做法。
猜你喜欢
  • 2020-12-07
  • 2014-09-12
  • 1970-01-01
  • 2023-02-24
  • 1970-01-01
  • 2022-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多