0

I have an array object that looks like this and stored in a variable $named_array

Array
(
    [results] => Array
        (
            [0] => Array
                (
                    [date] => 2013-15-6
                    [position] => 5
                    [person] => John
                )

            [1] => Array
                (
                    [date] => 2013-15-6
                    [position] => 3
                    [person] => Alex
                )

        )

)

Another array called $post which is a WordPress post object array called with $posts = get_posts(array(.... I then have a foreach each loop with $post being the variable.

Within my foreach loop i've tried the following to combine the arrays but it's not working.

$combinedData = array_merge($post, $named_array);
print_r($combinedData);

I can see my post object arrays with print_r($post); within the foreach loop as well as the named_array. What's the correct function to add the named_array to the post array?

Thanks

5
  • Depends on what you want your final array to look like. Are you trying to append each post to the "results" array? Commented Aug 20, 2013 at 4:59
  • @koala_dev I'm trying to append the results array to the post array so the post array has one nested array being "results" Commented Aug 20, 2013 at 5:02
  • You mean each post is going to have the same result array? Commented Aug 20, 2013 at 5:03
  • The results array is stored in a variable, it's dynamic not the same exact values but same keys Commented Aug 20, 2013 at 5:05
  • I see, I've added an answer that I believe is what you need Commented Aug 20, 2013 at 5:07

4 Answers 4

1

$posts as you mentioned, is an array of objects

$post is an object, not an array, so you can't use array_merge()

If you want to add the results as a new property to $post, the correct way to do it is:

$post->results = $named_array['results'];
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the code. Can you let me know when running $post->results does this create an array in the post object called 'results' where the key's and values from the named_array['results'] are then placed. Am I basically saying what key's and values to get from the named array and which array to place them into in the other?
It's just like any other assignation code, say you have a variable $x=1 and then you set another's variable value to $x like $y=$x now $y holds the same value as $x, in this case we add a new property to the $post object and set the value to the results array
1

if $post is an object you can add an array called results with this

$post->results = Array
    (
        [0] => Array
            (
                [date] => 2013-15-6
                [position] => 5
                [person] => John
            )

        [1] => Array
            (
                [date] => 2013-15-6
                [position] => 3
                [person] => Alex
            )

    )

or $post->results = $named_array['results'];

Comments

0

http://php.net/manual/en/function.array-merge-recursive.php Try this instead of array_merge

Comments

0

It makes no sense to use array_merge inside the foreach loop in this case. You use array_merge to append the second array overwriting identical keys. You use foreach for array operations that apply to each element.

You did'nt show how your $post array is looking like. So I can only guess.

$combinedData = array_merge ( $post, $named_array );

already appends the named array to $post. Maybe you wanted this?

array_shift ( $named_array, $post ); 

It assumes that the post array looks like one element of the named array.

1 Comment

If this is not what you expect please show an example of your $post array.

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.