-2

I am having a multidimensional array like this:

Array
(
    [0] => stdClass Object
        (
            [id] => 12
            [userid] => 001


        )

    [1] => stdClass Object
        (
            [id] => 13                
            [userid] => 002

        )

)

I want to display the userid values from both like "001, 002" as a string with comma separated.

GitaarLAB
  • 14,536
  • 11
  • 60
  • 80
denis
  • 351
  • 1
  • 7
  • 16

2 Answers2

2

It is not a multidimensional array, it is an array that contains objects, meaning you access the property inside of the object with an -> , not with []'s --- so to turn this into a list of comma-separated values, do this:

foreach($yourArray as $object){

   $finalString[] = $object->id; 

    }

echo implode(", ", $finalString); 
rm-vanda
  • 3,122
  • 3
  • 23
  • 34
0

If we call your array $arr, then I would use array_map:

$userIds = array_map(function($obj) { return $obj->userid; }, $arr);

If you want to output csv, I would use fputcsv like this (for example):

$handle = fopen('php://output', 'rw');
fputcsv($handle, $userIds);

For more details on exporting to csv, see Export to CSV via PHP

Community
  • 1
  • 1
ymas
  • 474
  • 6
  • 10