0

在以下代码中,如何将数组值分配给 php 变量。

while($objResult = mysql_fetch_array($objQuery))
{
$cutomername[] = $objResult["firstname"];
$mailid[] = $objResult["mailid"];
$address[] = $objResult["address"];
$phonenumber[] = $objResult["phonenumber"];
$items[] = $objResult["itemname"];
$quantity[] = $objResult["quantity"];
$total[] = $objResult["grandtotal"];

    $name = $cutomername[];
    $mail = $mailid[];
    $addr = $address[];
    $phnum = $phonenumber[];
    $itm = $items[];
    $qty = $quantity[];
    $tot = $total[];
}
4

2 回答 2

1

您可以使用以下内容一次分配两个变量:

while($objResult = mysql_fetch_array($objQuery))
{
  $name = $cutomername[] = $objResult["firstname"];
  $mail = $mailid[] = $objResult["mailid"];
  $addr = $address[] = $objResult["address"];
  $phnum = $phonenumber[] = $objResult["phonenumber"];
  $itm = $items[] = $objResult["itemname"];
  $qty = $quantity[] = $objResult["quantity"];
  $tot = $total[] = $objResult["grandtotal"];
}
于 2013-06-19T16:37:52.250 回答
0
$name = $customername[count($customername)-1];

对于每个变量,依此类推。

或者,您可以在分配之前存储$total长度

$len = count($customername)-1;

然后对于每个变量,只需这样写:

$name = $customername[$len];
于 2013-06-19T16:32:43.900 回答