0

Hi guys I need a bit of help here,

My variable $a has value [{"id":"[1, 2, 3]"}] of type object, now from this I want the first id i.e. in this case I want 1. How can I fetch ut from variable $a?

When I var_dump it prints as below

class Illuminate\Support\Collection#1230 (1) {
  protected $items =>
  array(1) {
    [0] =>
    class stdClass#1221 (1) {
      public $a =>
      string(9) "[1, 2, 3]"
    }
  }
}

Thank you.

1
  • try ($a[0])->id[1] Commented Oct 21, 2020 at 11:40

2 Answers 2

1

What is the class of $a ?
Is it an extend of Illuminate\Database\Eloquent\Model ?

Maybe this :

$firstId = current($a->id);

EDIT :

I don't see the id on your dump.

current(json_decode($a->first()->id))

or

current(json_decode($a->first()->a))
Sign up to request clarification or add additional context in comments.

2 Comments

I have updated the question Can you check this once
Thanks a lot. You save my day!
0

Can you access ?

echo $a->id; // outputs strings "[1,2,3]"

if yes, then just decode it by using json_decode

$array = json_decode($a->id);
$firstElement = current($array);

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.