0

Okay so I need to push the array that is fetched from a few different tables into a new array. I thought this would do it but it doesn't. A warning saying array_push() expects parameter 1 to be an array. There is probably something really simple that I have done wrong but Im new to all this PHP stuff so have no idea. I thought the parameter 1 is an array as an array is fetched from database.

Here's the code:

$newsfeed = array("apple");

$news = mysql_query("
SELECT * FROM news
UNION ALL
SELECT * FROM feature ORDER BY timestamp DESC LIMIT 1
")or die(mysql_error());

while($row = mysql_fetch_array($news))
{

    $artist = mysql_query("
    SELECT * FROM members WHERE artist='Y'
    ORDER BY timestamp DESC LIMIT 2
    ")or die(mysql_error());

    while($row1 = mysql_fetch_array($artist))
    {
        array_push($newsfeed, $row['title'], $row1['artistname']);
    }
}
echo($newsfeed);

3 Answers 3

2

Look at the documentation for array_push. The first parameter must be the name of the array you're pushing into.

array_push($myArray, $row['title'], $row1['artistname']);

$myArray will now have $row['title'] and $row1['artistname'] in it.

You're also missing braces on the while loop. You can only omit braces when there is only one line following it. It's still good practice to have braces anyway.

If you want to see the value of an array, you should use print_r instead of echo.

If you want to print out the elements of the array, you'll need to loop through them. Then you can use echo, when you're only printing one element of the array. You can't use it for an entire array or it'll just print array. See the foreach documentation for examples on how to do this:

$arr = array("one", "two", "three");
foreach ($arr as $value) {
    echo $value;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Update your question with what you tried and we can take a look at it again. Did you declare the $myArray array?
Okay, the array is working but it is echoing 'array' and not the contents of the array. I only used print_r for testing purposes
That's what echo does. Use a loop and print only one element if you want to use echo.
Thanks for all the help. You don't have to answer as you would be going very far out of your way but how can you echo data from this particular array that has been pushed? I have no idea whatsoever, i tried using a 'foreach' but either that is the wrong thing to use or i am using it wrong.#
No problem, I've updated my answer with an example and link for more info on looping.
2

$row['title'] is not an array.. it's an element of the array $row.

If you want to store all 'title' and 'artistname' to an array, you can do it

$artists = array();
array_push($artists, $row['title'], $row1['artistname']);
print_r($artists);

If you want to group 'title' and 'artistname'

$artists = array();
$artist_data = array('title' => $row['title'], 'artistname' => $row['artistname'])
array_push($artists, $artist_data);
print_r($artists);

Comments

0

Could you do something like.

$artists=array();
$artists[ $row['title'] ] = $row1['artistname'];

Or am I misunderstanding the question?

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.