我有一个这样的数组:
SELECT ((date_trunc('day', (m.last_mailing AT TIME ZONE 'UTC')::timestamp) +
interval ('1 day') +
(m.email_delivery_hour::int::text || ':00:00')::time
)::timestamptz AT TIME ZONE t.timezone
)::timestamptz AT TIME ZONE 'UTC'
FROM acts2301_membership.member_subscriptions m
JOIN acts2301_email_queue.timezone t ON m.timezone_reference = t.reference
WHERE m.subscription_reference = '514';
我正试图获得这种格式:
Array
(
[0] => Chat Show
[1] => Non-fiction
[2] => Inspirational
)
但是我得到这样的东西:
"genres": [
{
"name": "Chat Show"
},
{
"name": "Non-fiction"
},
{
"name": "Inspirational"
}
]
这就是我在做的事情:
genres": [
"Chat Show",
"Non-fiction",
"Inspirational"
]
然后这是更大阵列的一部分
while($row = mysqli_fetch_array($Data))
{
$Genres = explode('/', filter_var(rtrim($row['genres'], '/'), FILTER_SANITIZE_URL));
}
答案 0 :(得分:1)
print_r(
json_encode(["genres" => array_map(
function($v) { return ['name' => $v]; },
$Genres)]));
结果
{"genres":[{"name":"Chat Show"},{"name":"Non-fiction"},{"name":"Inspirational"}]}
答案 1 :(得分:0)
例如,这是PHP中的数组
$var = array(
"Chat Show",
"Non-fiction" ,
"Inspirational"
);
没有钥匙"名称"。您应该创建一个新数组并将每个元素作为数组推送到新数组。
$result = array();
foreach($var as $name)
{
$arr = array("name"=>$name);
array_push($result, $arr);
}
之后,使用$result
json_encode
进行编码
$json = json_encode($result,true);
echo $json;
以下是echo $json
的输出结果。
[{"name":"Chat Show"},{"name":"Non-fiction"},{"name":"Inspirational"}]
答案 2 :(得分:0)
试试这个:
$Genres=array("Chat Show","Non-fiction","Non-fiction");
$new["genres"]=array();
foreach($Genres as $key => $name){
$new["genres"][$key] = ['name' => $name];
}
echo json_encode($new);
<强>输出强>
{"genres":[{"name":"Chat Show"},{"name":"Non-fiction"},{"name":"Non-fiction"}]}
答案 3 :(得分:0)
您在此处发布的json
字符串不是有效的json,或者是json
的一部分。
所以,你的javascript中可能已经有genres
了,想要得到剩下的东西,这是
[
{ "name": "Chat Show" },
{ "name": "Non-fiction" },
{ "name": "Inspirational" }
]
您当前的PHP $Genres
看起来像这样,因为您正在爆炸字符串
$Genres = [ 'Chat Show', 'Non-fiction', 'Inspirational' ];
应用此选项可更改当前$Genres
array_walk($Genres, function(&$v){ $v = ['name'=>$v]; });
在您的javascript中使用它,
"genres": <?php json_encode($Genres)?>
答案 4 :(得分:-1)
试试这个:
$genres_new['name']=$Genres;
echo json_encode($genres_new);
答案 5 :(得分:-1)
你的问题是,你有简单的字符串数组,但你想要一个关联的多级数组。这是一个相对简单的操作。首先让我们用一些代码说明问题:
// That is what you have, an :
$Genres = [
"Chat Show",
"Non-fiction",
"Inspirational",
];
// or the same for php > 5.4:
$Genres = array(
"Chat Show",
"Non-fiction",
"Inspirational",
);
这将产生以下json字符串(echo json_encode($Genres);
):
["Chat Show","Non-fiction","Inspirational"]
但是如果你想要这样的输出:
[{"name":"Chat Show"},{"name":"Non-fiction"},{"name":"Inspirational"}]
您必须将字符串转换为数组。你可以用那个(或类似的循环)来做到这一点:
foreach($Genres as $key => $name){
$Genres[$key] = ['name' => $name];
}
之后你的数组看起来像这样:
Array (
0 =>
array (
'name' => 'Chat Show',
),
1 =>
array (
'name' => 'Non-fiction',
),
2 =>
array (
'name' => 'Inspirational',
),
)
把事情放在一起你会得到类似的东西:
<?php
// Do whatever is necessary to build your Genres array ...
$Genres = [
"Chat Show",
"Non-fiction",
"Inspirational",
];
// Convert the array into an array of arrays
foreach($Genres as $key => $name){
$Genres[$key] = ['name' => $name];
}
echo json_encode($Genres);
/**
Now you will get this output:
[{"name":"Chat Show"},{"name":"Non-fiction"},{"name":"Inspirational"}]
*/
// After that you can add it to the bigger array:
$biggerArray = [];
$biggerArray['genres'] = $Genres;
echo json_encode($biggerArray);
/**
Output:
{"genres":[{"name":"Chat Show"},{"name":"Non-fiction"},{"name":"Inspirational"}]}
*/