How should I parse the PHP given in a matrix?

  • jalleyne
    Asked on June 22, 2015 at 12:33 PM

    I'm trying to parse the data in a matrix form question to an individual entry in a mySQL database. Though I understand how to parse strings in PHP, the output for that question (using the method from this link: http://www.jotform.com/help/51-How-to-Post-Submission-Data-to-Thank-You-Page) Comes out as an array with one entry:

    How should I parse the PHP given in a matrix? Image 1 Screenshot 20

    I need to parse each entry within the array as an entry into a database. (ie explode() the elements in Array and attach them to variables in the database, eventually getting to the point so that a $chain variable would hold a value "delta / gamma" string. 

    It seems as if the " " either aren't registering on the text editor I'm using or the "" are one character off. Can you suggest a way of solving the problem?

  • KadeJM
    Replied on June 22, 2015 at 2:54 PM

    To my understanding I believe you mean you wish to separate your arrays at the moment.

    Are you trying to just separate the arrays into individual lines each? 

    Or were you intending to split those up and have those then become attached with the variables that are already associated to be pulled into your database?

    Please allow us some time to look into this and if we're able to do so then we'll update you here.

     

  • jalleyne
    Replied on June 22, 2015 at 3:07 PM

    My intention is to take the long string within the arrays and allocate them to variables defined in the database. So the really rough process would look something like:

    given: 

    [sampleinformation] =>

       Array ([0] => ["long string of things"] [1] => ["long string of things"]....)

     

    loop through each entry of the array 'sampleinformation'.

    For each index in the array, parse the long string held at that index. 

    allocate each token of the string (ie the tokens " ", "024 JRG" , "7.7" in the original example) to a variable in a mySQL database. So that a table 'sample' in the database refers to an element in the 'sampleinformation' array and the rows of that mySQL table refer to the tokens parsed from the array. 

     

  • BJoanna
    Replied on June 22, 2015 at 5:15 PM

    Did you try some code for example you provided? To iterate through array you can use foreach function. Foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable. There are two syntaxes:

    foreach (array_expression as $value)

        statement

    foreach (array_expression as $key => $value)

        statement

     

    There is an interesting article on stackoverflow how to iterate over array and split elements inside of array with delimiters. 

    http://stackoverflow.com/questions/16476140/php-array-explode-some-comma-separated-elements-into-new-items 

    You can modify this example in order to adjust it to work for you:

    $format_Arr = array("test1","test2,test2.1","test3","test4,test5,test6,test7");

    $formated_arr = array();

    foreach($format_Arr as $arr){

    $arr1 = explode(",",$arr);

    if(count($arr1)>1){

        $formated_arr = array_merge($formated_arr,explode(',',$arr));

    }else{

        $formated_arr[]= $arr;

    }

    }

    print_r($formated_arr);

    Hope this help. Let us know if you need further assistance.