Home > other >  implode(): Argument #2 ($array) must be of type ?array, string given, is an error I receive in displ
implode(): Argument #2 ($array) must be of type ?array, string given, is an error I receive in displ

Time:09-08

<?php

$hello = null;

if (isset($_POST['submit'])) {
    if (! empty($_POST['books'])) {
        $checked_array = $_POST['books'];
        foreach ($_POST['books_name'] as $key => $value) {
            if (in_array($_POST['books_name'][$key], $checked_array)) {
                $hello = $_POST['books_name'][$key] ." ". $_POST['books_qty'][$key];
                echo $hello1 = implode(" ", $hello);
            }
        }
    } else {
        $hello;
    }
}

Above is my code that I'm getting troubled with whereas I received an error "implode(): Argument #2 ($array) must be of type ?array, string given". I tried the code below and it displays the value ok

echo $hello = $_POST['books_name'][$key] ." ". $_POST['books_qty'][$key];

I assumed that using the implode method it will be ok, unfortunately not. As you can see I'm trying to implode two different value from a checkbox value and another input value.

CodePudding user response:

    First Declare `$hello` as a blanck Array. After that usr PHP `array_push()` function to push the values

$hello = [];

$hello = array_push(1st args,2nd args);

See the code here i written...

$hello = []; // Declare as a Array
if (isset($_POST['submit'])) {
if (! empty($_POST['books'])) {
    $checked_array = $_POST['books'];
    foreach ($_POST['books_name'] as $key => $value) {
        if (in_array($_POST['books_name'][$key], $checked_array)) {
            $hello[] = array_push($_POST['books_name'][$key] , $_POST['books_qty'][$key]);      // Push the values 
            echo $hello1 = implode(" ", $hello);        // Do your work !!
        }
    }
} else {
    $hello;
}
}
  • Related