-1

由于我是新手,我有一个问题

好吧,我在$nUserID哪里存储用户的 ID,就像

int(1)
int(2)
int(1)

$nAuctionID我有物品ID,它们就像

int(150022)
int(150022)
int(150031)

我需要把它放在 1 个数组中,让它像

array (

[1] => 150022
[2] => 120022,150031

)

哪个用户哪个项目 ID 观看

怎么做 ?

我虽然应该使用 foreach ,但我无法想象会是什么样子

从...开始

 $u[] = $nUserID;
 $i[] = $nAuctionID;`
4

3 回答 3

1

Grouping them by user ID, the following should result in what you're looking for.

$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);
于 2013-08-21T17:10:42.130 回答
1

这将起作用:

$arr = array();
foreach( $nUserID as $key=>$value)
{
   $arr[$value] =  $nAuctionID[$key] ;
}
print_r($arr);
于 2013-08-21T09:35:00.663 回答
0

最令人困惑的措辞问题EVA

$outputArray = array();

for ($i=0; $i< count($nUserID); $i++) {
    $outputArray[$nUserID[$i]] = $nAuctionID[$i];
}

echo "<pre>";
print_r($outputArray);

这就是我从你的问题中得到的..

于 2013-08-21T09:35:18.700 回答