你好,stackoverflow的人,我需要一点关于这个脚本的帮助。我需要更改数组键。我有剧本:

$Pirmas = mysql_query("SELECT user_id FROM dotp_user_task_type WHERE user_task_types_id = '$select1'");
if ($Pirmas) {
    while($row = mysql_fetch_array($Pirmas)){
        $firid[]=$row['user_id'];
    }
    $Pirmas=implode(",",$firid);
    $Antras = mysql_query("SELECT user_contact FROM dotp_users WHERE user_id IN ($Pirmas)");
    //$Nusers = mysql_query("SELECT CONCAT(contact_first_name, ' ', contact_last_name) as fullname FROM dotp_contacts WHERE user_id IN ($Nusers)");
    if ($Antras) {
        while($row = mysql_fetch_array($Antras)) {
            $secid[]=$row['user_contact'];
        }
        $Antras=implode(",",$secid);
        $Trecias = mysql_query("SELECT CONCAT(contact_first_name, ' ', contact_last_name) as fullname FROM dotp_contacts WHERE contact_id IN ($Antras)");
        if ($Trecias) {
            while($row = mysql_fetch_array($Trecias)) {
                $thrid[]=$row['fullname'];
            }
            $key = array($Pirmas);
            $thrid = array_combine($key, array_values($thrid));
            print_r($thrid);

此脚本将从数据库中获取用户名和姓氏。它像这样打印信息
array
(
[0] => John Malkovich,
[1] => Tina Morgan
)

所以我的问题是,我需要这个信息的关键是用户ID。我试着取变量$firid中的用户ID,然后这样组合:
$thrid = array_combine($firid, array_values($thrid));

但它印着:
[7] => John Malkovich,
[14] => Tina Morgan

应该是14约翰。还有7蒂娜。请帮帮我。

最佳答案

试试这个..

     $useridval=array();
            $secid=array();
            $Antras = mysql_query("SELECT user_contact,user_id FROM dotp_users WHERE user_id IN ($Pirmas)");

                if ($Antras) {
                    while($row = mysql_fetch_array($Antras)) {
                        $secid[]=$row['user_contact'];
                        $useridval[]=$row['user_id'];
                    }
            }
            for($i=0;$i<count($secid);$i++)
            {
                    $Trecias = mysql_query("SELECT CONCAT(contact_first_name, ' ', contact_last_name) as fullname,contact_id  FROM dotp_contacts WHERE contact_id = '$secid[$i]'");

                            if ($Trecias) {

    if(mysql_num_rows($Trecias)>0)
    {
$row = mysql_fetch_array($Trecias);
                                    $thrid[$useridval[$i]]=$row['fullname'];
    }
                                }
            }

                print_r($thrid);

09-09 19:28