0

I need to merge a php array INTO a php object.

I need my php object to look like this :

{"info1":"value","concurrents":[{"concurrent1":"value"},{"concurent2":"value"}],"info3":"value"};

, I'm actually getting my initial object like this :

$initial = pg_fetch_object($result);

After that, I'm getting the array with this command :

$concurrents = pg_fetch_all($result);

So please, how could i merge $concurrents INSIDE my $initial object, for executing this finally for destination to the front end:

print_r(json_encode($initial));

I've tried a foreach, but it doesn't work.

3
  • That's nice, but what are you fetching? What have you tried? Commented Jan 3, 2017 at 16:53
  • Show $initial and $concurrents. Commented Jan 3, 2017 at 16:56
  • I'm fetching sql data with postgre... I wanna return my JSON object to angularJs, it works very well except that i need to include this array inside my JSON OBJECT, its regular json it works except that i dont know how to generate it with php back end Commented Jan 3, 2017 at 17:01

2 Answers 2

1

Have you tried the following?

$initial->concurrents = $concurrents;

Or even:

$initial->concurrents = pg_fetch_all($result);
Sign up to request clarification or add additional context in comments.

Comments

0

Don't get your initial result as an object, get it as an array:

$initial = pg_fetch_assoc($result);

Then 'merging' is fairly easy (push the initial on to the beginning of your concurrents array):

$concurrents = pg_fetch_all($result);
array_unshift($concurrents, $initial);

Now you can print them:

print_r(json_encode($concurrents));

1 Comment

Thanks a lot But Will it print an array ? Because the front end angularjs need an Object, not an array., in this particular case, thank a lot for your answer.

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.