0

My json data is:

{"jsondata":[[1,7,16,29,41,45],[2,12,21,31,36,45]]}

There are 2 sets of numbers and each set has 6 different number. And the number of total sets can be more according to user's input. So, there may be 3 sets which means 18 numbers, etc. If I copy/paste that in to http://json.parser.online.fr/ , I see no error. So, json data is correct, I guess.

Second, here is my PHP file:

<?php
$input = file_get_contents('php://input');
$result=json_decode($input);
var_dump($result);
?>

That works fine too. I mean, when I run these lines, in the chrome's developer tool, I can see these:

object(stdClass)#1 (1) {
  ["jsondata"]=>
  array(2) {
    [0]=>
    array(6) {
      [0]=>
      int(1)
      [1]=>
      int(7)
      [2]=>
      int(16)
      [3]=>
      int(29)
      [4]=>
      int(41)
      [5]=>
      int(45)
    }
    [1]=>
    array(6) {
      [0]=>
      int(2)
      [1]=>
      int(12)
      [2]=>
      int(21)
      [3]=>
      int(31)
      [4]=>
      int(36)
      [5]=>
      int(45)
    }
   }
  }

So far, I think, what I have to understand is json data is correct and php can respond it back. So, how can I reach this numbers and assing them into another variable in PHP? My actual aim is send them into the mysql. If I can get them, it is easy.

Some says use key/value pair thing but the problem is, my json data creates itself according to user's input. User clicks some numbers and then the number get appended into the json data. And it continues each of six clicks:

function createjson(){
var c=1;
var json='{"jsondata":[[';
$("#kup td").each(function(){
    if($(this).html()!="" && $(this).html()!="SELECTED NUMBERS")
    {
        json+= parseInt($(this).html())+',';
        if($(this).index()%5==0 && $(this).index()!=0) //sixth number selected
        {
            json=json.substring(0,json.length-1);                       
            json+="],["
        }                       
    }               
});
json=json.substring(0,json.length-3);
json+="]]}";
return json;
};

I thought I found my answer here but it didn't work either or I did somethings wrong. I am very newbie on these json stuffs, so please don't force me to change whole thing. There must be a way to reach this values. So, please save me guys :-)

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Who Cares
  • 205
  • 5
  • 18
  • You can reach them just as you would any other array values. – Jay Blanchard May 21 '14 at 12:18
  • @JayBlanchard can you give an example syntax please? I really need this... Because I did everything I know with my PHP knowledge, but I am about the cry. I am serious :/ – Who Cares May 21 '14 at 12:19
  • You show what you use to create the JSON. What have you tried to get values from the JSON? Here are some examples - http://stackoverflow.com/questions/4383914/how-to-get-single-value-from-php-array – Jay Blanchard May 21 '14 at 12:23
  • @WhoCares Accessing array elements and object members is _really_ basic stuff. Reading a beginner's book or tutorial on PHP would probably be helpful for you. But for now, read the docs on [Arrays](http://www.php.net/manual/en/language.types.array.php) and [Objects](http://www.php.net/manual/en/language.types.object.php). (It will teach you how to access object members and array items, but also a bit more about them.) – Carsten May 21 '14 at 12:24
  • Guys, you are right, there is an array, but where is it? I used `$result->array(0)`, `$result->array(0)(0)`, `$result['array(0)']`, `$result['array(0)(0)']`, I used `foreach ($result as $k => $v)` but there is nothing. just NULL. – Who Cares May 21 '14 at 12:27
  • Start with `$result[0]` – Jay Blanchard May 21 '14 at 12:36
  • @JayBlanchard it returned error in chrome's dev. tool => "500" – Who Cares May 21 '14 at 12:37
  • Are you trying to get the values via JavaScript or PHP? If PHP, you cannot do that from the console. – Jay Blanchard May 21 '14 at 12:38
  • I am trying to get these values in my PHP file. I tried what you said, and PHP file didn't respond back to ajax part. So I think there is an error occured, right? If you want, I can try to insert `result[0]` in to mysql? just to be sure? – Who Cares May 21 '14 at 12:41
  • I know @Carsten - I am trying to teach the OP how to fish, rather than catching, cleaning, frying and then serving them. – Jay Blanchard May 21 '14 at 12:42
  • @JayBlanchard Okay, sorry. My lips are sealed. ;) – Carsten May 21 '14 at 12:43
  • @Carsten I did yours and it returned `Array`. Then I did `$result->jsondata[0][0]`, it returned with the correct number. Thank you so much :) – Who Cares May 21 '14 at 12:43
  • No worries @Carsten - we can teach the OP to fish together :) And it appears that they caught something! – Jay Blanchard May 21 '14 at 12:44
  • and what is this "OP" thing in this site? is it the question owner? :) – Who Cares May 21 '14 at 12:45
  • "OP" == Original Poster and/or Original Post – Jay Blanchard May 21 '14 at 12:46

1 Answers1

0

You can use json_encode function with second parameter to convert JSON string into array:

$input = '{"jsondata":[[1,7,16,29,41,45],[2,12,21,31,36,45]]}';

$data = json_decode($input, TRUE);

var_dump($data['jsondata']);

In this case you should get associated array that simply for using

Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39