Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

285
Visualizações
php how to convert multi-dimensional array to single array

I have below result from MySQL, PDO search - but I am not able to find suitable answer to make the arrays - to single array, insted of branched array.

   <?php

$dataResult = array("abcdef", "People 1 - 123-456-7890
People 2 - 
People 3 - Abcdef Jack
People 4 _ Defjkl Smack ");


foreach($dataResult as $result){
    if(strstr($result, PHP_EOL)){
        $dataResultArray[] = explode(PHP_EOL, $result);
    } else {
        $dataResultArray[] = $result;
    }

    
}
print_r($dataResultArray);

I am expecting the below result, against what I get is below.

Expected:

abcdef
People 1 - 123-456-7890
People 2 - 
People 3 - Abcdef Jack
People 4 _ Defjkl Smack

Output:

Array
(
    [0] => abcdef
    [1] => Array
        (
            [0] => People 1 - 123-456-7890
            [1] => People 2 - 
            [2] => People 3 - Abcdef Jack
            [3] => People 4 _ Defjkl Smack 
        )

)
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

PHP have a nice function for this case: array_walk_recursive:

// your multi-dimensional-array
$array = [
    'item1' => 'value1',
    'item2' => 'value2',
    'item3' => [
        'item3a' => 'value3',
    ],
    'item4' => [
        [
            'item4a' => [
                'value4',
                'value5',
            ],
        ]
    ]
];

array_walk_recursive($array, function($a) use (&$new) { $new[] = $a; });
print_r($new);

output

Array
(
    [0] => value1
    [1] => value2
    [2] => value3
    [3] => value4
    [4] => value5
)
over 4 years ago · Santiago Trujillo Relatório

0

You can use recusice function to convert multidimensional arrays to single array.


function filter($array)
{
    static $newArray;
    if (is_array($array)):
        array_map('filter', $array);
    else:
        $newArray[] = $array;
    endif;

    return $newArray;
}


$array = [
    'isim' => 'Şahin',
    'soyisim' => 'ERSEVER',
    'yabanci_dil' => [
        'tr' => 'Türkçe',
    ],
    'languages' => [
        [
            'php' => [
                'codeigniter',
                'laravel',
                'symfony'
            ],
            'javascript' => [
                'vuejs',
                'react' => [
                    'react',
                    'react-native'
                ]
            ]
        ]
    ]
];


print_r(filter($array));

Result:

Array
(
    [0] => Şahin
    [1] => ERSEVER
    [2] => Türkçe
    [3] => codeigniter
    [4] => laravel
    [5] => symfony
    [6] => vuejs
    [7] => react
    [8] => react-native
)
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda