I have a multidim array I need to pass from one script to another as part of a function call.

The array size will vary, but has the form:

$chart[0][0] = "";
$chart[0][1] = "100";
$chart[0][2] = "110";
$chart[0][3] = "120";
$chart[0][4] = "130";
$chart[1][0] = "CNC";
$chart[1][1] = 83;
$chart[1][2] = 11;
$chart[1][3] = 3;
$chart[1][4] = 3;

The call to the second script is as follows:

print InsertChart("charts.swf", "charts_library", "sample.php?data=".$chart,650, 400);

The parameters of the above function are as follows:
charts.swf //inserts flash file into web page
charts_library //tells function where to find charts_library
sample.php?data=".$chart //tells it where to get chart parameters and data
650,400 //specifies height and width

//On the called script I try to receive the array as follows:
$tableData = $_REQUEST['chart_data'];

//I then try to assign it to a $chart array in this file
$chart['chart_data'] = $tableData;

//I also tried array_push($chart, $tableData);  but got nowhere.

But something isn't working as the data is not added to the chart. If I place the $chart data in the sample.php file everything works great so I know all the other pieces are working. I seem to just be doing something wrong getting $chart['chart_data'] from one script to the other. I don't know if it matters but I also add other dimensions to the $chart array once in the sample.php file such as $chart['chart_type'].

    The expression "sample.php?data=".$chart is probably just going to result in the string sample.php?data=array. You could possibly [man]serialize/man the array then [man]urlencode/man the result of that for use in the URL query string, then [man]unserialize/man it in the receiving script. However, it seems to me it would be a lot easier to just use sessions and save it to/retrieve it from the $_SESSION array.

    script 1:

    <?php
    session_start();
    // ...blah blah blah
    $_SESSION['chart'] = $chart;
    // ...yadda yadda yadda...
    ?>
    

    script 2:

    <?php
    session_start();
    if(!is_array($_SESSION['chart']))
    {
       // raise an error since we did not get the chart array
       // die() or exit() if this is a fatal error condition
    }
    // access array data from $_SESSION['chart'], or copy it to $chart if that's easier:
    $chart = $_SESSION['chart'];
    
    // ...rest of script...
    ?>
    

      NogDog,

      You nailed it perfectly. I monkeyed around with serialize(), but it seemed like a kludgy way to do it. I haven't tackled sessions yet, but I tucked your examples into my code and it worked right off the bat. Glad I asked as I was making things way more complicated than they needed to be. Thanks for the solution and for introducing me to sessions!

        Write a Reply...