I'd like to open a .js file that contains a multi-dimensional array and parse it
into a PHP array.

.js array:

capitol = new Array("Capitol",
  new Array("Monday, May 14",
            "Zephyrs","6","8","3","0",
            "Bombers","7","8","1","0"),

  new Array("Wednesday, May 16",
            "Giants","6","0","9","1",
            "Orioles","8","3","6","0"),

  new Array("Thursday, May 17", 
            "Brewers","13","2","6","1", 
            "Astros","0","0","7","1")
)

using this code:

$datafile = fopen("capitol.js","r");
$js       = fread($datafile, 1000000);
$js       = str_replace('new Array', 'array', $js);
$js       = preg_replace('/(\w+)\s*=/', '$\1 =', $js);
$js       = $js.';';

// echo $js;  // echos correct PHP code!!!
eval($js);
var_dump($capitol);

I get:
Parse error: ...unexpected $ in rendercapitol.php(8) : eval()'d code on line 13

It echos the correct PHP code:

$capitol = array("Capitol", array("Monday, May 14", "Zephyrs","6","8","3","0", "Bombers","7","8","1","0"), array("Wednesday, May 16", "Giants","6","0","9","1", "Orioles","8","3","6","0"), array("Thursday, May 17", "Brewers","13","2","6","1", "Astros","0","0","7","1") );

    I copied-and-pasted your code, and it worked fine for me. (PHP 5.2.1 on Windows)

      Workin' great in PHP 4.4.7 and 5.2.2 on Windows (ISAPI/CGI, respectively)...

        mrbaseball34 wrote:
        $js       = fread($datafile, 1000000); 

        Psst: there's a function called [man]file_get_contents[/man] now.

          Write a Reply...