PHP - 在foreach循环中向数组推送数据

6
我想要实现以下数组格式:
{
    "success": true,
    "results": [
      {
        "name"  : "Choice 1",
        "value" : "value1",
        "text"  : "Choice 1"
      },
      {
        "name"  : "Choice 2",
        "value" : "value2",
        "text"  : "Choice 2"
      }
    ]
}

然而,我正在使用PHP和foreach循环,从我的数据库返回一些值:

//Search for clients in our database.
$stmt = $dbh->prepare("SELECT * FROM customers");
$stmt->execute();
$showAll = $stmt->fetchAll();

我现在有一个数组的第一部分和一个foreach循环:
$data = array(
    "success" => false,
    "results" => array()
);

foreach ($showAll as $client) {  
               $data_array[] = 
                     array(
                     'name' => $client['name'],
                     'value' => $client['name'],
                     'text' => $client['name']
                );

}

只有上面输出:
[
 {
   "name":"Choice 1",
   "value":"value 1",
   "text":"Choice 1"
 },
 {
   "name":"Choice 2",
   "value":"value2",
   "text":"Choice 2"
 }
]

因此,它缺少我原始数组的顶部部分-但我想循环遍历每个数据库结果在"results": [ ... ]中。


1
“Above only outputs:” 以下是您的输出代码 - Steve
@Steve,它显示在文本“上面只输出:”下面。 - oliverbj
1
这个问题中没有任何代码输出任何内容。我猜你在某个地方调用了 json_encode,但是你的问题并没有展示出来。 - Steve
4个回答

12

试一试

$data = array(
  "success" => false,
  "results" => array()
);

foreach ($showAll as $client) {  
  $data['results'][] = array(
    'name' => $client['name'],
    'value' => $client['name'],
    'text' => $client['name']
  );
}

$data['success'] = true; // if you want to update `status` as well
echo json_encode($data);

这将“success”值添加到末尾,并且不会像我在原始帖子中提到的那样输出值。 - oliverbj
2
“success”已经在$data数组中,顺序不应更改。 - Farooq Ahmed Khan

5

创建$data_array数组后,只需添加我在帖子中的几行代码即可。

Try this code snippet here(with sample input)

ini_set('display_errors', 1);
foreach ($showAll as $client)
{
    $data_array[] = array(
                'name' => $client['name'],
                'value' => $client['name'],
                'text' => $client['name']
    );
}
// add these lines to your code.
$result=array();
$result["success"]=true;
$result["results"]=$data_array;
echo json_encode($result);


@arkascha 先生,我已经测试过了,它以用户想要的方式产生了预期的输出。 - Sahil Gulati
我必须道歉,你是正确的!我被json用单个花括号括起来的立即值所迷惑了。对不起! - arkascha

1
尝试以下操作,由于$data_array中有一个数组在键"Results"上,因此您应该使用"results"作为键,然后尝试将数据推入该数组。
foreach ($showAll as $client) {  
               $data_array["results"][] = 
                     array(
                     'name' => $client['name'],
                     'value' => $client['name'],
                     'text' => $client['name']
                );

}

所以他可以使用json_encode()。这不是问题。 :) - Badshah Sahib

0
你可以直接使用json_encode并将其推送到结果数组中。
$data = array(
    "success" => false,
    "results" => array()
);

$result = [
 [
   "name" => "Choice 1",
   "value" => "value 1",
   "text" => "Choice 1"
 ],
 [
   "name" => "Choice 2",
   "value" => "value2",
   "text" => "Choice 2"
 ]
];

$data['results'] = json_encode($result);

它会将$result转换为json,但"success"将保持不变。所以,他想要整个数组作为json。我认为他应该首先将$result数组赋值给$data["results"],然后json_encode($data)。这可能对他解决问题有帮助。 - Badshah Sahib
哦,原 PO 可以做到那个,我猜我漏掉了 :p - Priyank

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接