tgezginis
tgezginis

Reputation: 144

PHP get array child

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
        )

)

Upvotes: 2

Views: 8241

Answers (2)

Sidux
Sidux

Reputation: 557

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

Upvotes: 2

sk8terboi87  ツ
sk8terboi87 ツ

Reputation: 3457

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

Upvotes: 0

Related Questions