在php中遇到数组问题。请帮忙

时间:2013-11-14 06:10:53

标签: php arrays json multidimensional-array

我有这个

    <?php 
    if(isset($_GET['datos'])){
        $xd = $_GET['datos'];

       $datosCompletos = explode(',', $xd);
       $longArreglo = count($datosCompletos);

       for ($i=0; $i < $longArreglo; $i++) { 
            $arregloInformacion = explode('|', $datosCompletos[$i]);

        $longInformacion = count($arregloInformacion);
        $dato = "dato".$i; 
        for ($u=0; $u < $longInformacion; $u++) {
            $dato[$u] = $arregloInformacion[$u];
            echo $dato[$u];

        }
   }
}
?>

这是我发送的数据 Sria.plan | Fnzas | 139 |洛佩兹%20Portillo%20Alcantara%20Jorge%20Ernesto | 29 | 2013年5月1日| 0000188B | T |名义的| 1984年3月19日| 2011-07-16 | H | 341.45 | 6305 | 276 | 153 | 673.4 | 7407.4 | 1185.18 | 8592.58 | 8674.75,Sria.plan.fnzas | Tesoreria | 1538 | Rodriguez的%20Guzman%20Noemi | 58 | 2011-05-01 | 0000188A | T |名义的| 1998年12月16日| 1994年7月9日| M | 1083.78 | 20841 | 276 | 153 | 2127 | 23397 | 3743.52 | 27140.5 | 27222.7,Sria.plan.fnzas | Tesoreria | 1500 | Martinez的%20Rodriguez%20Edith | 23 | 2013年5月1日| 0000188C | B | Hija | 1989年7月25日| 2006-04-16 | M | 438.62 | 8208 | 276 | 153 | 863.7 | 9500.7 | 1520.11 | 11020.8 | 11103

我需要进入

$dato0[0] = Sria.plan
$dato0[1] = Fnzas
$dato0[2] = 139
$dato0[3] = ... //etc

$dato1[0] = Sria.plan.fnzas
$dato1[1] = Tesoreria
$dato1[2] = 1538
$dato1[3] = ... //etc

$dato2[0] = Sria.plan.fnzas
$dato2[1] = Tesoreria
$dato2[2] = 1500
$dato2[3] = ... //etc

但是这个代码,我明白了: SF1L220TT12H362167188ST1R520TT11M122122322ST1M220BH12M482189111

为什么?!

6 个答案:

答案 0 :(得分:1)

要使用变量,您需要将$加倍:

$$dato[$u] = $arregloInformacion[$u];

但是你为什么要使用不同的"dato".$i变量呢?为什么不使用多维数组:

$dato[$i] = $arregloInformacion;

答案 1 :(得分:0)

问题是这段代码:

$dato = "dato".$i; 
for ($u=0; $u < $longInformacion; $u++) {
    $dato[$u] = $arregloInformacion[$u];
    echo $dato[$u];
}

您将$dato初始化为字符串。然后,当您尝试引用$dato[$u]时,PHP认为您的意思是偏移$dato处的字符串($u)的字符。您获得的输出是每个数据点的第一个字母,因此Sria.plan,Fnzas,139等变为SF1 ......

你想这样做(未经测试):

$arrayName = "dato".$i;
$$arrayName = array();
for ($u=0; $u < $longInformacion; $u++) {
    $$arrayName[$u] = $arregloInformacion[$u];
    echo $$arrayName[$u];
}

答案 2 :(得分:0)

<?php 
if(isset($_GET['datos'])){
  $outerArr = explode(',', $_GET['datos']);
  foreach ($outerArr as $items) {
    $data = explode('|', $items);
    $i = 0;
    foreach ($data as $dato) {
      $datos[$i] = $dato;
      echo $datos[$i];
      $i++;
    }
  }
}
?>

没有运行它。

答案 3 :(得分:0)

只需用100%经过测试的代码替换您的代码

<?php
$xd = 'Sria.plan|Fnzas|139|Lopez%20Portillo%20Alcantara%20Jorge%20Ernesto|29|2013-05-01|0000188B|T|Titular|1984-03-19|2011-07-16|H|341.45|6305|276|153|673.4|7407.4|1185.18|8592.58|8674.75,Sria.plan.fnzas|Tesoreria|1538|Rodriguez%20Guzman%20Noemi|58|2011-05-01|0000188A|T|Titular|1998-12-16|1994-07-09|M|1083.78|20841|276|153|2127|23397|3743.52|27140.5|27222.7,Sria.plan.fnzas|Tesoreria|1500|Martinez%20Rodriguez%20Edith|23|2013-05-01|0000188C|B|Hija|1989-07-25|2006-04-16|M|438.62|8208|276|153|863.7|9500.7|1520.11|11020.8|11103';
$datosCompletos = explode(',', $xd);
$longArreglo = count($datosCompletos);

for ($i = 0; $i < $longArreglo; $i++) {
    $arregloInformacion = explode('|', $datosCompletos[$i]);
    $longInformacion = count($arregloInformacion);
    $dato = "dato" . $i;
    for ($u = 0; $u < $longInformacion; $u++) {
        $final_array[$dato][$u] = $arregloInformacion[$u];
    }
}
echo "<pre>";
print_r($final_array);
?>

输出

Array
(
    [dato0] => Array
        (
            [0] => Sria.plan
            [1] => Fnzas
            [2] => 139
            [3] => Lopez%20Portillo%20Alcantara%20Jorge%20Ernesto
            [4] => 29
            [5] => 2013-05-01
            [6] => 0000188B
            [7] => T
            [8] => Titular
            [9] => 1984-03-19
            [10] => 2011-07-16
            [11] => H
            [12] => 341.45
            [13] => 6305
            [14] => 276
            [15] => 153
            [16] => 673.4
            [17] => 7407.4
            [18] => 1185.18
            [19] => 8592.58
            [20] => 8674.75
        )

    [dato1] => Array
        (
            [0] => Sria.plan.fnzas
            [1] => Tesoreria
            [2] => 1538
            [3] => Rodriguez%20Guzman%20Noemi
            [4] => 58
            [5] => 2011-05-01
            [6] => 0000188A
            [7] => T
            [8] => Titular
            [9] => 1998-12-16
            [10] => 1994-07-09
            [11] => M
            [12] => 1083.78
            [13] => 20841
            [14] => 276
            [15] => 153
            [16] => 2127
            [17] => 23397
            [18] => 3743.52
            [19] => 27140.5
            [20] => 27222.7
        )

    [dato2] => Array
        (
            [0] => Sria.plan.fnzas
            [1] => Tesoreria
            [2] => 1500
            [3] => Martinez%20Rodriguez%20Edith
            [4] => 23
            [5] => 2013-05-01
            [6] => 0000188C
            [7] => B
            [8] => Hija
            [9] => 1989-07-25
            [10] => 2006-04-16
            [11] => M
            [12] => 438.62
            [13] => 8208
            [14] => 276
            [15] => 153
            [16] => 863.7
            [17] => 9500.7
            [18] => 1520.11
            [19] => 11020.8
            [20] => 11103
        )

)

答案 4 :(得分:0)

请尝试此操作

  

$ _ GET ['datos'] ='Sria.plan | Fnzas | 139 | Lopez%20Portillo%20Alcantara%20Jorge%20Ernesto | 29 | 2013-05-01 | 0000188B | T | Titular | 1984-03-19 | 2011-07-16 | H | 341.45 | 6305 | 276 | 153 | 673.4 | 7407.4 | 1185.18 | 8592.58 | 8674.75,Sria.plan.fnzas | Tesoreria | 1538 | Rodriguez的%20Guzman%20Noemi | 58 | 2011-05-01 | 0000188A | T |名义的| 1998年12月16日| 1994年7月9日| M | 1083.78 | 20841 | 276 | 153 | 2127 | 23397 | 3743.52 | 27140.5 | 27222.7,Sria.plan.fnzas | Tesoreria | 1500 | Martinez的% 20Rodriguez%20Edith | 23 | 2013年5月1日| 0000188C | B | Hija | 1989年7月25日| 2006-04-16 | M | 438.62 | 8208 | 276 | 153 | 863.7 | 9500.7 | 1520.11 | 11020.8 | 11103' ;

if(isset($_GET['datos'])){
    $xd = $_GET['datos'];

   $datosCompletos = explode(',', $xd);
   foreach($datosCompletos as $key=>$value ){
        $datosCompletos = explode('|', $value);
        foreach($datosCompletos as $newkey=>$newvalue ){
            $datavalue[$key][$newkey] =  $newvalue;
        }
   }

echo "<pre>";
print_r($datavalue);

答案 5 :(得分:0)

这有效

  <?php
  $_GET['datos']="Sria.plan|Fnzas|139|Lopez%20Portillo%20Alcantara%20Jorge%20Ernesto|29|2013-05-01|0000188B|T|Titular|1984-03-19|2011-07-16|H|341.45|6305|276|153|673.4|7407.4|1185.18|8592.58|8674.75,Sria.plan.fnzas|Tesoreria|1538|Rodriguez%20Guzman%20Noemi|58|2011-05-01|0000188A|T|Titular|1998-12-16|1994-07-09|M|1083.78|20841|276|153|2127|23397|3743.52|27140.5|27222.7,Sria.plan.fnzas|Tesoreria|1500|Martinez%20Rodriguez%20Edith|23|2013-05-01|0000188C|B|Hija|1989-07-25|2006-04-16|M|438.62|8208|276|153|863.7|9500.7|1520.11|11020.8|11103";

  if (isset($_GET['datos']))
  {
     $xd = $_GET['datos'];
     $datosCompletos = explode(',', $xd);
     $longArreglo    = count($datosCompletos);

     for ($i = 0; $i < $longArreglo; $i++)
     {
        $arregloInformacion = explode('|', $datosCompletos[$i]);

        $longInformacion = count($arregloInformacion);
        $dato            = "dato" . $i;
        for ($u = 0; $u < $longInformacion; $u++)
        {
           $tmp=$$dato;
           $tmp[$u] = $arregloInformacion[$u];
           $$dato=$tmp;
        }
     }
        var_dump($dato0[0]);
        var_dump($dato0[1]);
        var_dump($dato0[2]);

        var_dump($dato1[0]);
        var_dump($dato1[1]);
        var_dump($dato1[2]);
  }
  ?>