user1251698
user1251698

Reputation: 2143

PHP get variable value from array

If i print an array $cat i get

Array ( [1] => stdClass Object ( [cat_id] => 1 ..
Array ( [2] => stdClass Object ( [cat_id] => 2 ..

but if I try to get the cat_id

var_dump($cat->cat_id);

How can i do that?

Upvotes: 0

Views: 124

Answers (6)

Akhil Thayyil
Akhil Thayyil

Reputation: 9403

As $cat is an array, you need to use index

var_dump($cat[1]->cat_id);//This will work

Upvotes: 1

Matt Humphrey
Matt Humphrey

Reputation: 1554

If you want to get all of the cat_ids...

foreach ($cat as $row) {
  echo $row->cat_id;
}

Upvotes: 1

Suresh
Suresh

Reputation: 131

foreach($cat AS $singleCat){
   echo $singleCat->cat_id;
}

Upvotes: 3

Matt
Matt

Reputation: 7040

$cat is an array. You need to access the index which contains an object before you can access that object's properties:

var_dump($cat[1]->cat_id);

Upvotes: 1

Damian SIlvera
Damian SIlvera

Reputation: 866

try var_dump($cat[0]->cat_id);

Upvotes: 1

Luca Rainone
Luca Rainone

Reputation: 16458

$cat is an Array.

So,

var_dump($cat[1]->cat_id);

Upvotes: 2

Related Questions