简体   繁体   English

合并两个不同的数组

[英]Merge two different array

I am confused with array. 我对数组感到困惑。

What I want to do is two merge two array, but this kind of the two array are different: 我想做的是两个合并两个数组,但是这种两个数组是不同的:

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
                )

        )

)

I want to move the array of [sud] into [sad] array, and named it as quantity. 我想将[sud]数组移动到[sad]数组,并将其命名为数量。

This is my codes which generate the array above: 这是我的代码,生成上面的数组:

        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);

What I want to do here actually is to check if the number of array [stock] => value is not bigger than the number in array [sud]. 我实际上要在此处检查数组[stock] => value的数量是否不大于数组[sud]中的数量。

Please help, thanks in advance. 请帮助,在此先感谢。

If I understood well, you want to change it like this: 如果我了解得很好,您想这样更改它:

 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM