使数组键user_id

时间:2015-08-11 11:06:31

标签: php mysql database

Hello 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中的user_id,并将其组合起来:

$thrid = array_combine($firid, array_values($thrid));

但它打印出来:

[7] => John Malkovich,
[14] => Tina Morgan

它应该是14约翰。和7蒂娜。请帮帮我。

2 个答案:

答案 0 :(得分:2)

试试这个..

     $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);

答案 1 :(得分:0)

试试这个,

$firid = array_reverse($firid); //reverse the $firid array
$thrid = array_combine($firid, array_values($thrid));
echo "<pre>";print_r($third);

谢谢:)