0

I have this array which I intend to convert to an object for ease of use in my laravel blade but accessing the properties keeps throwing errors.

$applicationDetail = (object) array([
      'completedApplication' => 3,
      'incompleteApplication' => 4,
      'totalApplication' => 5,
  ]);

dd($applicationDetail->completedApplication);

Running the code block gives the error:

Undefined property: stdClass::$completedApplication

What am I doing wrong please?

mykoman
  • 1,715
  • 1
  • 19
  • 33

2 Answers2

1

I don't know what format in your array. It seems that the square brackets are not necessary. Try:

$applicationDetail = (object) array(
        'completedApplication' => 3,
        'incompleteApplication' => 4,
        'totalApplication' => 5,
    );
dd($applicationDetail->completedApplication);
Phil
  • 1,444
  • 2
  • 10
  • 21
1

Do it like

  $applicationDetail = (object) [
      'completedApplication' => 3,
      'incompleteApplication' => 4,
      'totalApplication' => 5,
  ];

dd($applicationDetail->completedApplication); // will give output 3

You are making a 2d Array, if you do

$applicationDetail = (object) array([
      'completedApplication' => 3,
      'incompleteApplication' => 4,
      'totalApplication' => 5,
  ]);
print_r($applicationDetail);

It will show the output

stdClass Object
(
    [0] => Array
        (
            [completedApplication] => 3
            [incompleteApplication] => 4
            [totalApplication] => 5
        )

)
bhucho
  • 3,903
  • 3
  • 16
  • 34