0

Have array

Array ( 
    [3] => 
    stdClass Object ( 
        [term_id] => 3 
        [name] => Lietuviu 
        [slug] => lietuviu 
        [term_group] => 0 
        [term_taxonomy_id] => 3 
        [taxonomy] => kalba 
        [description] => 
        [parent] => 0 
        [count] => 7 
        [object_id] => 135 
    ) 
)

want display: [name] => Lietuviu , try $var[3][name], but this not work

2
  • 1
    What is the output you're getting? Commented Nov 14, 2012 at 15:05
  • 4
    $var[3] is an object, so $var[3]->name. Commented Nov 14, 2012 at 15:05

4 Answers 4

1

That is because that Array is actually an object. Put $var into this function:

function object_to_array($data) 
    {
      if(is_array($data) || is_object($data))
      {
        $result = array(); 
        foreach($data as $key => $value)
        { 
          $result[$key] = object_to_array($value); 
        }
        return $result;
      }
      return $data;
    }

Like so:

$realArray = object_to_array($var);
Sign up to request clarification or add additional context in comments.

2 Comments

1 more question how to search values in this array, can I use function in_array ?
Yes you can, it just makes an array of the object you gave it. But I believe in_array doesnt work well with multi-dimensional arrays.
1

The value under the index 3 is a stdClass object, you will have to use the arrow operator -> to get its values:

print $var[3]->name;

Comments

0

As your $var[3] element is an object, you have to access its properties like this:

echo $var[3]->name;

Comments

0

You could use object_to_array as defined or access the value in following way: $var[3]->name

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.