PHP如何检索数组值

时间:2011-04-06 15:27:25

标签: php arrays

我有以下数组,我想检索namecommenteach of the tags(在数据库中插入。如何检索数组值。另外,我只能过滤标记大于3个字符且仅包含-Z0-9值的值。非常感谢。

Array
(
    [folder] => /test
    [name] => ajay
    [comment] => hello world.. test comment
    [item] => Array
        (
            [tags] => Array
                (
                    [0] => javascript
                    [1] => coldfusion
                )

        )

)

3 个答案:

答案 0 :(得分:5)

$name = $array['name'];
$comment = $array['comment'];
$tags = $array['item']['tags']; // this will be an array of the tags

然后你可以遍历标签,如:

foreach ($tags as $tag) {
  // do something with tag
}

或单独访问每个

echo $tags[0];
echo $tags[1];

答案 1 :(得分:2)

$name = $array['name'];
echo $name; // ajay

$comment = $array['comment']
echo $comment;  //hello world.. test comment

$tags = $array['item']['tags'];
echo $tags[0]; // javascript
echo $tags[1]; // coldfusion

http://www.php.net/manual/en/language.types.array.php

答案 2 :(得分:1)

要过滤超过3个字符的标签,且只有标签包含a-z,A-Z,0-9,您可以使用此代码

$alltags =  $your_array["item"]["tags"];

$valid_tags = array();
foreach($alltags as $tag)
  if ((preg_match("/^[a-zA-Z0-9]+$/", $tag) == 1) && (strlen($tag) > 3)) $valid_tags[] = $tag;

一样使用它
print_r($valid_tags);