0

我需要帮助处理数组。我有一组来自 MySQL 查询的数据。在 for 循环中打印后,我得到以下 array_flip:

    Array ( 

   [Duru 60] => 0 
   [Maxwell 50] => 1 
   [Fashanu 70] => 2 
   [Nwankwo 80] => 3 
   [Obi 0] => 4 

   ) 

数组值是 2 个字段名称和总分的组合。我想要实现的是这样的数组:

   Array (

   [Duru 60] => 60 
   [Maxwell 50] => 50 
   [Fashanu 70] => 70 
   [Nwankwo 80] => 80 
   [Obi 0] => 0 

   )

我想要实现的是将默认数组数字键(0,1,2,3,4)更改为从查询中获得的总分。

这是给出第一个数组块的代码:

PHP 代码开始

    $dataA = array();

    foreach($data as $key => $val){

$dataC = $val['lastname']." ".$val['total'];
array_push($dataA,($dataC));

     }
     $dataD = (array_flip($dataA));

     print_r($dataD);
4

3 回答 3

1
$dataA = array();
foreach($data as $key => $val){
    $dataK = $val['lastname']." ".$val['total'];
    $dataV = $val['total'];
    $dataA[$dataK] = $dataV;
}
print_r($dataA);
于 2012-07-31T14:35:19.330 回答
1

尝试这个:

    $dataA = array();

    foreach($data as $key => $val){

           $dataC = $val['lastname']." ".$val['total'];

           $dataA[$dataC] = $val['total'];

     }

     print_r($dataA);
于 2012-07-31T14:25:13.877 回答
1

为什么你不能这样做:

$newData = array();
foreach($data as $key => $val) {
  $newData[$val['lastname'] . ' ' . $val['total']] = $val['total'];
}
print_r($newData);
于 2012-07-31T14:26:41.670 回答