php 从 Foreach 创建数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9294664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create Array from Foreach
提问by Matthew Colley
I have a Mysql table which contains a column of JSON data and a column with an amount. The goal is to extract the JSON data and the amount and build an array within the foreach loop. Here is my code:
我有一个 Mysql 表,其中包含一列 JSON 数据和一列带有金额的列。目标是提取 JSON 数据和数量,并在 foreach 循环中构建一个数组。这是我的代码:
$sql = "SELECT `Amount`, `NewObject` FROM `mb_cart` WHERE `MyID` = '$id'";
$data_main = $db->query($sql);
Here is my statement that I am using to build the array:
这是我用来构建数组的声明:
foreach ($data_main as $transaction_main) {
$json_decoded = json_decode($transaction_main);
$cart = array('Amount' => $amount, 'CodeType' => $json_decoded->data->Type->data->codeType, 'Name' => $json_decoded->data->Name, 'SiteName' => $json_decoded->data->SiteName);
}
However when I run this, I am only returning only the first record set and the amount is blank, but the JSON data is listed. Appreciate any insight anyone cares to share.
但是,当我运行它时,我只返回第一个记录集并且数量为空,但列出了 JSON 数据。感谢任何人愿意分享的任何见解。
回答by gorelative
You need to add []
to $cart
array. WIth each run of foreach you're overwriting the variable $cart.
您需要添加[]
到$cart
数组。每次运行 foreach 时,您都会覆盖变量 $cart。
something like so would work:
像这样的事情会起作用:
foreach ($data_main as $transaction_main) {
$json_decoded = json_decode($transaction_main);
$cart[] = array('Amount' => $amount, 'CodeType' => $json_decoded->data->Type->data->codeType, 'Name' => $json_decoded->data->Name, 'SiteName' => $json_decoded->data->SiteName);
}
Or if you wanted the array key to match that of the ID of each row:
或者,如果您希望数组键与每一行的 ID 匹配:
Note:You will need to set $id variable somehow above IE: SELECT id, amount
also note that you COULD potentially have issues if integer from id
is non-unique.. eg(1,1,2,3,4,5,6) it will only show the last id of 1
instead of both (since key's are duplicates).
注意:您需要在 IE 上方以某种方式设置 $id 变量:SELECT id, amount
还请注意,如果来自id
非唯一的整数,您可能会遇到问题.. eg(1,1,2,3,4,5,6) 它只会显示最后一个 id1
而不是两者(因为键是重复的)。
foreach ($data_main as $transaction_main) {
$json_decoded = json_decode($transaction_main);
$cart[$id] = array('Amount' => $amount, 'CodeType' => $json_decoded->data->Type->data->codeType, 'Name' => $json_decoded->data->Name, 'SiteName' => $json_decoded->data->SiteName);
}
回答by George Cummins
Your variable $cart
is being overwritten in each loop because you are calling array()
each time.
您的变量$cart
在每个循环中都被覆盖,因为您array()
每次都在调用。
Instead, declare your array outside the loop, and just add values to it as needed:
相反,在循环外声明您的数组,并根据需要为其添加值:
$cart = array();
foreach ($data_main as $transaction_main) {
$json_decoded = json_decode($transaction_main);
$cart[] = array('Amount' => $amount, 'CodeType' => $json_decoded->data->Type->data->codeType, 'Name' => $json_decoded->data->Name, 'SiteName' => $json_decoded->data->SiteName);
}
回答by Akhil Thayyil
Try this :
尝试这个 :
$cart=array();
foreach ($data_main as $transaction_main) {
$json_decoded = json_decode($transaction_main);
$cart[] = array('Amount' => $amount, 'CodeType' => $json_decoded->data->Type->data->codeType, 'Name' => $json_decoded->data->Name, 'SiteName' => $json_decoded->data->SiteName);
}
You were treating the cart as a variable instead of array
您将购物车视为变量而不是数组
回答by 472084
$cart = array();
foreach ($data_main as $transaction_main) {
$json_decoded = json_decode($transaction_main);
$cart[] = array('Amount' => $amount, 'CodeType' => $json_decoded->data->Type->data->codeType, 'Name' => $json_decoded->data->Name, 'SiteName' => $json_decoded->data->SiteName);
}
print_r($cart);
回答by user3470929
the way to approach this is first create a variable $cart = array();
and then do foreach();
i have written code below please go through it
解决这个问题的方法是首先创建一个变量$cart = array();
,然后foreach();
我是否在下面编写了代码,请通过它
foreach($data_main as $transaction_main){
$json_decoded = json_decoded($transaction_main);
$cart[]=array('Amount' => $amount, 'CodeType' => $json_decoded->data->Type->data->codeType, 'Name' => $json_decoded->data->Name, 'SiteName' => $json_decoded->data->SiteName);
}