pixeloft pixeloft - 4 months ago 10
JSON Question

PHP append each element in an array

I'm having a difficult time appending each element in an array I have in PHP for multiple OneSignal tags. Here is the result of my current JSON encoded array:

[{"value":"email@address.com"},
{"value":"email@address.com"},
{"value":"email@address.com"}]


Desired output:

[{"key":"user_email","relation":"=","value":"email@address.com"},
{"key":"user_email","relation":"=","value":"email@address.com"},
{"key":"user_email","relation":"=","value":"email@address.com"}]


Here is my current PHP code:

$jsonData = array();

$allStaffInit = mysql_query("Select * from users");

while ($staffrow = mysql_fetch_object($allStaffInit)){

$jsonData[] = $staffrow;

}

echo json_encode($jsonData);


Any help is greatly appreciated! Thanks!

Answer

Try replacing

$jsonData[] = $staffrow;

with

$object = new stdClass();
$object->key = "user_email";
$object->relation = "=";
$object->value = $staffrow->value;

$jsonData[] = $object;

I am typing this in a browser, so cannot test, but you get the idea (if you don't get the idea, ask in comments :)