0

I am trying to explode a string into a list based off comma's, I have done this before but for some reason this not working now.

Just for reference, the line where I called echo echo $members[6]; returns 2009, 7, 146, 277.00, Company 6 so I know everything is being stored properly into $members

I also know from calling $arrlength it returns the correct size of strings that I populate into $members.

So I have the right data and it is formatted correctly yet the ONLY lines that print on my index.php is the echo $members[6]; statement and since I have a VERY long scroll bar I am also guessing that echo "<br><br>"; prints too.

Any help would be greatly appreciated.

<html>
<head>
</head>
<body>

<?php
$str_data = file_get_contents("file.json");
$data = json_decode($str_data,true);

foreach ($data as $key => $value) 
{   
    foreach ($value as $k => $v) 
    { 
       $t = "";
        foreach ($v as $kk => $vv) 
        { 
             $t = $t . ", " . $vv;         
        }
        $members[] = substr($t, 2); //cuts out initial comma + space      
    }
}
$arrlength=count($members);

echo $members[6]; // returns 2009, 7, 146, 277.00, Company 6

for ($i=0; $i<$arrlength; $i++) 
{
     list($year, $rank, $revenue, $profit, $company) = explode(",", $members[i]);
     $year = trim($year);
     $rank = trim($rank);
     $revenue = trim($revenue);
     $profit = trim($profit);
     $company = trim($company);

     echo $year;
     echo $rank;
     echo $revenue;
     echo $profit;
     echo $company;
     echo "<br><br>";   
     }
?> 
</body>
</html>
Austin
  • 3,010
  • 23
  • 62
  • 97
  • 1
    Don't forget what language you're working in ;) – Jonast92 May 24 '14 at 23:39
  • You really need to turn `error_reporting` on in your php.ini (http://www.php.net/manual/en/errorfunc.configuration.php#ini.error-reporting). You wouldn't be asking that question if you did – Ben May 24 '14 at 23:41

2 Answers2

2

It does not work because you missed the $:

explode(",", $members[i]);

Should be:

explode(",", $members[$i]);

You should turn on errors.

Community
  • 1
  • 1
Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52
1

You are missing the $ in the explode function:

explode(",", $members[$i])

The code doesn't know what element to explode and if you had warnings/errors turned on it would have told you that :)

As soon as I added that the code worked as expected.

Fluffeh
  • 33,228
  • 16
  • 67
  • 80