Php 在数组末尾正确插入数组
我有这样一个数组:Php 在数组末尾正确插入数组,php,arrays,insert,Php,Arrays,Insert,我有这样一个数组: $itemID = [ "appid" => 570, "contextid" => "2", "amount" => 1, "assetid" => $value ]; $itemsID = [ ]; array_push($itemsID, $itemID); 我想制作另一个数组,其中包含不同的$itemIDs,因为$value将不断变化 我尝试使用array\u push如下: $itemID
$itemID = [
"appid" => 570,
"contextid" => "2",
"amount" => 1,
"assetid" => $value
];
$itemsID = [ ];
array_push($itemsID, $itemID);
我想制作另一个数组,其中包含不同的$itemID
s,因为$value
将不断变化
我尝试使用array\u push
如下:
$itemID = [
"appid" => 570,
"contextid" => "2",
"amount" => 1,
"assetid" => $value
];
$itemsID = [ ];
array_push($itemsID, $itemID);
输出为:
Array
(
[0] => Array
(
[appid] => 570
[contextid] => 2
[amount] => 1
[assetid] => 5628263595
)
)
Array
(
[0] => Array
(
[appid] => 570
[contextid] => 2
[amount] => 1
[assetid] => 3651140937
)
)
我希望输出为:
Array
(
[0] => Array
(
[appid] => 570
[contextid] => 2
[amount] => 1
[assetid] => 5628263595
[1] => Array
(
[appid] => 570
[contextid] => 2
[amount] => 1
[assetid] => 3651140937
)
我怎样才能做到这一点
完整代码:
foreach ($select as $key => $value) {
if(array_key_exists($key, $select)) {
$itemID = [ "appid" => 570,
"contextid" => "2",
"amount" => 1,
"assetid" => $value ];
$itemsID = [ ];
array_push($itemsID, $itemID);
echo "<pre>";
print_r($itemsID);
echo "</pre>";
}
foreach($select as$key=>$value){
如果(数组\键\存在($key$select)){
$itemID=[“appid”=>570,
“contextid”=>“2”,
“金额”=>1,
“资产净值”=>$value];
$itemsID=[];
数组\u push($itemsID,$itemID);
回声“;
$itemsID = []; // create the array to hold result
foreach ($select as $key => $value) {
if(array_key_exists($key, $select)) {
$itemID = [ "appid" => 570,
"contextid" => "2",
"amount" => 1,
"assetid" => $value ];
// add another occurance to the array
// containing the new $itemID array
$itemsID[] = $itemID;
}
}
// print the results
echo "<pre>";
print_r($itemsID);
echo "</pre>";
打印(itemsID);
回声“;
$itemsID = []; // create the array to hold result
foreach ($select as $key => $value) {
if(array_key_exists($key, $select)) {
$itemID = [ "appid" => 570,
"contextid" => "2",
"amount" => 1,
"assetid" => $value ];
// add another occurance to the array
// containing the new $itemID array
$itemsID[] = $itemID;
}
}
// print the results
echo "<pre>";
print_r($itemsID);
echo "</pre>";
}
}
没有测试,但你明白了。
编辑到您的代码:
$itemList = [ ];
foreach ($select as $key => $value) {
if(array_key_exists($key, $select)) {
$item = [ "appid" => 570,
"contextid" => "2",
"amount" => 1,
"assetid" => $value ];
array_push($itemList, $item);
}
}
echo "<pre>";
print_r($itemList);
echo "</pre>";
$itemsID=array();
foreach($select as$key=>$value){
如果(数组\键\存在($key$select)){
$itemID=[“appid”=>570,
“contextid”=>“2”,
“金额”=>1,
“资产净值”=>$value];
$itemsID[]=array\u merge($itemsID,$itemID);
回声“;
$itemsID = []; // create the array to hold result
foreach ($select as $key => $value) {
if(array_key_exists($key, $select)) {
$itemID = [ "appid" => 570,
"contextid" => "2",
"amount" => 1,
"assetid" => $value ];
// add another occurance to the array
// containing the new $itemID array
$itemsID[] = $itemID;
}
}
// print the results
echo "<pre>";
print_r($itemsID);
echo "</pre>";
打印(itemsID);
回声“;
$itemsID = []; // create the array to hold result
foreach ($select as $key => $value) {
if(array_key_exists($key, $select)) {
$itemID = [ "appid" => 570,
"contextid" => "2",
"amount" => 1,
"assetid" => $value ];
// add another occurance to the array
// containing the new $itemID array
$itemsID[] = $itemID;
}
}
// print the results
echo "<pre>";
print_r($itemsID);
echo "</pre>";
}
}
每次迭代都要重新创建$itemsID
。而是在循环外创建一次,在llop内添加,并在循环后检查:
$itemList=[];
foreach($select as$key=>$value){
如果(数组\键\存在($key$select)){
$item=[“appid”=>570,
“contextid”=>“2”,
“金额”=>1,
“资产净值”=>$value];
数组\u push($itemList,$item);
}
}
回声“;
$itemsID = []; // create the array to hold result
foreach ($select as $key => $value) {
if(array_key_exists($key, $select)) {
$itemID = [ "appid" => 570,
"contextid" => "2",
"amount" => 1,
"assetid" => $value ];
// add another occurance to the array
// containing the new $itemID array
$itemsID[] = $itemID;
}
}
// print the results
echo "<pre>";
print_r($itemsID);
echo "</pre>";
打印(项目列表);
回声“;
$itemsID = []; // create the array to hold result
foreach ($select as $key => $value) {
if(array_key_exists($key, $select)) {
$itemID = [ "appid" => 570,
"contextid" => "2",
"amount" => 1,
"assetid" => $value ];
// add another occurance to the array
// containing the new $itemID array
$itemsID[] = $itemID;
}
}
// print the results
echo "<pre>";
print_r($itemsID);
echo "</pre>";
注意:我将变量重命名为
$item
和$itemList
,以便更好地描述它们的用法-变量名称不明确使得代码很难用这一行$itemsID=[]来理解代码>您正在向主阵列添加另一级别的阵列。如果没有这一行,代码将按照您的要求执行
另外,向数组中添加新事件的更简单语法是只需执行$itemsID[]=$itemID代码>
$itemsID=[];//创建数组以保存结果
foreach($select as$key=>$value){
如果(数组\键\存在($key$select)){
$itemID=[“appid”=>570,
“contextid”=>“2”,
“金额”=>1,
“资产净值”=>$value];
//向数组中添加另一个实例
//包含新的$itemID数组
$itemsID[]=$itemsID;
}
}
//打印结果
回声“;
$itemsID = []; // create the array to hold result
foreach ($select as $key => $value) {
if(array_key_exists($key, $select)) {
$itemID = [ "appid" => 570,
"contextid" => "2",
"amount" => 1,
"assetid" => $value ];
// add another occurance to the array
// containing the new $itemID array
$itemsID[] = $itemID;
}
}
// print the results
echo "<pre>";
print_r($itemsID);
echo "</pre>";
打印(itemsID);
回声“;
$itemsID = []; // create the array to hold result
foreach ($select as $key => $value) {
if(array_key_exists($key, $select)) {
$itemID = [ "appid" => 570,
"contextid" => "2",
"amount" => 1,
"assetid" => $value ];
// add another occurance to the array
// containing the new $itemID array
$itemsID[] = $itemID;
}
}
// print the results
echo "<pre>";
print_r($itemsID);
echo "</pre>";
请发布您的完整代码。我认为这里的问题可能是您在推送之前设置了$itemsId=[],您能给我们看一下您的完整代码吗?正如我所说,问题是您总是将$itemsId=[]的代码部分设置为空$itemsId;删除它并在循环之前初始化它