"TypeError: implode(): Argument #2 ($array) must be of type ?array, string given" in PHP 8

13,881

$_POST['vpn_network'] ?? '' means that the value can either be the array that you submitted or a string. It would make more sense to have $_POST['vpn_network'] ?? [].

You really should not trust the values submitted in the form. Check each of your assumptions. If you expect an array than check if it is an array.

$vpn_network = isset($_POST['vpn_network']) && is_array($_POST['vpn_network']) ? $_POST['vpn_network'] : [];
$vpn1 =implode(',', $vpn_network);

You could also use the filter extension. It offers a number of filters that can validate the data coming from forms.

Share:
13,881

Related videos on Youtube

Muhammad Yahya Imran
Author by

Muhammad Yahya Imran

Updated on June 04, 2022

Comments

  • Muhammad Yahya Imran
    Muhammad Yahya Imran about 2 years
    <div class="form-group col-md-8" id="my" style="display: none;">
        <label>Choose Vpn Server</label>
        <div class="row">
            <?php
                $sqlUrl4 = "SELECT * FROM vpn_networks";
                $result4 = myQuery($sqlUrl4);
    
                while ($row4 = myFetchArray($result4)) {
            ?>
            <div class="col-sm-4 text-center">
                <label>
                    <input type="checkbox" name="vpn_network[]" value="<?php echo $row4['id'];?>" id="iptv" />
                    <?php echo $row4['title'];?>
                </label>
            </div>
            <?php
                        }
                    ?>
        </div>
    </div>
    
    $vpn1 =implode(',', $_POST['vpn_network']?? '');
    

    Error:

    Fatal error: Uncaught TypeError: implode(): Argument #2 ($array) must
    be of type ?array, string given in
    C:\xampp\htdocs\ideology\partnerprocess.php:22 Stack trace: #0
    C:\xampp\htdocs\ideology\partnerprocess.php(22): implode(',', '') #1
    {main} thrown in C:\xampp\htdocs\ideology\partnerprocess.php on line
    22
    
    • Fikfattt
      Fikfattt over 3 years
      your $_POST['vpn_network'] its array or string ?
    • Definitely not Rafal
      Definitely not Rafal over 3 years
      '' is definitely not an array, thats why you get the error message " ... must be of type array, string given ...", Also make sure $_POST['vpn_network'] is an array. As a quick solution: change '' to [].
    • Muhammad Yahya Imran
      Muhammad Yahya Imran over 3 years
      yes its an array..
    • emandt
      emandt over 3 years
      Do a "print_r($_POST['vpn_network'])" and post the results here
    • Muhammad Yahya Imran
      Muhammad Yahya Imran over 3 years
      print_r not working
  • Nico Haase
    Nico Haase over 2 years
    Please share more details about how to resolve the problem. The error message already tells us that the second parameter has to be an array, and linking to the documentation as the sole part of solving the problem is not a good way to answer on StackOverflow
  • Simas Joneliunas
    Simas Joneliunas over 2 years
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review