1

Here is my code

foreach ($query1 as $post)
{
    foreach ($query2 as $data)
    {
        if ($post->post_id == $data->post_id)
        {
            // add all actions from a post to its array
            if (!isset($post->post_meta))
            {
                $post->post_meta = array( strtolower($data->post_meta_key) => $data->post_meta_value );
            }
            else
            {
                array_push( $post->post_meta[strtolower($data->post_meta_key)] = $data->post_meta_value );
            }
        }
    }
}

Im not sure how to fix the code. Im not getting the value, only the key, and a few errors.

array_push() expects at least 2 parameters, 1 given

It should print out something like this

 [0] => stdClass Object
        (
            [post_id] => 218
            [post_meta] => Array
                (
                    [flagged] => 0
                    [deleted] => 1
                )

        )
4
  • what returned $data->post_meta_value ? Commented Aug 11, 2011 at 8:29
  • php.net/manual/en/function.array-push.php PHP has a good documentation, use it when in trouble. Commented Aug 11, 2011 at 8:29
  • Unclear what parts of the code do. You test if $post->post_meta is set, but you are certain that $post->post_meta[strtolower($data->post_meta_key)] is set? Commented Aug 11, 2011 at 8:30
  • @lucasmus - Yes, im positive post_meta_key is set... I update the code a question a little. I changed the code. Im new with PHP, but does it have what to do with the object and than array? Commented Aug 11, 2011 at 8:38

4 Answers 4

1

Do you mean this?

$post->post_meta[strtolower($data->post_meta_key)] = $data->post_meta_value;
Sign up to request clarification or add additional context in comments.

4 Comments

@cnot Can you clarify that with an exact error message, please?
array_push() expects at least 2 parameters, 1 given
@cnot Right, and I'm saying you should not use array_push but just assign the value to the key. array_push doesn't do keys.
...I feel like a dumb@ss... I dont know why i didnt do that. Thanks
0

i think, you need this:

 $post->post_meta[strtolower($data->post_meta_key)] = $data->post_meta_value;

Comments

0

From the manual page of array_push (emphasis mine):

array_push() treats array as a stack, and pushes the passed variables onto the end of array.

So you cannot pass a key. If you want to pass a key, use

$yourArray[$theKey] = $theValue;

which will then either overwrite the $theValue for $theKey if it already exists or append it to the end of the array. Also see:

Comments

0

I agree with others. Besides, as php manual note:

Note: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.

http://php.net/manual/en/function.array-push.php

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.