0

我在尝试用 PHP 编写代码时遇到了麻烦:

{
   "value": {
          "board": [
                  {"name": "Abdul-Jabbar", "values": [38387, 1560, 24.6]},
                  {"name": "Karl Malone", "values": [36928, 1476, 25.0]},
                  {"name": "Michael Jordan", "values": [32292, 1072, 30.1]},
                  {"name": "W. Chamberlain", "values": [31419, 1045, 30.1]},
                  {"name": "Kobe Bryant", "values": [29484, 1161, 25.4]},
                  {"name": "Shaq O Neal", "values": [28596, 1207, 23.7]},
                  {"name": "Moses Malone", "values": [27409, 1329, 20.6]},
                  {"name": "Elvis Hayes", "values": [28313, 1303, 21.0]},
                  {"name": "H. Olajuwon", "values": [26946, 1238, 21.8]}
                  ]
            }
}

到目前为止,我有这个:

  $cart = array();

  foreach ($deliveries as $delivery) {

    // get name of the message sent.
    $msgFilter = array('id' => $delivery->messageId);
    $message = array_pop($client->readMessages(array('pageNumber' => 1,
                             'includeContent' => false,
                             'filter' => $msgFilter))->return);



   $cart[] = array(
    "value" => array(
        array(
            "message" => $message->name
        ),
        array(
            "value" => $delivery->revenue
        )
    )
  );


  }

  $postData = json_encode($cart);

我不知道如何将值和板包装在数组周围?!?

4

1 回答 1

1

您的代码对我来说有点令人困惑,从 json 的意义上讲,要制作的数组实际上是一个对象/数组混合,更好的表示是

value.board.array_part

如果您只想用板和值数组包装购物车数组

$board = array("board"=>$cart);
$val = array("value"=> $board);
于 2013-10-25T03:53:57.143 回答