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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧