0

I have a question here on how to parse a $POST_['value'] (e.g 3785,3789,3790,3787 ) from a form to an array($POST_['value']) and do foreach. Please see the sample code below:

function someFunction(){
    $html = '';
    $int = $_POST['Ids']; //POST the value as 3785,3789,3790,3787
    $IDs = array($int);

    foreach ($IDs as $ID) {
        $intVal = '<int>' . $ID .'</int>';
        $html .= $intVal;
    }

    return $html;
}

however the result displays it as the ****whole string** rather than array**. And if I put the array(3785,3789,3790,3787) like this, it will parse as array in foreach. How to convert the $POST_['IDs'] to number or some sort in order to be recognised as array?

Thanks

Mike

Niranjan N Raju
  • 12,047
  • 4
  • 22
  • 41
Mikee Tsai
  • 11
  • 1
  • Possible duplicate of [how to convert array values from string to int?](http://stackoverflow.com/questions/9593765/how-to-convert-array-values-from-string-to-int) – Rene Korss Oct 19 '15 at 07:39

2 Answers2

1

This will work

function someFunction(){
    $html = '';
    $int = $_POST['Ids']; //POST the value as 3785,3789,3790,3787
    $IDs = explode(',', $_POST['Ids']);

    foreach ($IDs as $ID) {
        $intVal = '<int>' . $ID .'</int>';
        $html .= $intVal;
    }

    return $html;
}
Sumit Pandey
  • 448
  • 2
  • 9
0

You need to do a quick fix to the following line:

<?php

$IDs = explode(",", $int);
smottt
  • 3,272
  • 11
  • 37
  • 44
Zaman
  • 551
  • 4
  • 8