这可能已被多次询问,但我无法找到解决方案。
这是我的阵列:
$request=Array (
[0] => Array ( [staName] => Auditorium Stravinsky 2m2c )
[1] => Array ( [staName] => Geneva Arena )
[2] => Array ( [staName] => Les Docks )
[3] => Array ( [staName] => Kheops )
)
我需要输出如下JSON:
"Auditorium Stravinsky 2m2c ","Geneva Arena","Les Docks","Kheops"
我目前的代码如下:
foreach($request as $value)
{
$names[]=$value;
}
$jsonValue = json_encode(array_values($names));
print_r($jsonValue);
我目前的输出如下JSON格式:
[{"staName":"Auditorium Stravinsky 2m2c "},{"staName":"Geneva Arena"},{"staName":"Les Docks"},{"staName":"Kheops"}]
如何阻止“staName”被排除?
非常感谢,请考虑我的帖子,因为这只是我在本网站上发表的第二篇文章。
答案 0 :(得分:1)
<?php
$request=Array (
0 => Array ( 'staName' => 'Auditorium Stravinsky 2m2c' ) ,
1 => Array ( 'staName' => 'Geneva Arena' ) ,
2 => Array ( 'staName' => 'Les Docks' ) ,
3 => Array ( 'staName' => 'Kheops' )
);
$newArray=array();
for($i=0;$i<count($request);$i++){
$newArray[$i]=$request[$i]['staName'];
}
$newArray=json_encode($newArray,true);
print_r($newArray);
输出是合并的json:
["Auditorium Stravinsky 2m2c","Geneva Arena","Les Docks","Kheops"]
答案 1 :(得分:0)
你可以通过以下方式实现这一点 的代码强>
$a = array();
foreach($request as $key =>$val){
foreach($val as $k => $v){
$a[] = $v;
}
}
print_r(json_encode($a));
检查此演示link
<强>输出强>
["Auditorium Stravinsky 2m2c","Geneva Arena","Les Docks","Kheops"]
答案 2 :(得分:0)
所有array
定义中的第一个都不正确。并且您的输出是简单字符串而不是JSON
<?php
$request=Array (
0 => Array ( 'staName' => 'Auditorium Stravinsky 2m2c' ),
1 => Array ( 'staName' => 'Geneva Arena' ) ,
2 => Array ( 'staName' => 'Les Docks' ) ,
3 => Array ( 'staName' => 'Kheops' )
);
$name = '';
foreach($request as $value)
{
foreach($value as $value2)
{
$name = $name . ' ' . $value2;
}
}
echo $name;
输出
Auditorium Stravinsky 2m2c Geneva Arena Les Docks Kheops