Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PHP How to foreach explode POST multiple values

I want to foreach explode from POST multiple values

Cut off "1234" with first space (by character not position)

I get error "Array to string conversion"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Form

<input type="checkbox" name="docid[]" value="1234 harry potter">

Submit

$docID = $_POST['docid'];
foreach($docID as $val) {
    echo explode(' ', $val);
}

Output

1234

EDIT
I want to insert output to DB

>Solution :

explode returns an array of strings, so echo won’t work. It also has a third parameter, use that to limit the elements returned:

<?php 
$docID = $_POST['docid'];
foreach($docID as $val) {
    $exploded = explode(' ', $val, 2);
    // print_r($exploded); # Array ( [0] => 1234 [1] => harry potter )
    echo $exploded[0];
}

will output 1234

Edit: if you want to cut off 1234 from your result use echo $exploded[1]; – this will output harry potter

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading