0

If I do a json_decode like this:

$json = json_decode($jsondata);
echo var_dump($json);

I get something like this:

object(stdClass)#1 (1) 
{ ["QRY_JISGesch"]=> array(3) 
    { [0]=> object(stdClass)#2 (8) 
        { ["JISGeschID"]=> int(7) ["StandorteID"]=> int(0) ["FSKID"]=> int(23) 
        } 
    [1]=> object(stdClass)#3 (8) 
        { ["JISGeschID"]=> int(8) ["StandorteID"]=> int(0) ["FSKID"]=> int(22) 
        } 
    [2]=> object(stdClass)#4 (8) 
        { ["JISGeschID"]=> int(9) ["StandorteID"]=> int(0) ["FSKID"]=> int(1)
        } 
    } 
} 

How do I find out "QRY_JISGesch" in code?

klammerauf
  • 17
  • 4

4 Answers4

1

You could use reset() to get the first member of an object (or an array).

$json = json_decode($jsondata);
$first = reset($json);

If you only want the get the first "key", you can use key();

$json = json_decode($jsondata);
$key = key($json); // QRY_JISGesch
Syscall
  • 19,327
  • 10
  • 37
  • 52
0

Try this. It will help you :-

$json = json_decode($jsondata, true);
// true means objects will be converted into associative arrays

and you can access QRY_JISGesch like this way :- $json['QRY_JISGesch']

Yash Parekh
  • 1,513
  • 2
  • 20
  • 31
  • Don't think that this solves the problem - if `QRY_JISGesch` is unknown, it is unknown – Nico Haase Apr 25 '18 at 11:28
  • I'm aware of that second param in json_decode, but this doesn't help me here. And I also know that I can access the var with $json['QRY_JISGesch']. But I need to do a function that can handle different values, other than QRY_JISGesch. – klammerauf Apr 25 '18 at 11:29
  • @klammerauf You want to check that `QRY_JISGesch` exists or not. correct?? – Yash Parekh Apr 25 '18 at 11:30
0

Is there only one item given in the array, and you are not interested in the name itself, but the values? Then array_values could help.

Otherwise, you could use array_keys to read all keys and iterate over them? Or use a foreach loop which does not care about the array key neither.

Nico Haase
  • 11,420
  • 35
  • 43
  • 69
  • array_values($json) gives a warning "array_values() expects parameter 1 to be array, object given". array_values($json[0]) gives a fatal error "Cannot use object of type stdClass as array" – klammerauf Apr 25 '18 at 11:38
  • array_keys($json) gives the same warning as array_values. – klammerauf Apr 25 '18 at 11:41
  • Ah, yeah - this could have worked with `$json = json_decode($jsondata, true);` – Nico Haase Apr 25 '18 at 11:44
0

get_object_vars (object $object) will give you a list of all accessible properties of the returned object.

Cuagau
  • 548
  • 3
  • 7