使用PHP将数组合并到一个多维数组中

时间:2016-12-09 03:34:18

标签: php arrays multidimensional-array

我正在尝试将数组合并为一个单维多维数组。可能有超过5个阵列需要组合,因此我需要一个代码,无论它们有多少,都会自动组合所有阵列。我尝试了array_merge,但它需要在逗号格式的参数中手动定义数组。

要转换的代码是:

Array
(
    [id] => 1
    [name] => Item 1
    [slug] => item-slug-1
    [parent] => 0
)
Array
(
    [id] => 2
    [name] => Item 2
    [slug] => item-slug-2
    [parent] => 1
)
Array
(
    [id] => 3
    [name] => Item 3
    [slug] => item-slug-3
    [parent] => 2
)
Array
(
    [id] => 4
    [name] => Item 4
    [slug] => item-slug-4
    [parent] => 3
)
Array
(
    [id] => 5
    [name] => Item 5
    [slug] => item-slug-5
    [parent] => 3
)

这就是我希望它的样子:

Array
(
    [0] => Array
        {
            [id] => 1
            [name] => Item 1
            [slug] => item-slug-1
            [parent] => 0
        }

    [1] => Array
        {
            [id] => 2
            [name] => Item 2
            [slug] => item-slug-2
            [parent] => 1
        }

    [2] => Array
        {
            [id] => 3
            [name] => Item 3
            [slug] => item-slug-3
            [parent] => 2
        }

    [3] => Array
        {
            [id] => 4
            [name] => Item 4
            [slug] => item-slug-4
            [parent] => 3
        }

    [4] => Array
        {
            [id] => 5
            [name] => Item 5
            [slug] => item-slug-5
            [parent] => 3
        }
)

以下是数组的生成方式:

  1. 我收到服务器的响应JSON,如下所示:

    [{"slug":"item-slug-1","name":"Item 1","id":1},{"slug":"item-slug-2","name":"Item 2","id":2},{"slug":"item-slug-3","name":"Item 3","id":3,"children":[{"slug":"item-slug-4","name":"Item 4","id":4},{slug":"item-slug-5","name":"Item 5","id":5}]}]

  2. 我解码JSON然后将其转换为这样的数组:

    $categories_obj = json_decode( $_POST['order'] ); $categories_arr = json_decode(json_encode( $categories_obj ), true);

  3. 我创建了一个遍历每个项目的函数,以便更容易插入到我的数据库中:

    function walk_and_update($data, $parent = 0, $count = 0) {
    
      if( is_array($data) ) {
        $combine = array();
        /* The arrays are generated here */
         foreach( $data as $key => $row ) {
            $formatted = array(
                'id'        =>  $row['id'],
                'name'      =>  $row['name'],
                'slug'      =>  $row['slug'],
                'parent'    =>  $parent
            );
    
            print_r( $formatted );
    
             /* My SQL update is here */
    
            if( isset( $row['children'] ) ) {
                walk_and_update( $row['children'], $row['id'], $count );
            }
        }
    }
    

    }

  4. 然后我使用这样的函数:

    walk_and_update( $categories_arr );

2 个答案:

答案 0 :(得分:0)

像这样:

$arr1 = Array(
    'id' => 1,
    'name' => 'Item 1',
    'slug' => 'item-slug-1',
    'parent' => 0);

$arr2 = Array(
    'id' => 2,
    'name' => 'Item 2',
    'slug' => 'item-slug-2',
    'parent' => 1);
$arr3 = Array(
    'id' => 3,
    'name' => 'Item 3',
    'slug' => 'item-slug-3',
    'parent' => 2);
$new_arr = array();    
for($i=1;$i<=3;$i++) {
    $var_name = 'arr'.$i;
    array_push($new_arr,$$var_name);
}    
echo "<pre>";print_r($new_arr);

输出:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Item 1
            [slug] => item-slug-1
            [parent] => 0
        )

    [1] => Array
        (
            [id] => 2
            [name] => Item 2
            [slug] => item-slug-2
            [parent] => 1
        )

    [2] => Array
        (
            [id] => 3
            [name] => Item 3
            [slug] => item-slug-3
            [parent] => 2
        )

)

答案 1 :(得分:0)

你可以创建函数来处理数组,然后将期望的数组返回到多维数组。

首先我知道你要合并的数组,它们是一个一个地生成还是只生成一个可以生成许多数组的数组?

//you could do this if your array produces one time only
$counter = 0;
$new_array=array();
foreach (your array as $key => $val)
{
   $new_array[$counter]['id']= $val['id'];
   $new_array[$counter]['name']= $val['name'];
   $new_array[$counter]['slug']= $val['slug'];
   $new_array[$counter]['parent'] = $val['parent'];
   $counter++;
}



//if your array produces more than one time
function main ()
{
$data['counter']=0;
$data['newarray']=array();

$data = $this->mergin_array(your array,$data['counter'])

}

function merging_array(your array,$counter)
{
   $data = array();
   $counter_new = $counter;
   foreach(your array as $key => $val)
   {
      $data['newarray'][$counter]['id'] = $val['id'];
      $data['newarray'][$counter]['name'] = $val['name'];
      $data['newarray'][$counter]['slug'] = $val['slug'];
      $data['newarray'][$counter]['parent'] = $val['parent'];
      $counter_new++;
   }
   $data['counter'] = $counter_new;

   return $data;

}

所以无论您使用了多少,当您在主函数中访问$ data [&#39; newarray&#39;]时,它将为您提供多个数组的组合