0

how to put integer values, taken by another array, instead of 1,2,3 like this?

$arr_id = array(1,2,3); //this arr_id go to line 2 instead of 1,2,3
'id' => array('$in' => array(1,2,3))

well, putting $arr_id instead of "1,2,3"... something like this

'id' => array('$in' => array($arr_id))

the problem is that 1,2,3 are number, but in my $arr_id i've strings. If i try to convert string to int i've a problem with "," too

here

array($arr_id))

i need to have integers (not string) taken by $arr_id, separated by ","...

7
  • It looks like they're already integers. Are you saying that $arr_id is actually equal to the string "1,2,3"? Can you clarify with code examples exactly what you're starting with and what you want to end up with? Commented Mar 15, 2017 at 20:06
  • Or is it already an array of ints and you're trying to turn it into "1,2,3"? Commented Mar 15, 2017 at 20:08
  • @Don'tPanic i'm tryin' to put it inside 1,2,3 in the line 'id' => array('$in' => array(1,2,3)). It starts with a string (verified by echo gettype($arr_id);) . Please read below in the first answer and relative comments... there's a progression :) Commented Mar 15, 2017 at 20:27
  • if i wrote 'id' => array('$in' => array($array_replaced[1])) or 'id' => array('$in' => array($array_replaced[2])) the code works fine. But i can't write [integers. Very strange problem.... Commented Mar 15, 2017 at 20:31
  • I'd really like to help. But I don't really understand from your description what you're starting with or what you want to end up with. Can you edit your question to add a code example that shows that? Commented Mar 15, 2017 at 20:36

1 Answer 1

1

You can convert a string array to an int array in PHP using:

$arr_id_str = array("1","2","3");
var_dump(array_map('intval',$arr_id_str));
Sign up to request clarification or add additional context in comments.

1 Comment

ok... your code is correct but... there's a problem: if i put $array_replaced $array_replaced = array_map('intval',$arr_id_str); inside 'id' => array('$in' => array($array_replaced)) the code doesn't work Warning: array_values() expects parameter 1 to be array, null given i can't write $array_replaced[1] or 2 or 3... 'cause is the same to write 1,2,3. This fields is dynamic.....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.