2016-12-29 3 views
1

У меня есть цикл PHP, который толкает данные в массив, который в конечном итоге будет использоваться для создания списка опций select в моем раскрывающемся списке.PHP Вложенный массив неверно

Я чувствую, что это очень близко, но я что-то делаю неправильно.

Массив types должен быть частью ссылки category.

// 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; 
} 

У меня есть кое-что неуместное, и его создание массива не должно быть настроено правильным образом.

Вот выходной ток:

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 
     ) 

) 

Вот мой желаемый результат:

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 
      ) 

) 

Раскрывающийся в свою очередь, будет выглядеть следующим образом:

Negative 
    Bad day at school 
Positive 
    Ate Dinner 
    Brushed Teeth 
    Completed Homework 

ответ

4

Ваш желаемый результат не действительно действительная структура массива, по крайней мере, как вы ее набрали. $output[0] не может быть как строкой Negative, так и множеством опций. Я предлагаю сделать категорию ключной примерно так:

// 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; 
} 
Смежные вопросы