0

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"

I'm sure it's just some stupid mistake I make here. I have googled this quite a lot but I have not been able to find any solution that helps me.

Shikiryu
  • 10,180
  • 8
  • 49
  • 75
DannyCruzeira
  • 564
  • 1
  • 6
  • 19

1 Answers1

2

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.

deceze
  • 510,633
  • 85
  • 743
  • 889