TransWikia.com

PHP get array child

Stack Overflow Asked by tgezginis on November 24, 2021

I want to change last child of array (122) with second child of array (0). You can see with samples. Please help me.

Current version:

Array
(
    [0] => Array
        (
            [122] => Array
                (
                    [PROGRAM_ID] => 181
                    [VENUE_ID] => 2
                    [AUDIT_ID] => 96
                )

        )

)

I want this:

Array
(
    [122] => Array
        (
            [PROGRAM_ID] => 181
            [VENUE_ID] => 2
            [AUDIT_ID] => 96
        )

)

2 Answers

I suppose you're looking for something like this....

https://stackoverflow.com/a/2408971/1172872

Like this:

$result = array();

foreach($array as $inner) {
    $result[key($inner)] = current($inner);        
}

The $result array would now look like this:

Array
(
    [122] => Array
    (
        [PROGRAM_ID] => 181
        [VENUE_ID] => 2
        [AUDIT_ID] => 96
    )
)

Answered by sk8terboi87 ツ on November 24, 2021

This works fine $array = reset($array); for the first child and $array = end($array); for the last.

Answered by Sidux on November 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP