合并两个不同的数组

时间:2015-03-11 09:45:19

标签: php arrays

我对数组感到困惑。

我想要做的是两个合并两个数组,但这两个数组是不同的:

Array
(
    [0] => Array
        (
            [sud] => 60
            [sad] => Array
                (
                    [incharge] => Perusahaan
                    [perusahaan_id] => 1
                    [barang_id] => 3
                    [gudang_id] => 2
                    [stock] => 1
                )

        )

    [1] => Array
        (
            [sud] => 23
            [sad] => Array
                (
                    [incharge] => Perusahaan
                    [perusahaan_id] => 1
                    [barang_id] => 4
                    [gudang_id] => 1
                    [stock] => 2
                )

        )

)

我想将[sud]的数组移动到[sad]数组中,并将其命名为quantity。

这是我生成上面数组的代码:

        if($q->num_rows() > 0)
            {
                foreach ($q->result() as $row => $rows)
                    {
                        $data[] = $rows;
                        $stock[] = $rows->stock;
                    }
            }           
            $i = -1;
            foreach ($update as $updates)
                {
                    $i++; 
                    $test3['sud'] = $stock[$i];
                    $test3['sad'] =  $updates; 
                    $happy[] = $test3; 
                }
                    print_r ($happy);

我想在这里做的实际上是检查数组[stock] => value的数量是否不大于数组[sud]中的数字。

请提前帮助,谢谢。

1 个答案:

答案 0 :(得分:1)

如果我理解得很好,你想改变它:

 if($q->num_rows() > 0)
            {
                foreach ($q->result() as $row => $rows)
                    {
                        $data[] = $rows;
                        $stock[] = $rows->stock;
                    }
            }           
            $i = -1;
            foreach ($update as $updates)
                {
                    $i++; 

                    $test3['sad'] =  $updates; 
                    $test3['sad']['quantity'] =  $stock[$i]; 
                    $happy[] = $test3; 
                }
                    print_r ($happy);