数组读取

时间:2017-03-09 15:38:07

标签: php jquery arrays ajax

我有一个网站,其中包含几个部分的输入表单。我将每个输入保存在一个数组中,并且这些数组再次打包在一个数组中,这样每个部分都有自己的数组。

然而,我似乎遇到了循环问题。它需要遍历剖面数组,因为那些" Section"数组包含应在其中插入的语言。

如何正确循环?

的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);

然后使用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'];

            }

        }

    }

但是,这种循环不起作用。它返回错误Illegal String Offset。但是我不确定我是否需要遍历我需要的部分数组,否则我不知道将它放在数据库中的哪个位置。

如何获得想要的结果?

更新1:$ objects打印

根据要求,对象变量打印

    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"}]
        )

)

在for循环中,我提取第二个JSON字符串。

更新2:var $ items return 所以变量项现在返回带有我需要的4个数组的子数组:

    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
        )

)

这4个值是查询所需的值。那个查询是我的终结。我怎样才能实现这个目标?我不想在彼此之后更新每个值。 (它必须是单个查询)

1 个答案:

答案 0 :(得分:1)

您有一个包含JSON字符串的数组(称为$object)数组。循环,解码和循环。循环中解码的JSON string的结构如下:

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
        )
)

所以你循环播放并访问namevalue

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

我猜你可以循环解码的JSON并像这样访问:

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