0

I have the following array which contains both objects and arrays. How do I get only the specific values (for each object) based on their keys? I've tested and the array is displaying (see below) but I cannot isolate the 'name" value as needed.

I have tried the following code to get the name value:

case 'field_prgm_housing' :
$node = 'field_color';
$tids =  field_get_items('node', $node, $key, $node->language);
$terms = taxonomy_term_load_multiple(array(), array('tid' => $tids));
$nameonly = $terms->[0]->name[0];
return = $nameonly;
break;      
Colors (Array, 2 elements)
    12 (Object) stdClass
      tid (String, 2 characters ) 12
      vid (String, 1 characters ) 3
      name (String, 9 characters ) Blue
      description (String, 0 characters )
      format (String, 13 characters ) filtered_html
      weight (String, 1 characters ) 0
      vocabulary_machine_name (String, 15 characters ) colors
      rdf_mapping (Array, 5 elements)
      path (Array, 1 element)
    13 (Object) stdClass
      tid (String, 2 characters ) 13
      vid (String, 1 characters ) 3
      name (String, 8 characters ) Green
      description (String, 0 characters )
      format (String, 13 characters ) filtered_html
      weight (String, 1 characters ) 0
      vocabulary_machine_name (String, 15 characters ) colors
      rdf_mapping (Array, 5 elements)
      path (Array, 1 element)
7
  • 1
    If the array's output is the var_dump of $terms: this one should work $terms[$i]->name. (Just replace $i with the relevant key) Commented Nov 15, 2017 at 11:15
  • 1
    $names = Array_column($array, "name") is that what you are looking for Commented Nov 15, 2017 at 11:19
  • @OfirBaruch in this case what would be the relevant key if not name? Commented Nov 15, 2017 at 11:21
  • According to your example $i should be 12 or 13. But since this data is dynamic you should find a way to get it by using a loop or a sort of a condition. Commented Nov 15, 2017 at 11:23
  • @Andreas In this case "$arrray" would be terms correct? '$nameonly = Array_column($terms, "name");' returns an empty array Commented Nov 15, 2017 at 11:23

2 Answers 2

2
Try this


    $node = 'field_color';
    $tids =  field_get_items('node', $node, $key, $node->language);
    $terms = taxonomy_term_load_multiple(array(), array('tid' => $tids));

    //loop all the values and get the require value
   $name = array();
    foreach($terms as $term){
          $name[] = $term->name;
    }
    return $name;
Sign up to request clarification or add additional context in comments.

4 Comments

I'm operating within switch statements, for each throws errors.
I need to use the values elsewhere hence when I them to be available as the $nameonly variable; is this possible with foreach?
Here is what I've tried so far but its only showing the first value: foreach($terms as $term){ $name=$term->name; } $return[$key] = $name;
$name = array(); foreach($terms as $term){ $name[] = $term->name; } return $name;
0

What $terms->[0] suppose to mean?

$terms is an array, so you have to access it by specifying the index you want to get, like: $terms[12], $terms[13]...

Elements of that array are object so when you get one you have to use "->" operator to get it's field (or method).

So it has to be like:

$nameonly = $terms[12]->name;
$nameonly = $terms[13]->name;

Or you can iterate trough all the array items, as @kranthi suggested.

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.