开发者

Drupal/PHP - Get a specific value in an array

Hi

Suppose I have the code below:

    [taxonomy] => Array
    (
        [118] => stdClass Object
            (
           开发者_JAVA技巧     [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


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);


$tids = array_keys($yourArray);

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜