Mysql的抽取PHP数组 [英] Mysql extracting array in Php

查看:93
本文介绍了Mysql的抽取PHP数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是低于我的MySQL阵列提取的问题:

What is the problem with my mysql array extraction below:

        $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转换为数组。
必须有上述code的一个问题。
这是我得到的的var_dump 取值:阵列(0){}
阵列(1){[0] =>阵列(2){[UID2] => NULL [电子邮件] => NULL}}

推荐答案

我觉得这是你想要做什么:

I think this is what you're trying to do:

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



修改在回答意见问题:

根据您所使用的逻辑,其结果很可能是相同的。上面我举的例子将允许这种情况下为 $号,而你的榜样将不会:

Based on the logic you are using, the result will probably be the same. My example above will allow this scenario for $numbers, while your example will not:

array(2)
{
    [123] => array(2)
    {
        [0] => 1234567890,    // same as $numbers[456][0]
        [1] => 9876543210
    },
    [456] => array(1)
    {
        [0] => 1234567890    // same as $numbers[123][0]
    }
}

但基于的方式, $号根据 $ UID2 产生,你可能不会看到任何区别。我的意思是,如果数(123)收益 1234567890 ,那么数(456 )可能不会返回 1234567890 ,所以你可能不会永远碰到这样一个场景,你会看到一个区别。

But based on the way that $number is generated based on $uid2, you probably won't see any difference. What I mean is that, if number(123) returns 1234567890, then number(456) probably will not return 1234567890, so you probably wouldn't ever run into a scenario where you would see a difference.



修改2 在思考这个多一些之后,我敢打赌,你的code可以大大简化这个:


EDIT 2 After thinking about this some more, I'm betting that your code can be greatly simplified to this:

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

这篇关于Mysql的抽取PHP数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆