English 中文(简体)
PHP 阵列格式
原标题:PHP array format
  • 时间:2012-05-22 19:40:30
  •  标签:
  • php
  • json

我怎样才能得到这个格式 :

{
"products": [
    {
        "id":            4,
        "link":          "/product.php?id_product=4",
        "quantity":      1,
        "priceByLine":   "$185.00",
        "name":          "Orci hendrer...",
        "price":         "$185.00",
        "idCombination": 0,
        "hasAttributes": false,
        "hasCustomizedDatas": false,

        "customizedDatas":[
                ]


    },  {
        "id":            5,
        "link":          "/product.php?id_product=5",
        "quantity":      1,
        "priceByLine":   "$215.00",
        "name":          "Semper rutru...",
        "price":         "$215.00",
        "idCombination": 0,
        "hasAttributes": false,
        "hasCustomizedDatas": false,

        "customizedDatas":[
                ]


    }],

}

作为 PHP 阵列吗?

$array[] = array( id =>5, link =>  product.php?id_product=5  ... and so on)

但当我和JSON编码时,它不起作用。

最佳回答

您只需像以下例子一样以正确的方式嵌入您的阵列 。

$arr = array(  products  => array(
  array( id =>4, link =>  product.php?id_product=4  ),
  array( id =>5, link =>  product.php?id_product=5  )
  )
);

<强 > EDIT

在您的代码中,它会看起来像这样插入对象:

$arr = array(  products  => array() );

接下来的每一种产品都可以这样添加(例如,在分析数据库结果的循环中):

$arr[ products ][] = array( id =>5, link =>  product.php?id_product=5  );
问题回答
$data = array(
   products  => array(
    array( id =>5, link =>  product.php?id_product=5  ... and so on),
    array( id =>5, link =>  product.php?id_product=5  ... and so on)
    // repeat once for element.
  )
);
json_encode($data);

如果您需要以 JSON 格式返回 AJAX 回复 - 您需要添加页眉 。

<?php 
header("Content-type: application/json");
echo json_encode($array);

- 否则,您可以将阵列打印成 JS Global 变量。

<?php
echo  <script> ;
echo  var foo =   . json_encode($array); .  ; ;
echo  </script> ;

这是 json_encode($array) 的输出,在那里, $array 被嵌套。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签