【发布时间】:2016-08-25 12:14:23
【问题描述】:
由于 PHP 中的数组更像是哈希映射,我在努力获取单个值而不是整个数组
我的 json 对象:
[{
"title": "Hello world1",
"placement": "world",
"time": "today",
"tags": "Hello world im stucked"
},{
"title": "Hello world2",
"placement": "world2",
"time": "today2",
"tags": "Hello2 world2 im2 stucked2"
}]
我的 getTags 函数:
function getTags($string){
$tags[] = explode(" " , $string);
return $tags;
}
我的代码正在迭代一个 json 对象($obj),获取每次迭代的“标签”,使用函数 getTags(//string to split)将它们拆分到一个名为“$tags”的数组中,然后再次将它们迭代到获取每次迭代的值。
//Iterate json
for ($i = 0 ; $i < sizeof($obj) ; $i++){
//split the tags string to array (" ")
$tags[] = getTags($obj[$i]->tags);
//Iterate tags array
for($z = 0; $z < sizeof($tags); $z++) {
//get the value of the array
var_dump($tags[$z]).die;
}
}
结果将是:
array(1) { [0]=> array(4) { [0]=> string(5) "Hello" [1]=> string(5) "world" [2]=> string(2 ) "im" [3]=> 字符串(7) "卡住" } }
而不是我所期待的:
字符串(5)“你好”
【问题讨论】:
-
你能告诉我们
getTags函数吗? -
第二个循环中有一个“死”,它会在显示第一个项目后立即停止执行。
-
@vincenth 当然,它用于调试我只想要循环的单次迭代