【问题标题】:PHP - Push data to array in foreach loop [duplicate]PHP - 在foreach循环中将数据推送到数组[重复]
【发布时间】:2017-09-16 19:02:06
【问题描述】:

我想实现以下数组格式:

{
    "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": [ ... ]

【问题讨论】:

  • "Above only outputs:" 请出示您的输出代码
  • @Steve 它显示在文本“以上仅输出:”的下方
  • 这个问题中没有代码可以输出任何东西。我猜你在某个地方打电话给json_encode,但你的问题并没有表明

标签: php arrays foreach


【解决方案1】:

试试这个

$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);

【讨论】:

  • 这会将“成功”值添加到末尾,并且不会输出我的 OP 中提到的值..
  • 'success' 已经在$data 数组中,顺序不应该改变。
【解决方案2】:

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

Try this code snippet here(带样本输入)

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 先生,我已经对其进行了测试,它以用户想要的方式提供了预期的输出
  • 我不得不道歉,你确实是对的!我被 json 用一个花括号括起来的立即值这一事实愚弄了。对不起!
【解决方案3】:

尝试这样做,因为您在键“结果”上的 $data_array 中有一个数组,因此您也应该使用“结果”作为键,然后尝试将数据推送到该数组中

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

}

【讨论】:

  • 这样他就可以使用 json_encode()。这不是问题。 :)
【解决方案4】:

您可以简单地使用 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 但“成功”将保持不变。因此,他想将整个数组制作为 json。我认为他应该先将$result 数组分配给$data["results"] 然后json_encode($data); 这可能有助于他解决他的问题。
  • Ya OP 可以做到这一点,我想我错过了:p
猜你喜欢
  • 1970-01-01
  • 2018-10-25
  • 2019-02-23
  • 2018-01-04
  • 1970-01-01
  • 2016-12-23
  • 2021-05-30
  • 2022-01-27
  • 1970-01-01
相关资源
最近更新 更多