0

hi guys, how to get first and last (key and value) array multidimesional below?

    $data1 = array(
            array(
            '0'=>array(
            '7'=>'chelsea',
            '8'=>'everton',
            '9'=>'villa',
            '10'=>'liverpool'
            ),
            array(
            '23'=>'milan',
            '24'=>'inter',
            '25'=>'juventus',
                ),
                ),
            '1'=>array(
            array(
            '53'=>'madrid',
            '54'=>'barcelona',
            '55'=>'altetico',
            '56'=>'everton',        
            ),
            array(
            '243'=>'milan',
            '244'=>'inter',
            '245'=>'juventus',
                ),
            ),
            '2'=>array(
            array(
            '523'=>'madrid',
            '524'=>'barcelona',
            '525'=>'altetico',
            '526'=>'everton',
            ),
            array(
            '223'=>'milan',
            '224'=>'inter',
            '225'=>'juventus',
                ),
            )
            );

    i want yield like this:

    Array
    (
        [0] => Array
            (
                [0] => Array
                    (
                        [7] => chelsea
                        [10] => liverpool
                    )

                [1] => Array
                    (
                        [23] => milan
                        [25] => juventus
                    )

            )

        [1] => Array
            (
                [0] => Array
                    (
                        [53] => madrid 
                        [56] => everton
                    )

                [1] => Array
                    (
                        [243] => milan
                        [245] => juventus
                    )

            )

        [2] => Array
            (
                [0] => Array
                    (
                        [523] => madrid
                        [526] => everton 
                    )

                [1] => Array
                    (
                        [223] => milan
                        [225] => juventus
                    )

            )

    )
3
Contributors
2
Replies
4
Views
4 Years
Discussion Span
Last Post by diafol
3

get first and last (key and value) array multidimesional

Can you provide the array code so we can understand you better.

3

Sorry to post a putative solution, but the contrivance made an explanation difficult without it:

foreach($data1 as $ktop=>$dtop){
    foreach($dtop as $v){
        $value2 = end($v);
        $key2 = key($v);
        $value1 = reset($v);
        $key1 = key($v);
        $newarray[$ktop][] = array($key1=>$value1,$key2=>$value2);  
    }
}
echo "<pre>";
print_r($newarray);
echo "</pre>";

loop over each item in the outer array and have a nested loop looping - creating and then adding an item to the new array on every iteration
reset moves the pointer of the array to the beginning
end moves the pointer to the last item of the array

Edited by diafol

Votes + Comments
Crystal ball works!
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.