0

嗨我有以下数组

array(4) {
  [0]=>
  array(3) {
    ["id_acreditado"]=>
    string(3) "174"
    ["cantidad"]=>
    string(7) "4008.00"
    ["acreditado"]=>
    string(27) "Olga Olivia Lucio Hernandez"
  }
  [1]=>
  array(3) {
    ["id_acreditado"]=>
    string(3) "175"
    ["cantidad"]=>
    string(7) "4008.00"
    ["acreditado"]=>
    string(23) "Enrique Carranco Vences"
  }
  [2]=>
  array(3) {
    ["id_acreditado"]=>
    string(3) "176"
    ["cantidad"]=>
    string(7) "4008.00"
    ["acreditado"]=>
    string(32) "Juana Patricia Contreras Paredes"
  }
  [3]=>
  array(3) {
    ["id_acreditado"]=>
    string(3) "177"
    ["cantidad"]=>
    string(7) "4008.00"
    ["acreditado"]=>
    string(17) "Noemi Cruz Campos"
  }
}

我想用上述数组的一些值和不同的索引创建一个二维数组。我正在使用 foreach 循环来实现这一点。

$j=1;
foreach($acreditados as $acreditado){
    $tmp['oneCol'] = $j;
    $tmp['twoCol'] = $acreditado['acreditado'];
    $tmp['threeCol'] = $acreditado['cantidad'];
    $info['fourCol'] =$acreditado['id_acreditado'];
    $info[]=$tmp;
    $j++;
}

$tmp 是一个辅助一维数组,最近添加为双维 $info 数组的一行,但是我没有得到预期的输出。我想要以下内容作为输出:

array(4) {
      [0]=>
      array(3) {
        ["oneCol"]=>
        int(1)
["twoCol"]=>
        string(27) "Olga Olivia Lucio Hernandez"
        ["threeCol"]=>
        string(7) "4008.00"
["fourCol"]=>
        string(3) "174" 
      }
      [1]=>
      array(3) {
 ["oneCol"]=>
        int(2)
["twoCol"]=>
        string(23) "Enrique Carranco Vences"
        ["threeCol"]=>
        string(7) "4008.00"
["fourCol"]=>
        string(3) "175"
      }
      [2]=>
      array(3) {
 ["oneCol"]=>
        int(3)
       ["twoCol"]=>
        string(32) "Juana Patricia Contreras Paredes"
        ["threeCol"]=>
        string(7) "4008.00"   
 ["fourCol"]=>
        string(3) "176"
      }
      [3]=>
      array(3) {
 ["oneCol"]=>
        int(4)
       ["twoCol"]=>
        string(17) "Noemi Cruz Campos"
        ["threeCol"]=>
        string(7) "4008.00"
 ["fourcol"]=>
        string(3) "177"
      }
    }
4

1 回答 1

1

你有一个代码错误

线

$info['fourCol'] =$acreditado['id_acreditado'];

应该

$tmp['fourCol'] =$acreditado['id_acreditado'];

所以代码显示为:(添加了 $tmp 数组重置)

$j=1;
foreach($acreditados as $acreditado){
    $tmp = array();
    $tmp['oneCol'] = $j;
    $tmp['twoCol'] = $acreditado['acreditado'];
    $tmp['threeCol'] = $acreditado['cantidad'];
    $tmp['fourCol'] =$acreditado['id_acreditado'];
    $info[] = $tmp;
    $j++;
}
于 2012-05-31T15:35:24.040 回答