如何为php变量赋值数组

时间:2013-06-19 16:30:28

标签: php

在下面的代码中,如何将数组值分配给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[];
}

2 个答案:

答案 0 :(得分: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"];
}

答案 1 :(得分:0)

$name = $customername[count($customername)-1];

每个变量都是如此。

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

$len = count($customername)-1;

然后对于每个变量都这样写:

$name = $customername[$len];