How to pass an array of string into single variable in php -


basically, have variable called $userid = $_post['userid']; can take 1 value.

but want take multiple values if possible, $userid = 1,2,4,5 or $userid = 2 or $userid = 1,2,3,4,5,6,7

i don't know how multiple numbers here php script:

<?php     require "init.php"; function foo() {     $numargs = func_num_args();     echo "number of arguments: $numargs \n";     if ($numargs >= 2) {         echo "second argument is: " . func_get_arg(1) . "\n";     }     $arg_list = func_get_args();     ($i = 0; $i < $numargs; $i++) {         echo "argument $i is: " . $arg_list[$i] . "\n";     } }     if(!empty($_post['userid'])){         $userid = $_post['userid'];          $userid = foo($userid);         $stmt = "select userid, forename, surname, email, age                 users                 userid in (?)";            $result = $conn-> prepare($stmt);         $result->bind_param('i', $userid);         $result->execute();          $outcome=$result->get_result();         $response = array();         if(($outcome->num_rows)>0){             while($row = $outcome->fetch_assoc()){                 $response[] = array                 (                     "userid" => $row["userid"],                     "forename" => $row["forename"],                     "surname" => $row["surname"],                     "email" => $row["email"],                     "age" => $row["age"]                 );             }         echo json_encode($response);          }         else{             echo json_encode("none found");         }     }      ?> 

edit: above code keep getting: number of arguments: 1 argument 0 is: 1,2,3 "failed" - if $userid = 1,2,3

so can see userid can take 1 value, want take many in cases.how convert takes many.

that bit stuck on, rest of sql stuff works fine in phpmyadmin when provide hardcoded values

you use call-user-func-array

eg:

  $userid = "1,2,3,4,5,6,7";   call-user-func-array(function_name, explode(',', $userid)); 

if use php 5.6 >, can use ... variadic functions.

manual

as per comment,

you can use func_get_arg.

modified example

<?php function foo() {     $numargs = func_num_args();     echo "number of arguments: $numargs \n";     if ($numargs >= 2) {         echo "second argument is: " . func_get_arg(1) . "\n";     }     $arg_list = func_get_args();     ($i = 0; $i < $numargs; $i++) {         echo "argument $i is: " . $arg_list[$i] . "\n";     } }  foo($userid); ?> 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -