0

I'm trying to get the value out of an array in PHP.

The value of $v when displayed with print_r($v) is as follows:

Array ( [0] => Array ([name] => BLARGH ) 
        [1] => Array ( [name] => TEMP CATEGORY ) 
       )

I'm trying to iterate over this and pull out the value of the name key as follows:

foreach($v as $category) {
 echo  $category->name; 
}

The echo returns no value. Further, if I add a print_r($category) to the loop I get a return of

Array ( [name] => TEMP CATEGORY )

How do I get the name value out of the array?

2
  • 1
    Did you do any research at all regarding accessing values of arrays? Commented Jan 10, 2014 at 20:04
  • @OP You can also type cast an array to an object if you wish to retain the -> operation. IE foreach ($v as $category) { $category = (object)$category; echo $category->name; } For this small of a demonstration its a little overkill, but some like to retain the -> method rather than ['name'] clutter. Example: ideone.com/Nxoejf Commented Jan 10, 2014 at 20:09

5 Answers 5

4

inside your foreach loop do $category['name']

Sign up to request clarification or add additional context in comments.

Comments

2

This is an array, not an object. Use array notation:

echo $category['name'];

See here: http://3v4l.org/gPL27

Comments

2
foreach($v as $category) {
   echo  $category['name']; 
}

Comments

2
foreach($v as $category) {

   echo  $category['name']; 

}

what you did wrong:

in this case:

$catagory->name

$category would need to be an object, not an array

Comments

-2
<?php
foreach($v as $key => $value){
    echo $key;
    echo $value;
}
?>

2 Comments

This would echo 1 and Array.
you're right, i read too fast and didn't see it was multidimensional

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.