user2930137
user2930137

Reputation: 63

having problems with array in php. Help please

I have this

    <?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];

        }
   }
}
?>

This is data which I send 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

I need to get in

$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

But con this code, I get this: SF1L220TT12H362167188ST1R520TT11M122122322ST1M220BH12M482189111

Why?!

Upvotes: 0

Views: 81

Answers (6)

fredtma
fredtma

Reputation: 1053

This works

  <?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]);
  }
  ?>

Upvotes: 0

Siraj Khan
Siraj Khan

Reputation: 2348

Please try this it will work

$_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);
   foreach($datosCompletos as $key=>$value ){
        $datosCompletos = explode('|', $value);
        foreach($datosCompletos as $newkey=>$newvalue ){
            $datavalue[$key][$newkey] =  $newvalue;
        }
   }

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

Upvotes: 0

Veerendra
Veerendra

Reputation: 2622

Just replace your code With this one 100% tested

<?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);
?>

Output will be

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
        )

)

Upvotes: 0

tamilps2
tamilps2

Reputation: 46

<?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++;
    }
  }
}
?>

Did'nt run it.

Upvotes: 0

elixenide
elixenide

Reputation: 44831

The problem is this code:

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

You initialize $dato as a string. Then, when you try to refer to $dato[$u], PHP thinks you mean the character of a string ($dato) at offset $u. The output you are getting is the first letter of each of your data points, so Sria.plan, Fnzas, 139, etc. becomes SF1...

You want to do this (untested):

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

Upvotes: 0

Barmar
Barmar

Reputation: 781058

To use a variable variable you need to double the $:

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

But why are you doing it with different "dato".$i variables? Why don't you use a multidimensional array:

$dato[$i] = $arregloInformacion;

Upvotes: 1

Related Questions