如何在PHP中通過數組後訪問「代碼」和「類型」值?
順便說一句,我正在使用「jquery-json」插件。有沒有辦法做到這一點,沒有任何插件?將jQuery對象數組傳遞給PHP,使用JSON
的jQuery:
$(function(){
function product(code, type) {
return {
code: code,
type: type
}
}
var products = [];
products.push(product("333", "Product one"), product("444", "Second product"));
var jsonProducts = $.toJSON(products);
$.post(
"php/process.php",
{products: jsonProducts},
function(data){
$("#result").html(data);
}
);
});
PHP:
<?php
$products = json_decode($_POST["products"], true);
foreach ($products as $product){
echo $product;
}
?>
呀,這是我第一次嘗試,但我得到這個:「注意:試圖獲取非對象的屬性在...「 – elclanrs 2011-03-25 17:43:04
當我做print_r時,我得到了正確的數組strcuture:Array([code] => 333 [type] => Product one)Array([code] => 444 [type] =>第二個產品) – elclanrs 2011-03-25 17:46:27
嘗試'$ product [「code」]'和'$ product [「type」]'? – Bleaourgh 2011-03-25 17:49:47