-2

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

8
  • What the purpose of this code? $number = number($yfbid);if(strlen($number) > 9){ , What? Commented Feb 29, 2012 at 19:30
  • sensible variable names would help a lot. Commented Feb 29, 2012 at 19:32
  • 1
    possible duplicate of php - create assciative array while loop Commented Feb 29, 2012 at 19:32
  • 1
    what are you expecting to accomplish? I see several issues but I'm not even sure where to begin. Commented Feb 29, 2012 at 19:34
  • no it is not a possible duplicate, there the question was about the loop, now that that is covered, the question is about extracting the array from mysql. regarding the variables - you are right i had some of them wrong when i copied the code, now it's editted Commented Feb 29, 2012 at 19:34

3 Answers 3

2

Look at this 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);
            }
        }

You iterate all positions of array and call them $uid2. Then, in the first line you do $uid2 = $array['uid2'];. You loose your array position.

Maybe you wanted something like: $var = $uid2['uid2'];

Sign up to request clarification or add additional context in comments.

Comments

1

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

6 Comments

this looks great! thanks. but shouldn't the in array function look like this: !in_array($number, $numbers)
oh awesome!! I was hoping/ thinking that this may indeed can be simplified directly from the mysql. thanks!
mysql_fetch_row($result) will return an array. If you use list($uid2) = mysql_fetch_row($result), it gets the first value from the numeric array and assigns it to the variable $uid2. If you wanted the first two values from an array, you could use list($value1, $value2) = $array;, or if you just wanted the third value from an array, you could do list( , , $value3) = $array;. See the PHP documentation for further explanation.
why do my arrays look like this { [345678]=> array(1) { ["email"]=> string(19) "[email protected]" }, how do I get it to be just: { [345678]=> "[email protected]" }
I had both numbers and emails going into one array. See updated answer again. This will give you two separate arrays in the structure you want.
|
0
foreach($array as $uid2){
    $uid2 = $array['uid2'];

makes no sense. Loop over each element in $array and assign the elements to $uid2 sequentially, then kill that assigned and pull a fixed value from the same array?

As well, where is $yfbid set? From your code sample, it's undefined, and you try to convert this undefined value to a number, then check the string length of that number. Why not just strlen($yfbid) and let PHP handle the conversion?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.