I am trying to get the data from a table into an array, and I want to use a foreach loop not a while for speed issues.
I tried using
function getRowsAssoc()
{
$ret = array();
while (($temp = mysql_fetch_assoc($this->result)) !== FALSE)
{
if (count($temp) > 0)
{
$ret[] = $temp;
}
}
if (count($ret) > 0)
{
return $ret;
}
else
{
return FALSE;
}
}
However this results in to.
Array (
[0] => Array ( [MySKU] => BB1-3500-48 [UPC] => 721343100171 )
[1] => Array ( [MySKU] => BC7-3501-19 [UPC] => 721343103516 )
[2] => Array ( [MySKU] => BC7-3501-95 [UPC] => 721343103523 )
[3] => Array ( [MySKU] => BB1-3502-12 [UPC] => 721343114000 )
[4] => Array ( [MySKU] => bc7-2370-03 [UPC] => 721343121602 )
)
the problem with this is that instead of returning an Assoc array it is adding a numbered array in top of it so now I cannot get the data from the item codes.
I would like to get like this
Array (
[MySKU] => BB1-3500-48 [UPC] => 721343100171
[MySKU] => BC7-3501-19 [UPC] => 721343103516
[MySKU] => BC7-3501-95 [UPC] => 721343103523
[MySKU] => BB1-3502-12 [UPC] => 721343114000
[MySKU] => bc7-2370-03 [UPC] => 721343121602
)
Specify the key:
function getRowsAssoc()
{
$ret = array();
while (($temp = mysql_fetch_assoc($this->result)) !== FALSE)
{
if (count($temp) > 0)
{
$ret[$temp["MySKU"]] = $temp;
}
}
if (count($ret) > 0)
{
return $ret;
}
else
{
return FALSE;
}
}
First of all you should know, how the structure of the result array should look.
For a structure like
Array
(
[MySKU-x] => UPC-x
[MySKU-y] => UPC-y
[MySKU-z] => UPC-z
)
you can use this:
function getRowsAssoc()
{
$return = array();
while (($temp = mysql_fetch_assoc($this->result)) !== false)
{
if (count($temp) > 0)
{
$return[$temp['MySKU']] = $temp['UPC'];
}
}
if (count($return) > 0)
{
return $return;
}
else
{
return false;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.