0

i have an array like this ( it has an array inside an array. Each parent array have more that just one child array )

 $value4old = Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [id] => 112
                    [id_tindak_lanjut] => 30.3
                    [nilai_tindak_lanjut] => 0
                    [memo_tindak_lanjut] => 
                    [tindak_lanjut_no] => 1
                    [tgl_tindak_lanjut] => 2018-01-30
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [id] => 111
                    [id_tindak_lanjut] => 30
                    [nilai_tindak_lanjut] => 0
                    [memo_tindak_lanjut] => 
                    [tindak_lanjut_no] => 2
                    [tgl_tindak_lanjut] => 2018-01-07
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [id] => 110
                    [id_tindak_lanjut] => 20.3
                    [nilai_tindak_lanjut] => 0
                    [memo_tindak_lanjut] => 
                    [tindak_lanjut_no] => 2
                    [tgl_tindak_lanjut] => 2017-10-24
                )

        )

    [3] => Array
        (
            [0] => Array
                (
                    [id] => 108
                    [id_tindak_lanjut] => 10.2
                    [nilai_tindak_lanjut] => 0
                    [memo_tindak_lanjut] => 
                    [tindak_lanjut_no] => 1
                    [tgl_tindak_lanjut] => 2018-01-19
                )

            [1] => Array
                (
                    [id] => 109
                    [id_tindak_lanjut] => 30.3
                    [nilai_tindak_lanjut] => 0
                    [memo_tindak_lanjut] => Tes
                    [tindak_lanjut_no] => 2
                    [tgl_tindak_lanjut] => 2018-01-22
                )

        )

)

I tried to remove my parent array ( that one array before the child array ), and leave the child array, so it makes the child array turn into parents array.

what i expect is just like below:

Array
    (
                [0] => Array
                    (
                        [id] => 112
                        [id_tindak_lanjut] => 30.3
                        [nilai_tindak_lanjut] => 0
                        [memo_tindak_lanjut] => 
                        [tindak_lanjut_no] => 1
                        [tgl_tindak_lanjut] => 2018-01-30
                    )
                [1] => Array
                    (
                        [id] => 111
                        [id_tindak_lanjut] => 30
                        [nilai_tindak_lanjut] => 0
                        [memo_tindak_lanjut] => 
                        [tindak_lanjut_no] => 2
                        [tgl_tindak_lanjut] => 2018-01-07
                    )
                [2] => Array
                    (
                        [id] => 110
                        [id_tindak_lanjut] => 20.3
                        [nilai_tindak_lanjut] => 0
                        [memo_tindak_lanjut] => 
                        [tindak_lanjut_no] => 2
                        [tgl_tindak_lanjut] => 2017-10-24
                    )
                [3] => Array
                    (
                        [id] => 108
                        [id_tindak_lanjut] => 10.2
                        [nilai_tindak_lanjut] => 0
                        [memo_tindak_lanjut] => 
                        [tindak_lanjut_no] => 1
                        [tgl_tindak_lanjut] => 2018-01-19
                    )

                [4] => Array
                    (
                        [id] => 109
                        [id_tindak_lanjut] => 30.3
                        [nilai_tindak_lanjut] => 0
                        [memo_tindak_lanjut] => Tes
                        [tindak_lanjut_no] => 2
                        [tgl_tindak_lanjut] => 2018-01-22
                    )

    )

I tried this code

$in = count($value4old)-1; 
$val = array(); 
for ($i=0;$i<=$in;$i++) {   
$val = array_merge($value4old[$i]); 
};
print_r($val); 
echo "</pre>";

but i got like below

Array
(
    [0] => Array
        (
            [id] => 108
            [id_tindak_lanjut] => 10.2
            [nilai_tindak_lanjut] => 0
            [memo_tindak_lanjut] => 
            [tindak_lanjut_no] => 1
            [tgl_tindak_lanjut] => 2018-01-19
        )

    [1] => Array
        (
            [id] => 109
            [id_tindak_lanjut] => 30.3
            [nilai_tindak_lanjut] => 0
            [memo_tindak_lanjut] => Tes
            [tindak_lanjut_no] => 2
            [tgl_tindak_lanjut] => 2018-01-22
        )

)

What could go wrong with my code ?

3 Answers 3

2

There are plenty of ways to do this and your code should work as well, but you made a small error in your array_merge:

You wrote:

$val = array_merge($value4old[$i]);

What you need is

$val = array_merge($val, $value4old[$i]);

Basically you are just doing an assignment every time instead of a merge and you end up with the last value.

Sign up to request clarification or add additional context in comments.

Comments

2
$new = [];
foreach ($value4old as $array) {
    foreach($array as $inner) {
        $new[] = $inner;
    }
}

Comments

1

To remove the outer array (when it has only one value) I always use current().

$array = [
    '0' => ['key' => 'value'];
];

$array = current($array);

Will result in:

array(1) {
    'key' => 'value'
}

For an array with multiple keys that needs to be flattened, I suggest just using a foreach loop.

$newArray = [];
foreach ($array as $key => $value) {
    $newArray[$key] = current($value);
}

2 Comments

array with key "3" have more than one value... it has two value
@Gagantous I guess I've overlooked that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.