下面我的mysql数组提取有什么问题:
$mysql_array = mysql_query("SELECT * FROM table1 WHERE uid1='$uid1'");
$array = array();
while($row = mysql_fetch_assoc($mysql_array)){
$array[] = $row;
}
$array = array_unique($array);
$array = array_reverse($array);
$emails = array();
$numbers = array();
foreach($array as $row){
$uid2 = $row['uid2'];
$number = number($uid2);
if(strlen($number) > 9){
$numbers[] = array('uid2' => $uid2, 'number' => $number);
}
else{
$email = email($uid2);
$emails[] = array('uid2' => $uid2, 'email' => $email);
}
}
$numbers = array_unique($numbers);
$emails = array_unique($emails);
var_dump($numbers);
var_dump($emails);
我必须做一些事情来将mysql中的"Resource"转换为数组。上面的代码一定有问题。这就是我在var_dump
上得到的:array(0) { }
和array(1) { [0]=> array(2) { ["uid2"]=> NULL ["email"]=> NULL } }
看看这个foreach:
foreach($array as $uid2){
$uid2 = $array['uid2'];
$number = number($yfbid);
if(strlen($number) > 9){
$numbers[] = array('uid2' => $uid2, 'number' => $number);
}
else{
$email = email($uid2);
$yfbid[] = array('uid2' => $uid2, 'email' => $email);
}
}
您迭代数组的所有位置,并将它们称为$uid2。然后,在第一行中执行$uid2 = $array['uid2'];
。你失去了你的阵列位置。
也许你想要这样的东西:$var = $uid2['uid2'];
我想这就是你想要做的:
$result = mysql_query("SELECT DISTINCT * FROM table1 WHERE uid1 = '{$uid1}' ORDER BY /* insert column name here, followed by ASC or DESC */");
$table = array();
while ($row = mysql_fetch_assoc($result))
$table[] = $row;
// at this point, each entry in the $table array is one row from 'table1'.
// shouldn't need to do array_unique or array_reverse with the modified SQL query above.
$numbers = $emails = array();
foreach ($table as $row)
{
$number = number($row["uid2"]);
if (strlen($number) > 9 && !in_array($numbers[$row["uid2"]], $number, true))
$numbers[$row["uid2"]][] = $number;
else
{
$email = email($row["uid2"]);
if (!in_array($emails[$row["uid2"]], $email, true))
$emails[$row["uid2"]][] = $email;
}
}
// shouldn't need to do array_unique with the if-statements above
var_dump($numbers);
var_dump($emails);
编辑回答评论中的问题:
根据您使用的逻辑,结果可能是相同的。我上面的例子将允许$numbers
的这种情况,而您的例子不会:
array(2)
{
[123] => array(2)
{
[0] => 1234567890, // same as $numbers[456][0]
[1] => 9876543210
},
[456] => array(1)
{
[0] => 1234567890 // same as $numbers[123][0]
}
}
但是,根据$number
基于$uid2
生成的方式,您可能看不到任何区别。我的意思是,如果number(123)
返回1234567890
,那么number(456)
可能不会返回1234567890
,所以你可能永远不会遇到会看到差异的情况。
EDIT 2经过进一步思考,我敢打赌您的代码可以大大简化为:
// only selecting uid2 from the table
$result = mysql_query("SELECT DISTINCT uid2 FROM table1 WHERE uid1 = '{$uid1}' ORDER BY /* insert column name here, followed by ASC or DESC */");
$output = array();
while (list($uid2) = mysql_fetch_row($result))
{
$number = number($uid2);
if (strlen($number) > 9)
$output[$uid2]["number"] = $number;
else
$output[$uid2]["email"] = email($uid2);
}
var_dump($output);
上次编辑(希望如此):
// only selecting uid2 from the table
$result = mysql_query("SELECT DISTINCT uid2 FROM table1 WHERE uid1 = '{$uid1}' ORDER BY /* insert column name here, followed by ASC or DESC */");
$numbers = $emails = array();
while (list($uid2) = mysql_fetch_row($result))
{
$number = number($uid2);
if (strlen($number) > 9)
$numbers[$uid2] = $number;
else
$emails[$uid2] = email($uid2);
}
var_dump($numbers);
var_dump($emails);
foreach($array as $uid2){
$uid2 = $array['uid2'];
毫无意义。循环$array中的每个元素,并将元素顺序分配给$uid2,然后终止分配的元素,并从同一数组中提取固定值?
同样,$yfbid设置在哪里?在您的代码示例中,它是未定义的,您尝试将这个未定义的值转换为一个数字,然后检查该数字的字符串长度。为什么不只是strlen($yfbid)
,让PHP来处理转换呢?