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_dump
s: array(0) { }
and array(1) { [0]=> array(2) { ["uid2"]=> NULL ["email"]=> 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]
}
}
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.
// 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);
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'];
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?