1

I use a library for Synchronize a local WebSQL DB to a server specifically https://github.com/orbitaloop/WebSqlSync. I use PHP: 5.4.7 this is json var $json

{
"info":"",
"data":{
    "clientes":[],
    "conceptos_gastos":[],
    "formaspago":[{
        "idFormaPago":7,
        "FormaPago":"quwuw",
        "Dias":1,
        "Cuotas":1,
        "last_sync_date":null
    }],
    "listaprecios":[],
    "producto":[],
    "repartidores":[],
    "tipodocumento":[],
    "vehiculos":[],
    "zonas":[]
}
}

when I try to get the values

$js=json_decode($json,true);
foreach ($js['formaspago'] as $row ) {
   echo $row["Formapago"];

}

I get this error:

Invalid argument supplied for foreach

any help is appreciated

3
  • 5
    Add your php code as well. Commented Jul 15, 2013 at 21:29
  • 4
    Hint: $js['data']['formaspago'] Commented Jul 15, 2013 at 21:35
  • You should learn to debug your code. Try to add this right before the foreach line, it will pretty-print your array. It's quite obvious that the key $js['formaspago'] doesn't exist, it's really $js['data']['formaspago']... echo '<pre>'; var_dump($js); Commented Jul 15, 2013 at 21:37

2 Answers 2

2

use json_decode

//array
$data = json_decode($json, true);
foreach($data["data"]["formaspago"] as $row) {
    echo $row["Formapago"];
}

Thanks @JimL for the hint.

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

1 Comment

json_decode($json) returns an object. json_decode($json, true) returns an array :)
1

You're not passing a valid array to the foreach loop. However, even if you parse the json you've supplied, a foreach won't work because it's an object not an array.

$json = '{
"info":"",
"data":{
    "clientes":[],
    "conceptos_gastos":[],
    "formaspago":[{
        "idFormaPago":7,
        "FormaPago":"quwuw",
        "Dias":1,
        "Cuotas":1,
        "last_sync_date":null
    }],
    "listaprecios":[],
    "producto":[],
    "repartidores":[],
    "tipodocumento":[],
    "vehiculos":[],
    "zonas":[]
}
}';

$obj = json_decode($json);

print $obj->info will return an empty string.

$obj->data is an object that has various properties some of which are empty arrays. Passing these into the foreach should work. i.e.

foreach($obj->data->clients as client) {
  // Do something here
}

Further, $obj->data->formaspago has exactly one element in the array, which is an object. You can access this object with a foreach loop:

foreach($obj->data->formaspago as formaspago) {
  print formaspago->idFormaPago; // Will print 7
  print formaspago->FormaPago; // Will print "quwuw"
  print formaspago->Dias; // Will print 1
  print formaspago->Cuotas; // Will print 1
  print formaspago->last_sync_date; // Will print nothing
}

1 Comment

Your answer helped me a lot but had to treat it like an array: $obj= json_decode($json, true);

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.