0

I have problem with fetching json array that i send with post by android app.

PHP code :

<?php
$properties_json = json_decode($_POST['property'], true);
foreach ($properties_json->properties as $pro_element) {
   echo $pro_element->property_id . '<br/>';
   echo $pro_element->property_name . '<br/>';
   echo $pro_element->property_value . '---------';
}

JSON array that i post :

{
  "properties": [
    {
      "property_id": "654",
      "property_name": "Gender",
      "property_value": "Male"
    },
    {
      "property_id": "655",
      "property_name": "Name",
      "property_value": "Amin"
    },
    {
      "property_id": "656",
      "property_name": "Mobile",
      "property_value": "654-451-456"
    }
  ]
}

Error :

Invalid argument supplied for foreach()

enter image description here

8
  • can you use print_r($properties_json->properties)? what is output? Commented Dec 12, 2018 at 9:19
  • 3
    You set true in json_decode, so it's array. not object - $properties_json['properties'] Commented Dec 12, 2018 at 9:20
  • Possible duplicate of json_encode/json_decode - returns stdClass instead of Array in PHP Commented Dec 12, 2018 at 9:20
  • @splash58 There is no difference Commented Dec 12, 2018 at 9:22
  • 1
    Check content of $properties_json by var_dump() Commented Dec 12, 2018 at 9:24

2 Answers 2

2

json_decode($_POST['property'], true); return an array. In your code you're using it as an object. Here is the proper way :

<?php
$properties_json = json_decode($_POST['property'], true);

foreach ($properties_json['properties'] as $pro_element) {
   echo $pro_element['property_id'] . '<br/>';
   echo $pro_element['property_name'] . '<br/>';
   echo $pro_element['property_value'] . '---------';
}

EDIT:

You can check if $_POST['property'] exist and is not null.

<?php
if(isset($_POST['property'])){
    if($_POST['property'] != null and $_POST['property'] != ""){
        $properties_json = json_decode($_POST['property'], true);

        foreach ($properties_json['properties'] as $pro_element) {
            echo $pro_element['property_id'] . '<br/>';
            echo $pro_element['property_name'] . '<br/>';
            echo $pro_element['property_value'] . '---------';
        }
    }else{
        echo "POST['property'] is empty";
    }
}else{
    echo "Missing POST['property']";
}
Sign up to request clarification or add additional context in comments.

1 Comment

You should also check that $properties_json is not NULL though
1

You reference $properties_json->properties as if it were an object, but you converted your JSON to an array by doing json_decode($_POST['property'], true); (see the documentation of json_decode)

You either need to change the true to false or do foreach ($properties_json['properties'] as $pro_element) {

3 Comments

Invalid argument supplied for foreach()
Are you sure you do actually POST anything and that $_POST['property'] contains a JSON array?
If I execute this <?php $_POST['property'] = '{ "properties": [ { "property_id": "654", "property_name": "Gender", "property_value": "Male" } ] }'; $properties_json = json_decode($_POST['property'], false); foreach ($properties_json->properties as $pro_element) { echo $pro_element->property_id . '<br/>'; echo $pro_element->property_name . '<br/>'; echo $pro_element->property_value . '---------'; } I get the expected result: 654<br/>Gender<br/>Male---------

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.