如何从其他嵌套数组php构建嵌套数组

时间:2015-07-09 11:29:34

标签: php arrays json nested

我有数组,其中每个元素都是sql列的数组。 MAIN数组的一个元素如下所示:

Array(

    [type] => water
    [id] => KK
    [animalName] => "josip"
    [key] => 123
    [key2] => 2
    [alive] => true
    [name] => "Lilly"
    [key] => 1
    [big] => false
)

现在我需要构建嵌套数组,如下所示:

array(

    [type] => water
    [id] => KK
    ["animals"] => array(
                 [animalName] => "josip"
                 [key] => 123
                 [key2] => 1.5
                 [alive] => true
                 ["babies" => array(
                              [name] => "jimmy"
                              [key] => 1
                              [big] => false
                            )
               )

只要在MAIN数组的前一个元素中键入== type我需要添加更多动物,所以

array(

    [type] => water
    [id] => KK
                 ["animals"] => array(
                   [animalName] => "rom"
                   [key] => 213
                   [key2] => 5423
                   [alive] => true
                   ["babies" => array(
                              [name] => "julia"
                              [key] => 199
                              [big] => false
                            )
               )
                 ["animals"] => array(
                   [key] => 123123111
                   [key2] => 123213
                   [alive] => true
                   [name] => "tom"
                   ["babies" => array(
                              [name] => "robert"
                              [key] => 1123
                              [big] => false
                            )
               )

我已经建立了一个算法,但它真的很复杂且不够理想,所以我想寻找尽可能简单的解决方案,任何想法?

重点是让json得到结构:

{
   "type":"water",
   "id":"KK",
   "animals":[
      {
         "AnimalName":"crocodile",
         "key":1,
         "key2":1.1,
         "alive":true,
         "babies":[
            {
               "name":"jimmy",
               "Key":123,
               "big":false
            }
         ]
      },
      {
         "AnimalName":"fish",
         "key":22,
         "key2":1.5,
         "alive":true,
         "babies":[
            {
               "name":"lilly",
               "key2":5,
               "big":false
            }
         ]
      }
   ],
   "type":"land",
   "id":"LL",
   "animals":[
      {
         "AnimalName":"lion",
         "key":1,
         "key2":1.1,
         "alive":true,
         "babies":[
            {
               "name":"jimmy",
               "Key":123,
               "big":false
            }
         ]
      },
      {
         "AnimalName":"dog",
         "key":22,
         "key2":1.5,
         "alive":true,
         "babies":[
            {
               "name":"lilly",
               "key2":5,
               "big":false
            }
         ]
      }
   ]
}

只要在主数组的前一个元素中输入== type,添加更多动物,如果类型与previus不同,则将下一个元素添加到其他类型的根数组中,并添加其他动物。

对不起,如果它有点凌乱

1 个答案:

答案 0 :(得分:0)

试试这个,它对你有帮助..

$array = array(

        'type' => 'water',
        'id' => 'KK',
        'animalName' => "josip",
        'key' => 123,
        'key2' => 2,
        'alive' => 'true',
        'name' => "Lilly",
        'key3' => 1,
        'big' => 'false'
    );
    $resulArray = array();
    foreach ($array as $key => $value) {

        if ($key == 'animalName' || $key == 'key' || $key == 'key2' ||  $key == 'alive') {
            $resulArray['animals'][$key] = $value;
        }
        else if ($key == 'name' || $key == 'key3' || $key == 'big') {
            $resulArray['animals']['babies'][$key] = $value;
        } else {
            $resulArray[$key] = $value;
        }    
    }

    // return array
    echo '<pre>';
    print_r($resulArray);

    // return json 
    $resulJson = json_encode($resulArray);
    echo '<pre>';
    print_r($resulJson);