0

I have a site with an input form existing out of a few sections. I save every input in an Array, and those arrays are again packed in an array, so that every section has its own Array.

However, I seem to have trouble with looping through. It needs to loop through the section arrays, because those "Section" arrays contain the language where it should be inserted in, in the database.

How can I loop it correctly?

Javascript

     // Start Array to store each arrays
    var preresult = [];

    $(".accordeon-body").each(function() {

        // DATA: Language Short
        var language_short = $(this).data("id");

        // Create Array from inputs, add language key
        var array = $(this).find("textarea").serializeArray();
        array.push({name: 'language', value: language_short});

        // Create a JSON string from it
        var string = JSON.stringify(array);

        var object = {string};
        preresult.push(object);

        event.preventDefault();
    });

    var result = JSON.stringify(preresult);

Result is then sended with an AJAX POST.

ajax.php

 // Get all POST Values
    $string = $_POST['items'];

    // Decode the Main JSON String
    $objects = json_decode($string, true);

    // For Loop the Objects String
    for($i = 0; $i < count($objects); $i++){
        $obj = json_decode($objects[$i]['string'], true);

        // Foreach for every array
        foreach($obj as $arr => $accord) {

            // Foreach to get array Values
            foreach($accord as $data) {

                echo $data['agg_e_txt'];

            }

        }

    }

However, this looping is not working. It returns the error Illegal String Offset. But I'm not sure if I loop through the section arrays, which I need, otherwise I don't know where to put it in the database.

How do I get the wanted result?

UPDATE 1: $objects Print

At request the Objects variable print

    Array
(
    [0] => Array
        (
            [string] => [{"name":"agg_e_txt","value":"Gutentag. Dies ist ein test!"},{"name":"agg_e_rec","value":"Keine"},{"name":"agg_e_spec","value":"Keine"},{"name":"language","value":"de"}]
        )

    [1] => Array
        (
            [string] => [{"name":"agg_e_txt","value":"Hello there! How are U?"},{"name":"agg_e_rec","value":"None"},{"name":"agg_e_spec","value":"None"},{"name":"language","value":"en"}]
        )

    [2] => Array
        (
            [string] => [{"name":"agg_e_txt","value":""},{"name":"agg_e_rec","value":""},{"name":"agg_e_spec","value":""},{"name":"language","value":"fr"}]
        )

    [3] => Array
        (
            [string] => [{"name":"agg_e_txt","value":""},{"name":"agg_e_rec","value":""},{"name":"agg_e_spec","value":""},{"name":"language","value":"es"}]
        )

    [4] => Array
        (
            [string] => [{"name":"agg_e_txt","value":""},{"name":"agg_e_rec","value":""},{"name":"agg_e_spec","value":""},{"name":"language","value":"pt-br"}]
        )

    [5] => Array
        (
            [string] => [{"name":"agg_e_txt","value":""},{"name":"agg_e_rec","value":""},{"name":"agg_e_spec","value":""},{"name":"language","value":"ch"}]
        )

    [6] => Array
        (
            [string] => [{"name":"agg_e_txt","value":""},{"name":"agg_e_rec","value":""},{"name":"agg_e_spec","value":""},{"name":"language","value":"ko"}]
        )

    [7] => Array
        (
            [string] => [{"name":"agg_e_txt","value":""},{"name":"agg_e_rec","value":""},{"name":"agg_e_spec","value":""},{"name":"language","value":"ru"}]
        )

)

In the for-loop I extract the Second JSON String.

UPDATE 2: var $items return So the variable items does now return the subarray with the 4 arrays I need:

    Array
(
    [0] => Array
        (
            [name] => agg_e_txt
            [value] => 
        )

    [1] => Array
        (
            [name] => agg_e_rec
            [value] => 
        )

    [2] => Array
        (
            [name] => agg_e_spec
            [value] => 
        )

    [3] => Array
        (
            [name] => language
            [value] => ru
        )

)

Those 4 Values are the values I need for a query. That Query is my endgoal. How can I achieve this goal? I Don't want to update every value after eachother. (It has to be a single query)

3
  • print_r($object); Commented Mar 9, 2017 at 15:38
  • @AbraCadaver Updated the Question Commented Mar 9, 2017 at 15:43
  • try to replace this line $obj = json_decode($objects[$i]['string'], true); with $obj = $objects[$i]['string'] and check Commented Mar 9, 2017 at 15:49

1 Answer 1

1

You have an array (called $object) of arrays that contain a JSON string. Loop, decode and loop. The decoded JSON string in the loop will be structured like:

Array
(
    [0] => Array
        (
            [name] => agg_e_txt
            [value] => Gutentag. Dies ist ein test!
        )

    [1] => Array
        (
            [name] => agg_e_rec
            [value] => Keine
        )

    [2] => Array
        (
            [name] => agg_e_spec
            [value] => Keine
        )

    [3] => Array
        (
            [name] => language
            [value] => de
        )
)

So you loop that and access the name and value:

foreach($object as $json) {
    $rows = json_decode($json['string'], true);
    foreach($rows as $row) {
        echo $row['name'] . "=" . $row['value'] . "<br>\n";
    }
}

I guess alternately you could loop the decoded JSON and access like so:

foreach($object as $json) {
    $rows = json_decode($json['string'], true);
    foreach($rows as $row) {
        foreach($row as $key => $val) {
            echo $key . "=" . $val . "<br>\n";
        }
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I Decode the Array within the array in the for-loop. Do you mean that I should replace my for loop with a foreach?
Yes, this is how I would do it, much cleaner and easier to follow.
Thanks. This indeed, gives me a result. Does he loop every "subarray" like your example? I can't check this with a print. (Because that will ofcourse print every Array)
I added an alternate to loop the decoded JSON in the inner loop.
What if I want to insert all 4 values in a single update query? Somehow I need to read out the key to tell them which column. I need to insert / update the 4 "values" in a single query. I Updated the question for a better explanation.

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.