0

I have this multidimensional array coming from the database:

Array
(
    [0] => Array
        (
            [0] => Thing1
        )

    [1] => Array
        (
            [0] => Thing2
        )

)

I need to extract the data like this: Thing1, Thing2

I tried a lot of methods, but no one works. How can I do that?

GMB
  • 216,147
  • 25
  • 84
  • 135
Devmiki
  • 49
  • 1
  • 6

2 Answers2

0

Assuming the data you want is always in the 0 index of the nested array, you could iterate over the array and extract those elements:

$result = array();
foreach ($arr as $value) {
    $result[] = $value[0];
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

Use the built in array_column()

<?php
$data = [
    ['Thing1'],
    ['Thing2']
];

print_r(array_column($data, 0));

Result:

Array
(
    [0] => Thing1
    [1] => Thing2
)

I need to extract the data like this: Thing1, Thing2

<?php
echo implode(', ', array_column($data, 0));
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106