0

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," 
7
  • 3
    What you are looking for is json_decode(). Commented Aug 24, 2014 at 12:43
  • i have used decode, but when i used decode, it become very complicated, i received NULL when i tested JSON with var_dump. first time i create this the data still appear on $nama and $jumlah, then there's error offset. but now.. i do not know what im doing, its become more difficult $nama and $jumlah does not appear anymore..just error offset appear. Commented Aug 24, 2014 at 15:27
  • $pecah = json_decode($encoding, true); this way the json will be decoded into an array which will make it less complicated. Commented Aug 24, 2014 at 15:33
  • many thanks any people for feedback, i tried decode the $encoding, but its return NULL value.., Commented Aug 24, 2014 at 15:47
  • What's the value of $json? Do a var_dump($json). Commented Aug 24, 2014 at 15:48

0

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.