0

I am doing project in Laravel. Given below is my output array

Array ( [0] => Illuminate\Support\Collection Object ( [items:protected] => Array ( [0] => stdClass Object ( [u_id] => f2772366-4e7f-4257-90bd-8ea506dd8f84 ) 1 => stdClass Object ( [u_id] => c0b6da0f-1268-4c0d-a38c-675cc33573a8 ) [2] => stdClass Object ( [u_id] => 15a31589-bba6-4e22-a5c2-1dcd13f43cfe ) [3] => stdClass Object ( [u_id] => a4a05a47-edfe-4f00-aeca-4607897ec760 ) [4] => stdClass Object ( [u_id] => f8b20d31-ac80-4a98-b561-d255c79236fd ) [5] => stdClass Object ( [u_id] => 3901ee9e-01bb-483a-9f74-8f7b76290cd5 ) ) [escapeWhenCastingToString:protected] => ) 1 => Illuminate\Support\Collection Object ( [items:protected] => Array ( [0] => stdClass Object ( [u_id] => 15a31589-bba6-4e22-a5c2-1dcd13f43cfe ) 1 => stdClass Object ( [u_id] => 3901ee9e-01bb-483a-9f74-8f7b76290cd5 ) ) [escapeWhenCastingToString:protected] => ) )

I need to print u_id separated by comma

This is what I have done. showing error

foreach($selectedUsers as $key => $selectedUser){
            $listUser[] = $selectedUser->u_id;
        }

On dd() output like thisenter image description here

  • and what error is it showing? $selectedUser itself seems to be an array. have you tried printing $selectedUser and see what is returned? We need more info if we are to help you. – Mash tan Jun 28 '22 at 03:40
  • @Mashtan Property [u_id] does not exist on this collection instance – Satheesh Kumar Jun 28 '22 at 03:42
  • Related: [How can I fetch the nested relation through a single statement into an array](https://stackoverflow.com/a/69461155/2943403) and [Get data from a multidimensional array, Laravel](https://stackoverflow.com/q/61080458/2943403) – mickmackusa Jun 28 '22 at 05:11
  • Please never present textual data as an image. [Why should I not upload images of code/data/errors when asking a question?](https://meta.stackoverflow.com/q/285551/2943403) – mickmackusa Jun 28 '22 at 05:33
  • 1
    based on dd output image you should try `$flaten_arr = Arr::flatten($listUser); $result = Arr::join($flaten_arr, ',');` – Pragnesh Chauhan Jun 28 '22 at 05:38

1 Answers1

0

After

foreach($selectedUsers as $key => $selectedUser){
        $listUser[] = $selectedUser->u_id;
    }

this foreach, you had an array in the$listUser variable. You can use implode() function to merge them with commas. For eg:

$arr = ['a', 'b', 'c'];
$temp = implode(",", $arr);
echo $temp;

which will give a string a,b,c you can see mode details about implode here

Amal S R
  • 870
  • 7
  • 21
  • The asker already stated in their question that `foreach($selectedUsers as $key => $selectedUser){ $listUser[] = $selectedUser->u_id; }` is "showing error". This means that your answer cannot possibly work. – mickmackusa Jun 28 '22 at 05:45