Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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
                )

        )
share|improve this question
what returned $data->post_meta_value ? – Dawid Sajdak Aug 11 '11 at 8:29
php.net/manual/en/function.array-push.php PHP has a good documentation, use it when in trouble. – Gabi Purcaru Aug 11 '11 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? – Lucas Moeskops Aug 11 '11 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? – cnotethegr8 Aug 11 '11 at 8:38

4 Answers

up vote 0 down vote accepted

Do you mean this?

$post->post_meta[strtolower($data->post_meta_key)] = $data->post_meta_value;
share|improve this answer
This will work. but than i get en error that a second param is needed... – cnotethegr8 Aug 11 '11 at 8:32
@cnot Can you clarify that with an exact error message, please? – deceze Aug 11 '11 at 8:33
array_push() expects at least 2 parameters, 1 given – cnotethegr8 Aug 11 '11 at 8:34
@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. – deceze Aug 11 '11 at 8:41
...I feel like a dumb@ss... I dont know why i didnt do that. Thanks – cnotethegr8 Aug 11 '11 at 8:44

i think, you need this:

 $post->post_meta[strtolower($data->post_meta_key)] = $data->post_meta_value;
share|improve this answer

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:

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.