I tried to explode some JSON but I got an error, "Undefined offset: 1."
I don't know how to fix it. I tried to var_dump
the JSON content, it shows the string word I need. However, when I try to explode
and foreach
it, I get an error. Please help me.
This is the code:
$json = file_get_contents('http://localhost/project/services/grafik');
$encoding = json_encode($json,true);
$pecah = explode(",",$encoding);
// var_dump($pecah);
foreach($pecah as $row => $data)
{
$p1 = explode(',', $data);
$p2[$row] = explode(',',$p1[0]);
$nama=$p2[$row][0];
$jumlah=$p2[$row][1];
}
This is the JSON content:
"Windows XP Prof ,106, Windows 7 ,43, Windows Vista Business ,13, Windows 7 Prof ,21, B4 ,1, Windws 7 Prof ,1, IPCOP ,1, Windows 2003 server ,4, Windows 2008 server ,1, Windows 2008 R2 ,4, NULL ,9, Windows 8 Prof ,6,operating,1,"
json_decode()
.$pecah = json_decode($encoding, true);
this way the json will be decoded into an array which will make it less complicated.$json
? Do avar_dump($json)
.