0

だから、これはPHPの私の元の配列です

$data = array(
  "results" => array(
    "course" => "$cc",
    "books" => array(
      "book" =>
      array(  
      //Place temp here;  
      )
    )
  )
);

これは、配列の中央に移動するデータがさらにある場所です。数百または1つになる可能性があります

foreach ($my_array as $counter => $bc) {
        $temp = array(
          "-id" => "$id[$counter]",
          "-title" => "$title[$counter]",
          "-isbn" => "$isbn[$counter]",
          "-borrowedcount" => "$borrowedcount[$counter]"
        );
    }

したがって、次のように有効な JSON の方法でエンコードできます。

{
 "results": {
  "course": "CC167",
  "books": {
   "book": [
    {
      "-id": "585457",
      "-title": "Beginning XNA 20 game programming : from novice to professional",
      "-isbn": "1590599241",
      "-borrowedcount": "16"
    },
    {
      "-id": "325421",
      "-title": "Red Hat Linux 6",
      "-isbn": "0201354373",
      "-borrowedcount": "17"
    },
    {
      "-id": "424317",
      "-title": "Beginner's guide to darkBASIC game programming",
      "-isbn": "1592000096",
      "-borrowedcount": "46"
    },
    {
      "-id": "437390",
      "-title": "Objects first with Java : a practical introduction using BlueJ",
      "-isbn": "0131249339",
      "-borrowedcount": "89"
    },
    {
      "-id": "511094",
      "-title": "Objects first with Java : a practical introduction using BlueJ",
      "-isbn": "2006044765",
      "-borrowedcount": "169"
    }
   ]
  }
 }
}

あなたが提供できる助けをありがとう

4

1 に答える 1

3

ループしてすべての値を追加したら、book配列要素を の値に割り当てます。$temp

$data['results']['books'] = $temp;

または、最初にループし、$data必要な値が既にあるときに配列を設定するとき:

$data = array("results" => array("course" => "$cc", "books" => $temp));
于 2013-02-25T12:04:31.790 に答える