Php 将每个整数放入数组
因为我是新手,所以我有个问题 我有$nUserID,其中存储用户的id是Php 将每个整数放入数组,php,arrays,for-loop,Php,Arrays,For Loop,因为我是新手,所以我有个问题 我有$nUserID,其中存储用户的id是 int(1) int(2) int(1) 在$nAuctionID中,我有项目id,它们如下 int(150022) int(150022) int(150031) 我需要把它放在一个数组中 array ( [1] => 150022 [2] => 120022,150031 ) 哪个用户哪个项目id监视 怎么做 我想应该用foreach,但我无法想象will的样子 从 $u[] = $nUserI
int(1)
int(2)
int(1)
在$nAuctionID中,我有项目id,它们如下
int(150022)
int(150022)
int(150031)
我需要把它放在一个数组中
array (
[1] => 150022
[2] => 120022,150031
)
哪个用户哪个项目id监视
怎么做
我想应该用foreach,但我无法想象will的样子
从
$u[] = $nUserID;
$i[] = $nAuctionID;`
这将有助于:
$arr = array();
foreach( $nUserID as $key=>$value)
{
$arr[$value] = $nAuctionID[$key] ;
}
print_r($arr);
最令人困惑的问题伊娃
这就是我从您的问题中得到的信息。根据用户ID对它们进行分组,下面应该会得出您想要的结果
这就是我从你的问题中得到的,我希望它能帮助你接收空数组。我在我的计算机上测试过,它工作正常。你确定你定义的数组正确吗?是的,看起来首先需要将$value放入$arr,然后再放入另一个数组,以放入项目id的$u[]=$nUserID$u[]=$nAuctionID;我确实喜欢这样,并且接收数组4{[0]=>int1[1]=>int150022[2]=>int2[3]=>int150022}
$outputArray = array();
for ($i=0; $i< count($nUserID); $i++) {
$outputArray[$nUserID[$i]] = $nAuctionID[$i];
}
echo "<pre>";
print_r($outputArray);
$usersWatching = array();
// The following results in:
// array(1 => array(150022, 150023), 2 => array(150022))
// Which should be way more useful.
foreach ($nUserID as $i => $userID)
{
if (!isset($usersWatching[$userID]))
$usersWatching[$userID] = array();
$usersWatching[$userID][] = $nAuctionID[$i];
}
// To get the comma-separated version, do this:
$usersWatching = array_map(function ($value) {
return implode(',', $value);
}, $usersWatching);