doudou3716 2016-12-29 23:18 采纳率: 100%
浏览 33
已采纳

PHP嵌套数组未对齐

I have a PHP loop that is pushing data into an array that will eventually be used to create a list of select options in my dropdown.

I feel like it is kinda close but I am doing something wrong somewhere.

The array of types needs to be part of the category it is associated with.

// Get the list of behavior types
public function _behavior_types()
{

    $cat = $this->global_model->get_behavior_categories();
    $typ = $this->global_model->get_behavior_types();
    $output = array();

    // Loop over our categories
    foreach($cat as $c)
    {
        // Push the category name to an array
        $output[] = $c['categoryName'];

        // Loop over our types
        foreach($typ as $t)
        {   
            // If this type belongs to the category we are currently in, add it to the array
            if($t['categoryID'] == $c['categoryID'])
            {
                array_push($output, $t);
            }
        }

    }

    return $output;
}

I have something out of place however and its causing the array to not be set up in the correct way.

Here is the current output:

Array
(
    [0] => Negative
    [1] => Array
        (
            [categoryID] => 2
            [points] => -3
            [typeID] => 4
            [typeName] => Bad School Day
        )

    [2] => Positive
    [3] => Array
        (
            [categoryID] => 1
            [points] => 2
            [typeID] => 1
            [typeName] => Ate Dinner
        )

    [4] => Array
        (
            [categoryID] => 1
            [points] => 2
            [typeID] => 3
            [typeName] => Brushed Teeth
        )

    [5] => Array
        (
            [categoryID] => 1
            [points] => 3
            [typeID] => 2
            [typeName] => Completed Homework
        )

)

Here is my desired output:

Array
(
    [0] => Negative
        [0] => Array
            (
                [categoryID] => 2
                [points] => -3
                [typeID] => 4
                [typeName] => Bad School Day
            )

    [1] => Positive
        [0] => Array
            (
                [categoryID] => 1
                [points] => 2
                [typeID] => 1
                [typeName] => Ate Dinner
            )

        [1] => Array
            (
                [categoryID] => 1
                [points] => 2
                [typeID] => 3
                [typeName] => Brushed Teeth
            )

        [2] => Array
            (
                [categoryID] => 1
                [points] => 3
                [typeID] => 2
                [typeName] => Completed Homework
            )

)

The dropdown in turn will look like:

Negative
  Bad day at school
Positive
  Ate Dinner
  Brushed Teeth
  Completed Homework
  • 写回答

1条回答 默认 最新

  • dongyu7074 2016-12-29 23:32
    关注

    Your desired output is not really a valid array structure, at least how you have it typed. $output[0] cannot be both a string Negative and an array of options. I suggest making the category the key something like this:

    // Get the list of behavior types
    public function _behavior_types()
    {
    
        $cat = $this->global_model->get_behavior_categories();
        $typ = $this->global_model->get_behavior_types();
        $output = array();
    
        // Loop over our categories
        foreach($cat as $c)
        {
            // Push the category name to an array
            $output[$c['categoryName']] = array();
    
            // Loop over our types
            foreach($typ as $t)
            {   
                // If this type belongs to the category we are currently in, add it to the array
                if($t['categoryID'] == $c['categoryID'])
                {
                    array_push($output[$c['categoryName']], $t);
                }
            }
    
        }
    
        return $output;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历