1

I'm trying to parse JSON data from a jquery form which is dynamically created. Users can click the "add step" button to add as many (or as few) form fields as they like (including attaching media links), but I have no idea how to process such data within PHP. Here's some sample form $_POST data I'm expecting to receive:

Array
(
    [1] => Array //1, 2, 3, 4 are the position for this particular 'step' on the page
        (
            [Counter] => 1
            [Title] => step one
            [Step] => description
            [Links] => Array
                (
                    [0] => link 1
                    [1] => link 2
                )

        )

    [2] => Array
        (
            [Counter] => 2
            [Title] => step two
            [Step] => some kind of description
            [Links] => Array
                (
                    [0] => link 2
                )

        )

    [3] => Array
        (
            [Counter] => 3
            [Title] => step three
            [Step] => description (another one)
        )

    [4] => Array
        (
            [Counter] => 4
            [Title] => Step four
            [Step] => a lame description
            [Links] => Array
                (
                    [0] => link 1
                    [1] => link 2
                )

        )

    [tutorial_name] => tutorial name? jesus?
    [tutorial_description] => some useless description
    [tutorial_toolsused] => waste of a tools used
    [tutorial_keywords] => waste of keywords
)

Any idea how I could best go about processing such data (regex, foreach) ? Should I avoid using the post protocol?

Any help would be greatly appreciated!

4
  • 1
    That's not JSON. It looks like a PHP array created by json_decode(). Commented Feb 2, 2013 at 6:40
  • yes, sorry, allow me to reiterate, this is the print_r dump of the $_POST variable Commented Feb 2, 2013 at 7:38
  • What does the form that creates this data look like? The keys of $_POST are the name attributes of the inputs, do you really have name='1[Counter]'? Commented Feb 2, 2013 at 7:43
  • I'll try my best to explain a birds eye view of the code - essentially, each of the dynamically created fields is given a name like "steps[]" or "links[]" and features a data-newinput-id attribute which provides the initial id of the field when it was created to keep the elements together. Later on, the code combines the initial id with its matching fields and pushes them in an object. The numbers at the very beginning of the object represent the position of each of the fields as they are sortable within the page. I would post the code but it's to messy and is a temporary solution for now. Commented Feb 2, 2013 at 8:38

2 Answers 2

1

You can use the is_array() function in PHP to achieve the solution.

Basically, something like this.

foreach($_POST as $value) {
    if(is_array($value)) {
        //process your data here. i.e. the counter, step, links, title
}
Sign up to request clarification or add additional context in comments.

Comments

0

if, by chance, anyone is interested. Thanks again!

<?php
foreach($_POST as $value) {
        if(is_array($value)) {
            //process your data here. i.e. the counter, step, links, title
            echo $value['Position'] . "<br /><br />";
            echo $value['Counter'] . "<br /><br />";
            echo $value['Title'] . "<br /><br />";
            echo $value['Step'] . "<br /><br />";
            if(isset($value['Links'])){
                if(is_array($value['Links'])){
                    foreach($value['Links'] as $link){
                        echo $link . "<br /><br />";
                    }
                }
            }
        }
    }
?>

1 Comment

You won't need to use an inner is_array() for $value['Links'] since you already know it to be an array. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.