Php 如何将元素添加到现有数组中
如何将元素添加到现有数组中 这是我的密码。如您所见,我已经尝试使用这行代码将元素添加到数组中Php 如何将元素添加到现有数组中,php,arrays,Php,Arrays,如何将元素添加到现有数组中 这是我的密码。如您所见,我已经尝试使用这行代码将元素添加到数组中 $samlet[$key]['error'] = 'Dobbelt transaktion'; 但这是行不通的。我的数组中已经有一个名为error的键,但其值为空。我想填一下 foreach($samlet as $key) { $transid=$key['transactionid']; $id=$key['cardid']; $time=strto
$samlet[$key]['error'] = 'Dobbelt transaktion';
但这是行不通的。我的数组中已经有一个名为error的键,但其值为空。我想填一下
foreach($samlet as $key)
{
$transid=$key['transactionid'];
$id=$key['cardid'];
$time=strtotime($key['transactiontime']);
$price = $key['pricebefordiscount'];
$cardid = $key['cardid'];
if ($price > 200000 )
{
$samlet[$key]['error'] = 'O/2000,-';
}
if ($price < 2500 )
{
$samlet[$key]['error'] = 'U/25,-';
}
if(lookForNumbers(substr((string)$price, 0, -2)))
{
$samlet[$key]['error'] = '3 ens cifre i beløb';
}
/* if(isset($samlet[$key]))
{*/
foreach($samlet as $k1)
{
$time2=strtotime($k1['transactiontime']);
if($key<$k1)
{
if($k1['cardid']==$id)
{
if(compare($time,$time2))
{
if (empty($k1['transactionid'])|| empty($key['transactionid']))
{
# code...
}
else
{
$samlet[$key]['error'] = 'Dobbelt transaktion';
$samlet[$k1]['error'] = 'Dobbelt transaktion';
}
}
/* }*/
}
}
}
if ($cardid === '88888888')
{
$samlet[$key]['error'] = 'Testscan';
}
}
foreach($samlet作为$key)
{
$transid=$key['transactionid'];
$id=$key['cardd'];
$time=strottime($key['transactiontime']);
$price=$key['pricebefordiscount'];
$cardd=$key['cardd'];
如果($price>200000)
{
$samlet[$key]['error']='O/2000,-';
}
如果(价格<2500美元)
{
$samlet[$key]['error']='U/25,-';
}
if(lookForNumbers(substr((string)$price,0,-2)))
{
$samlet[$key]['error']='3 ens cifre i beløb';
}
/*if(isset($samlet[$key]))
{*/
foreach($samlet作为$k1)
{
$time2=strottime($k1['transactiontime']);
如果($key使用:$samlet[$key]['error'][]
它将向变量添加新元素$samlet[$key]['error']
执行打印($samlet[$key]['error']);
以查看该值现在包含一个存储的错误数组。然后您只需循环通过这些元素来记录、显示等。$samlet[$key]['error'][]['foo'
注意结尾处的额外[]
。这将使$samlet[$key]['error']
一个字符串数组。我刚才这样做了,它可以工作。但是出现了另一个问题。我希望你能帮助againI。我刚才这样做了,它可以工作。但是出现了另一个问题。我希望你能帮助我again@Zaz当然,问题是什么?但是现在我的问题是$time=$samlet[$key],我必须用strottime($key['transactiontime')替换它当我尝试这样做时,它不会起作用,我就是这样尝试$time=$samlet(strotime($key['transactiontime'))created和transactiontime之间存在很大差异。created=2013-03-06 19:44:11,而transactiontime=1362598242@Zaz您应该将此问题标记为已回答,并打开另一个关于其他问题的问题。在同一个问题中有多个问题是不好的,因为这会使网站混乱。=o)你回答完这个新问题后,我再问你。