0

I want to make an array of json objects having the structure as follows:

{
    "client-mac": "0C:D2:B5:68:73:24",
    "client-dbm": "-82",
    "clientManuf": "unknown"
}

I am using following php code to get this result:

$clientMac = $clients->{'client-mac'};
$clientStrength = $clients->{'snr-info'}->{'last_signal_dbm'};
$clientManuf = $clients->{'client-manuf'};
$jsonObject = array('client-mac' => $clientMac,
                    'client-dbm' => $clientStrength,
                    'clientManuf' => $clientManuf);
$jsonString = json_encode($jsonObject);

The problem is I am getting following json string:

{"client-mac":{
                 "0":"0C:D2:B5:68:73:24"
              },
 "client-dbm":{
                 "0":"-82"
              },
 "clientManuf":{"0":"Unknown"}
}

Why I am getting those extra keys as "0"? And how can I get my desired output? Thank you in advance :)

2 Answers 2

2

Apparently your source data has one more nested level with one key/value pair.

You could use reset on them to just pick the first value from that:

array('client-mac' => reset($clientMac),
      'client-dbm' => reset($clientStrength),
      'clientManuf' => reset($clientManuf));
Sign up to request clarification or add additional context in comments.

Comments

0

That's because $clientMac, $clientStrength and $clientManuf are objects, not literal strings. You have to change the first three lines in the following way,

$clientMac = $clients->{'client-mac'}->{'0'};
$clientStrength = $clients->{'snr-info'}->{'last_signal_dbm'}->{'0'};
$clientManuf = $clients->{'client-manuf'}->{'0'};
...

Comments

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.