0

I want to get a specific field of an array but it keeps on sending me error like Undefined index: in or Cannot use object of type stdClass as array in.I'm using codeigniter and this array is from my model and it is returned in my controller. Here is my controller.

  $data['inpatient'] = $this->billing_model->retrieveinpatient();
  echo $data['inpatient']['in'];

Here is the structure of my array:

 Array
 (
  [inpatient] => Array
    (
        [0] => stdClass Object
            (
                [iid] => 1
                [in] => IN
            )
    )

)

Any help?

2 Answers 2

2

Look at your structure. The array output is key => value. Your first array has key inpatient. The inpatient value is an array with one key - 0, holding stdClass.

So it must be

echo $data['inpatient'][0]->in;
Sign up to request clarification or add additional context in comments.

1 Comment

it returns Cannot use object of type stdClass as array in
1

The problem is that you are trying to access an object with the array dot notation.

Try this:

echo $data['inpatient'][0]->in;

The $data['inpatient'] is indeed an array, so you can use the index notation. In my example, the index is 0. Once you're pointing to the correct index, you'll have a stdClass Object to work with. In order to extract a property of stdClass Object, you can use the object property notation -> to get at the in property.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.