0

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 protected]"},
{"value":"[email protected]"},
{"value":"[email protected]"}]

Desired output:

[{"key":"user_email","relation":"=","value":"[email protected]"},
{"key":"user_email","relation":"=","value":"[email protected]"},
{"key":"user_email","relation":"=","value":"[email protected]"}]

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!

2
  • 3
    Stop using the deprecated and as of PHP7 removed mysql_* functions. Migrate to PDO and start using Prepared, Parameterized Queries. Commented Aug 5, 2016 at 16:57
  • $jsondata[] = array('key'=>$key,'relation'=>$rel,'value' => $val); Commented Aug 5, 2016 at 16:57

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

If you must use the mysql_ functions, you don't need to manually construct an object or array, just get the result set as an array and use that.

while ($staffrow = mysql_fetch_assoc($allStaffInit)){
    $jsonData[] = $staffRow;
}

But I seriously recommend you at least upgrade to using the mysqli extension instead. You won't really have to adjust your code much.

http://php.net/manual/en/book.mysqli.php

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.