1

Hi
Suppose I have the code below:

    [taxonomy] => Array
    (
        [118] => stdClass Object
            (
                [tid] => 118
                [vid] => 4
                [name] => A
                [description] => 
                [weight] => 4
            )

        [150] => stdClass Object
            (
                [tid] => 150
                [vid] => 5
                [name] => B
                [description] => 
                [weight] => 0
            )

    )

How can I only get the tid number and exclude others, could someone please give me a suggestion?

Thanks you

4
  • 6
    Couldn't you use $taxonomy[x]->tid? Commented Jan 10, 2011 at 8:53
  • 1
    The array is part of a node object, right? So I added the drupal tag. Commented Jan 10, 2011 at 9:03
  • 1
    @rik Adding drupal context to the question improve things at all, this is PHP only. Plus adding tags on guesses probably isn't best practise. Commented Jan 10, 2011 at 9:06
  • is the array key always the same as the tid value? surely then you'd just need to get the array keys. Commented Jan 10, 2011 at 9:39

3 Answers 3

2

Assuming taxonomy is key of array $arr, You can fetch tid as,

for example ,

$key = your key //the key for which you want fetch record

    $arr['taxonomy'][$key]->tid;

For getting all tid values,

$result = array();
foreach($arr['taxonomy'] as $key=>$value)
{
      $value = (array)$value;
      if(array_key_exists('tid'), $value)
     {
           $result[] = $value['tid'];
     }
}

print_r($result);
Sign up to request clarification or add additional context in comments.

4 Comments

[118] will change dynamically due to different terms/node, so this solution can be improved.
@Charles: Yes, 118 will be dynamic. I just given an example. You can replace 118 with dynamic value. Let me edit answer for your reference.
Sorry for the unclear explanation, Actually, I want to get the tid value from the array while I found the key "tid" in it?
@Charles: I have edited my answer which will give all tid values. Hope this helps.
0
$tids = array_keys($yourArray);

This works because the first level key and the values of subkey "tid" are the same.

3 Comments

so array_keys() return 118 and 150... But, if I want to check if the array have the key=tid then return the tid value, how to do it?
array_keys() does not inject or omit values. It returns all keys and the condition "key=tid" is true for all of them. If you want to extract only a subset (or even only one) TID, you could do array_intersect(array_keys($yourArray), $yourTIDs).
@Charles Yeung: given array_keys as above, just loop through the array keys (using foreach? or similar) and check if($taxonomy[$key]->tid == $key)
0

Charles Yeung you're requirements aren't clear, you say in the other answer (for Nik) that the taxonomy id is dynamic, equally it seems you have more than one taxonomy node, so can I assume you just want return an array of tid values, right?

Equally you said to rik that you want to check if the key=value, I've no idea what you mean by that, but perhaps you could start here...

$tid=array();

foreach($taxonomy as $key=>$value) {
    $tid[]=$value->tid;
}

print_r($tid);

This will give you an array of tid values, if you want to put a condition in there to constrain your output then feel free to do so, problem is your explanation of your requirements aren't clear.

1 Comment

I am sorry for the unclear requirement, please consider this problem: How to check if an array contain key=tid, then return tid's value? Thanks you

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.