dongli7870 2017-03-09 15:38
浏览 54
已采纳

数组读取中的数组

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)

  • 写回答

1条回答 默认 最新

  • doulanyan6455 2017-03-09 15:51
    关注

    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>
    ";
        }
    }
    

    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>
    ";
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效