php PHP如何检索数组值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5568901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP how to retrieve array values
提问by Ajay
I have following array, I want to retrieve name
, comment
and each of the tags
(to insert in database. How can i retrieve the array values. Also, can i filter ONLY the tags values which are larger than 3 characters and contains only a-Z0-9 valueus. Thank you very much.
我有以下数组,我想检索name
,comment
和each of the tags
(插入数据库。我如何检索数组值。另外,我能否仅过滤大于 3 个字符且仅包含 a-Z0-9 值的标签值。非常感谢。
Array
(
[folder] => /test
[name] => ajay
[comment] => hello world.. test comment
[item] => Array
(
[tags] => Array
(
[0] => javascript
[1] => coldfusion
)
)
)
回答by RDL
$name = $array['name'];
$comment = $array['comment'];
$tags = $array['item']['tags']; // this will be an array of the tags
You can then loop over the tags like:
然后,您可以遍历标签,例如:
foreach ($tags as $tag) {
// do something with tag
}
Or access each one individually
或单独访问每个
echo $tags[0];
echo $tags[1];
回答by Rocket Hazmat
$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
回答by Wh1T3h4Ck5
To filter tags longer than 3 chars and only tags contain a-z, A-Z, 0-9 you can use this code
要过滤超过 3 个字符的标签并且仅标签包含 az、AZ、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;
Use it like
使用它就像
print_r($valid_tags);