phparrayscodeigniter

Merge two different 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.

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].


Solution

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