合并两个不同的数组

问题描述 投票:0回答:1

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

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]中的数量。

php arrays codeigniter
1个回答
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);
© www.soinside.com 2019 - 2024. All rights reserved.