0

I have an object or array like below: "[{},{},{},{},{},{},{},{},{\"subject\":\"Math\"},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{\"subject\":\"Chemistry\"},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}]"

as well: [{},{},{},{},{},{},{},{},{"subject":"Math"},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{"subject":"Chemistry"},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}]

when i am iterating it using foreach loop it show " Invalid argument supplied for foreach() in C:\wamp64\www\eschool\cms\admin\scripts\time-table-add-submit.php on line 11 " May be due to the empty value at the beginning and at the end. but I want to iterate it somehow how to do that. Heres my code: JavaScript:

var timeTableSub = [
       {"subject": timeTableSub1},
       {"subject": timeTableSub2},
       ... Like Wise....
       {"subject": timeTableSub42}
];
var jsonArray = JSON.stringify(timeTableSub);

$.ajax({
        url: "scripts/time-table-add-submit.php",
        type: "POST",
        dataType: 'html',
        data:{
            'arrayList': jsonArray
        },
        success: function (data) {
            data = $.trim(data);
            $("#tempDiv").html(data);
        }
    });

PHP:

<?php
$arrayList = json_encode($_POST['arrayList'], TRUE);

echo $arrayList."<br>";
$newArray = json_decode($arrayList);
echo $newArray."<br>";




foreach ($newArray as $key => $value)
{
      echo $key . " => " . $value . "<br>";
}

?>

7
  • ANd what's the point of encoding and decoding data? Commented May 20, 2020 at 12:08
  • just to check the data i have, nothing else. Commented May 20, 2020 at 12:10
  • 1
    you don't need $arrayList = json_encode($_POST['arrayList'], TRUE); because what you've sent from the browser is already JSON. And therefore you should be writing $newArray = json_decode($_POST['arrayList']); instead later on as well Commented May 20, 2020 at 12:16
  • Demo: sandbox.onlinephpfunctions.com/code/… Commented May 20, 2020 at 12:27
  • thanx @ADyson now I'm getting the result but empty value lost. Is there a way it should print like "subject: " if it is empty Commented May 20, 2020 at 12:33

1 Answer 1

1

You appear to be stringifying the data twice, once on the front end during the request with JSON.stringify and once more when receiving the POST object by encoding it once more.

Removing either would fix this issue.

Sign up to request clarification or add additional context in comments.

7 Comments

following php is not working too. <?php $arrayList = $_POST['arrayList']; foreach ($arrayList as $key => $value) { echo $key . " => " . $value . "<br>"; } ?>
could you please send me the code to do it right. Thanx
Okay so imagine encoding or stringifying your object as packing the data in a box. What you were doing by stringifying/encoding twice is packing the data into 2 boxes, but only unpacking once (with your json_decode). When I said to remove either the JSON.stringify or json_encode, you removed the json_encode (packing) AND the json_decode (unpacking). What that meant was you went from unpacking it once from 2 boxes (1 box still left), to unpacking it NO times from 1 box (1 box still left), i.e. same issue. Adding your json_decode back in would fix it.
I understood what you advised me. for example I'm doing this: hello=encode1=encode2=decode2 and the data is still in the form of ecoded1.
Now i'll doing it right and getting what I want as specified.
|

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.