1

I got a data structure like below that is created by laravel collection put() function.

Code:

$array= collect();
$array->put($id, "1");
$array->put($name, "test");

Result:

{
  "id": "1",
  "name": "test"
}

How can I get the value with this syntax $array->id instead of this $array['id']?

thmspl
  • 2,437
  • 3
  • 22
  • 48
Simon Ng
  • 27
  • 4

1 Answers1

1

Check this answers, https://stackoverflow.com/a/56565951/641611

$arr = ['id' => 3];
$arrToObject = (object) $arr;
echo $arrToObject->id;
thmspl
  • 2,437
  • 3
  • 22
  • 48
David Magalhães
  • 750
  • 4
  • 10
  • 27
  • You can read more about [casting to an object](https://www.php.net/manual/en/language.types.object.php#language.types.object.casting) or just [type casting](https://www.php.net/manual/en/language.types.type-juggling.php#language.types.typecasting) in general in PHP. – Bogdan Nov 25 '19 at 17:01