如何在数组php中选择单个值?
因为PHP中的数组更像是散列映射,所以我会努力获取单个值,而不是整个数组 我的json对象:如何在数组php中选择单个值?,php,arrays,json,string,Php,Arrays,Json,String,因为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 stuck
[{
"title": "Hello world1",
"placement": "world",
"time": "today",
"tags": "Hello world im stucked"
},{
"title": "Hello world2",
"placement": "world2",
"time": "today2",
"tags": "Hello2 world2 im2 stucked2"
}]
My 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;
}
}
//迭代json
对于($i=0;$itags);
//迭代标记数组
对于($z=0;$z
结果将是:
数组(1){[0]=>数组(4){[0]=>字符串(5)“你好”[1]=>字符串(5)“世界”[2]=>字符串(2)“im”[3]=>字符串(7)“结巴”}
而不是我所期望的:
字符串(5)“你好”
使用php explode函数将字符串拆分为数组,如下所示:
$data = '[{
"title": "Hello world1",
"placement": "world",
"time": "today",
"tags": "Hello world im stucked"
},{
"title": "Hello world2",
"placement": "world2",
"time": "today2",
"tags": "Hello2 world2 im2 stucked2"
}]';
$dataArray = json_decode($data,true); ///return json object as associative array.
for ($i = 0 ; $i < sizeof($dataArray) ; $i++)
{
$tags = explode(' ',$dataArray[$i]['tags']);//split the string into array.
for ($z = 0 ; $z < sizeof($tags) ; $z++) //loop throug tags array
{
echo $tags[$z];
die; ///remove this for further excecution.
}
}
使用php explode函数将字符串拆分为数组,如下所示:
$data = '[{
"title": "Hello world1",
"placement": "world",
"time": "today",
"tags": "Hello world im stucked"
},{
"title": "Hello world2",
"placement": "world2",
"time": "today2",
"tags": "Hello2 world2 im2 stucked2"
}]';
$dataArray = json_decode($data,true); ///return json object as associative array.
for ($i = 0 ; $i < sizeof($dataArray) ; $i++)
{
$tags = explode(' ',$dataArray[$i]['tags']);//split the string into array.
for ($z = 0 ; $z < sizeof($tags) ; $z++) //loop throug tags array
{
echo $tags[$z];
die; ///remove this for further excecution.
}
}
在声明和使用
getTags
函数时,只需在$tags
之后删除[]
:
$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"
}]';
function getTags($string){
$tags = explode(" " , $string);
return $tags;
}
$obj = json_decode($json);
//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;
}
$json=”[{
“标题”:“你好,世界1号”,
“位置”:“世界”,
“时间”:“今天”,
“标签”:“Hello world我被绊倒了”
},{
“标题”:“Hello world2”,
“安置”:“世界2号”,
“时间”:“今天2”,
“标签”:“Hello2 world2 im2 stucked2”
}]';
函数getTags($string){
$tags=分解(“,$string);
返回$tags;
}
$obj=json_decode($json);
//迭代json
对于($i=0;$itags);
//迭代标记数组
对于($z=0;$z
在声明和使用getTags
函数时,只需删除[]
之后的$tags
:
$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"
}]';
function getTags($string){
$tags = explode(" " , $string);
return $tags;
}
$obj = json_decode($json);
//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;
}
$json=”[{
“标题”:“你好,世界1号”,
“位置”:“世界”,
“时间”:“今天”,
“标签”:“Hello world我被绊倒了”
},{
“标题”:“Hello world2”,
“安置”:“世界2号”,
“时间”:“今天2”,
“标签”:“Hello2 world2 im2 stucked2”
}]';
函数getTags($string){
$tags=分解(“,$string);
返回$tags;
}
$obj=json_decode($json);
//迭代json
对于($i=0;$itags);
//迭代标记数组
对于($z=0;$z
您能给我们看一下getTags
功能吗?可能有重复的“die”在您的第二个循环中,它将在显示第一项后立即停止执行。@vincenth当然,它是用于调试的。我只需要循环的一次迭代。您能给我们看一下getTags
函数吗?可能的重复项有一个“die”在第二个循环中,它将在显示第一项后立即停止执行。@vincenth当然,这是为了调试,我只需要循环的一次迭代。谢谢你,伙计,将变量声明为数组对象的错误也是如此。你能添加一个解释吗?:P提前感谢!这不完全是。将变量声明为数组使用$var=array();
创建。声明类似于$var[]='test'
会给你一个数组,它的第一个元素是test
。但是explode
函数已经返回了一个数组,因此你的getTags
函数返回了数组中的一个数组。然后,你把它放在另一个子数组中,这导致你最终得到了数组中的一个数组。我希望这是清楚的)不,这是一个完美的答案,你可以看到结果是:数组(1){[0]=>array(4){哇,谢谢你,伙计,把变量声明为数组对象的错误也是如此吗?你能补充一个解释吗?:P提前谢谢!这不完全是。把变量声明为数组是用$var=array();
。声明类似$var[]='test'
会给你一个数组,它的第一个元素是test
。但是explode
函数已经返回了一个数组,因此你的getTags
函数返回了数组中的一个数组。然后,你把它放在另一个子数组中,这导致你最终得到了数组中的一个数组。我希望这是清楚的)不,这是一个完美的答案,你可以看到结果是:数组(1){[0]=>array(4){