Help with PHP Multidimensional Array?

Asked

Viewed 28 times

0

I’m reading some API, and faced a situation with array, that I’m having a little trouble, I searched a lot in the forum before asking, however, as I have little experience I’m breaking my head.

The API structure comes this way:

Array
(
    [0] => Array(
            [cidade] => Rio de Janeiro
            [estado] => RJ
        )

)
Array
(
    [0] => Array
        (
            [cidade] => São Paulo
            [estado] => SP
        )

)

I need to take the top content and create contents to turn into a array single, as below:

Array
(
    [0] => Array
        (
            [cidade] => Rio de Janeiro
            [estado] => RJ
        )
    
    [1] => Array
        (
            [cidade] => São Paulo
            [estado] => SP
        )

)

1 answer

0


You can use a array_map for the construction of a new array by the initial model, is a array main and within each other position array, with a simple change function can be made a array representing information with all positions, example:

<?php

    $datas = array(
        array(array('cidade' => 'Rio de Janeiro', 'estado' => 'RJ')),
        array(array('cidade' => 'São Paulo', 'estado' => 'SP'))
    );

    function organize($item) {
        return $item[0];    
    }

    $newDatas = array_map('organize', $datas);

in that capacity function organize($item){}, for example other rules can be applied for each item contained in that array, in case I applied the rule that is in your question.

  • 1

    thank you very novic. helped me a lot!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.