Venelin
Venelin

Reputation: 3306

Trouble when merging two arrays after getting checkbox values

I have many checkboxes with same name. They are devided with 2 names subcategory and categories.

So i have this code:

<?php 
if (isset($_POST['subcategory'])) 
{
    $SubCategorys[] = $_POST['subcategory'];
    $categories[] = $_POST['categories'];
    $MergedArrays = array_merge($SubCategorys, $categories);

    echo implode(",",$MergedArrays);


}
?>      

I receive result like this:

Array,Array

I want to receive result like this: "3, 6, 34, 65, 23, 67,".

Where is my mistake and how can i achive that goal ?

Thanks in advance!

Upvotes: 0

Views: 65

Answers (2)

Mandeep singh
Mandeep singh

Reputation: 1

you should use store them in a variable with implode method

$hobby = $_POST['game'];
$b = implode(",", $hobby);

Upvotes: 0

RomanPerekhrest
RomanPerekhrest

Reputation: 92884

$_POST['subcategory'] and $_POST['categories'] are already arrays. You don't have to use external arrays in this case:

<?php 
    if (isset($_POST['subcategory'])) 
    {
        $SubCategorys = $_POST['subcategory'];
        $categories = $_POST['categories'];
        $MergedArrays = array_merge($SubCategorys, $categories);

        echo implode(",",$MergedArrays);

    }
    ?> 

Note: implode function lets you to join elements with a string in ONE-dimensional arrays only http://php.net/manual/en/function.implode.php

Upvotes: 3

Related Questions