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)