【问题标题】:PHP create array group by key and valuePHP通过键和值创建数组组
【发布时间】:2018-11-01 05:18:56
【问题描述】:

我在下面的 PHP 代码中有一个数组,我想将此数组转换为按数据值分组。简化数组总是很困难的

     Array
(
    [0] => Array
        (
            [video_id] => 14
            [video_title] => test1
            [video_category_name] => Beginner
            [video_category] => 1
        )

    [1] => Array
        (
            [video_id] => 18
            [video_title] => test
            [video_category] => 1
            [video_category_name] => Beginner
        )

    [2] => Array
        (
            [video_id] => 17
            [video_title] => first
            [video_category] => 3
            [video_category_name] => Mobility
        )

    [3] => Array
        (
            [video_id] => 19
            [video_title] => second
            [video_category] => 3
            [video_category_name] => Mobility
        )
)

我期待这个数组:

(
    [0] => Array
    (

         [ctg_name] => Beginner
          [cat_id] => 1
          [ data]  => Array
                (
                (
                    [0] => Array
                    (

                        [video_id] => 14
                        [video_title] => test1
                        [video_category_name] => Beginner
                        [video_category] => 1
                    )
                    [1] => Array
                    (

                         [video_id] => 18
                        [video_title] => test
                        [video_category] => 1
                        [video_category_name] => Beginner
                    )
                )   

    )
     [1] => Array
    (

          [ctg_name] => Mobility
          [cat_id] => 3
          [ data]  => Array
                (
                    [0] => Array
                    (

                        [video_id] => 17
                        [video_title] => first
                        [video_category] => 3
                        [video_category_name] => Mobility
                    )
                    [1] => Array
                    (

                         [video_id] => 19
                        [video_title] => second
                        [video_category] => 3
                        [video_category_name] => Mobility
                    )
                ) 
    )
)

我试过了,但没有得到我预期的结果。

foreach($video as $val){
            $result[$val['video_category']][] = $val;
        }

返回如下:

     Array
(
    [1] => Array
        (
                    [0] => Array
                    (

                        [video_id] => 14
                        [video_title] => test1
                        [video_category_name] => Beginner
                        [video_category] => 1
                    )
                    [1] => Array
                    (

                         [video_id] => 18
                        [video_title] => test
                        [video_category] => 1
                        [video_category_name] => Beginner
                    )
        )  

    [3] => Array
    (
         [0] => Array
                (

                    [video_id] => 17
                    [video_title] => first
                    [video_category] => 3
                    [video_category_name] => Mobility
                )
                [1] => Array
                (

                     [video_id] => 19
                    [video_title] => second
                    [video_category] => 3
                    [video_category_name] => Mobility
                )


    )
)    

有没有人有一个有效的方法来做到这一点? 请帮我 谢谢

     Array
(
    [0] => Array
        (
            [video_id] => 14
            [video_title] => test1
            [video_category_name] => Beginner
            [video_category] => 1
        )

    [1] => Array
        (
            [video_id] => 18
            [video_title] => test
            [video_category] => 1
            [video_category_name] => Beginner
        )

    [2] => Array
        (
            [video_id] => 17
            [video_title] => first
            [video_category] => 3
            [video_category_name] => Mobility
        )

    [3] => Array
        (
            [video_id] => 19
            [video_title] => second
            [video_category] => 3
            [video_category_name] => Mobility
        )
)

我期待这个数组:

(
    [0] => Array
    (

         [ctg_name] => Beginner
          [cat_id] => 1
          [ data]  => Array
                (
                (
                    [0] => Array
                    (

                        [video_id] => 14
                        [video_title] => test1
                        [video_category_name] => Beginner
                        [video_category] => 1
                    )
                    [1] => Array
                    (

                         [video_id] => 18
                        [video_title] => test
                        [video_category] => 1
                        [video_category_name] => Beginner
                    )
                )   

    )
     [1] => Array
    (

          [ctg_name] => Mobility
          [cat_id] => 3
          [ data]  => Array
                (
                    [0] => Array
                    (

                        [video_id] => 17
                        [video_title] => first
                        [video_category] => 3
                        [video_category_name] => Mobility
                    )
                    [1] => Array
                    (

                         [video_id] => 19
                        [video_title] => second
                        [video_category] => 3
                        [video_category_name] => Mobility
                    )
                ) 
    )
)

我试过了,但没有得到我预期的结果。

foreach($video as $val){
            $result[$val['video_category']][] = $val;
        }

返回如下:

     Array
(
    [1] => Array
        (
                    [0] => Array
                    (

                        [video_id] => 14
                        [video_title] => test1
                        [video_category_name] => Beginner
                        [video_category] => 1
                    )
                    [1] => Array
                    (

                         [video_id] => 18
                        [video_title] => test
                        [video_category] => 1
                        [video_category_name] => Beginner
                    )
        )  

    [3] => Array
    (
         [0] => Array
                (

                    [video_id] => 17
                    [video_title] => first
                    [video_category] => 3
                    [video_category_name] => Mobility
                )
                [1] => Array
                (

                     [video_id] => 19
                    [video_title] => second
                    [video_category] => 3
                    [video_category_name] => Mobility
                )


    )
)    

有没有人有一个有效的方法来做到这一点? 请帮我 谢谢

【问题讨论】:

    标签: php arrays codeigniter


    【解决方案1】:

    试试这个代码

    <?php
    $arr = array(
        0 => array
        (
            'video_id' => 14,
            'video_title' => 'test1',
            'video_category_name' => 'Beginner',
            'video_category' => 1
        ),
        1 => array
        (
            'video_id' => 18,
            'video_title' => 'test',
            'video_category' => 1,
            'video_category_name' => 'Beginner'
        ),
        2 => array
        (
            'video_id' => 17,
            'video_title' => 'first',
            'video_category' => 3,
            'video_category_name' => 'Mobility'
        ),
        3 => array
        (
            'video_id' => 19,
            'video_title' => 'second',
            'video_category' => 3,
            'video_category_name' => 'Mobility'
        )
    );
    echo "<pre>";
    $newArr = array();
    foreach($arr as $k=>$v){
        $newArr[$v['video_category_name']]['ctg_name']=$v['video_category_name'];
        $newArr[$v['video_category_name']]['cat_id']=$v['video_category'];
        $newArr[$v['video_category_name']]['data'][]=$v;
    }
    $newArr1 = array();
    foreach($newArr as $k=>$v){
       $newArr1[] = $v;
    }
    print_r($newArr);
    ?>
    

    输出

    Array
    (
        [Beginner] => Array
            (
                [ctg_name] => Beginner
                [cat_id] => 1
                [data] => Array
                    (
                        [0] => Array
                            (
                                [video_id] => 14
                                [video_title] => test1
                                [video_category_name] => Beginner
                                [video_category] => 1
                            )
    
                        [1] => Array
                            (
                                [video_id] => 18
                                [video_title] => test
                                [video_category] => 1
                                [video_category_name] => Beginner
                            )
    
                    )
    
            )
    
        [Mobility] => Array
            (
                [ctg_name] => Mobility
                [cat_id] => 3
                [data] => Array
                    (
                        [0] => Array
                            (
                                [video_id] => 17
                                [video_title] => first
                                [video_category] => 3
                                [video_category_name] => Mobility
                            )
    
                        [1] => Array
                            (
                                [video_id] => 19
                                [video_title] => second
                                [video_category] => 3
                                [video_category_name] => Mobility
                            )
    
                    )
    
            )
    
    )
    

    【讨论】:

    • 我想要 [0] 代替 [Beginner] 和 [1] 代替 ['Mobility']
    【解决方案2】:

    你可以试试这个。在你的 foreach 循环之后。

    foreach($video as $val){
        $result[$val['video_category']][] = $val;
    }
    $final_arr = [];
    foreach($result as $val){
        $final_arr[] = [
                    'ctg_name'=>$val[0]['video_category_name'],
                    'cat_id'=>$val[0]['video_category'],
                    'data'=>$val
                    ]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-28
      • 2016-04-25
      • 2011-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-13
      • 1970-01-01
      相关资源
      最近更新 更多