使数组键user_id [英] Make array keys user_id
本文介绍了使数组键user_id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hello stackoverflow的人我需要一个帮助这个脚本。我需要更改数组键。 Ive有这个脚本:
Hello stackoverflow people i need a bit help with this script. I need to change array keys. Ive got this script:
$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);
此脚本从数据库获取用户名和姓氏。它打印如下信息
This script is to take user name and last name from database. It print information like this
array
(
[0] => John Malkovich,
[1] => Tina Morgan
)
需要此信息的键将是用户ID。 Ive尝试使用变量$ firid中的user_id,并将其组合如下:
So my problem is that i need that key of this information would be the user id. Ive tried taking user_id's which is in the variable $firid and combine it like this:
$thrid = array_combine($firid, array_values($thrid));
但它输出:
[7] => John Malkovich,
[14] => Tina Morgan
这应该是14约翰。和7蒂娜。请帮助我。
And it should be 14 John. And 7 Tina. Please help me.
推荐答案
试试这个..
$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);
这篇关于使数组键user_id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文