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 :)

asked Jul 22, 2017 at 14:14

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));
answered Jul 22, 2017 at 14:32
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'};
...
answered Jul 22, 2017 at 14:31

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.