DannyCruzeira
DannyCruzeira

Reputation: 574

Echoing array data only prints "Array"

I have encountered a problem when trying to add values to an array through a foreach loop. Basically I have this form where there's a bunch of topics that the user can click 'like' or 'dislike' on with two radio-buttons per topic. I then want to collect the "likes" and the "dislikes" in two separate arrays, and insert them into a database, but something I don't do correct. Here is a sample from the HTML code:

Action movies  Like<input type="radio" name="1" value="1" />  Dislike<input type="radio" id="2" name="1" value="2" />

And the PHP code:

if (isset($_POST['submit'])) {
    $likes = array();
    $dislikes = array();

    foreach($_POST as $key => $value) {

        /* $key is the name of the object an user can click "like" or "dislike" on, $value   
        is either 'like', which is equal to '2' or 'dislike', equal to '1' */
        if ($value > 1) { array_push($likes, $key); } else { array_push($dislikes, $key); 
    }
} 
echo 'The object(s) the user likes: ' . $likes . ' , 
       and the object(s) the user dislikes: ' . $dislikes;

I however receive this:

"The object(s) the user likes: Array , and the object(s) the user dislikes: Array"

Upvotes: 0

Views: 1651

Answers (1)

deceze
deceze

Reputation: 522441

An array when cast to a string will simply be output as the string "Array". If you want to output each element in the array, loop through them or use something like join:

echo join(', ', $likes);

array_push is working just fine.

Upvotes: 2

Related Questions