dongmei9203 2012-02-29 19:26
浏览 38
已采纳

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

There must be something I need to do to convert the "Resource" from mysql into an array. There must be a problem with the above code. This is what I am getting on the var_dumps: array(0) { } and array(1) { [0]=> array(2) { ["uid2"]=> NULL ["email"]=> NULL } }

  • 写回答

3条回答 默认 最新

  • duanpaxin3531 2012-02-29 19:52
    关注

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


    EDIT Answering question from comments:

    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]
        }
    }
    

    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.


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


    LAST EDIT (Hopefully):
    // 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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法