0

I have an array which is expressed in this way:

$id = $values['ids'];

Result:

array(6) { [0]=> string(1) "4" [1]=> string(1) "6" [2]=> string(1) "7" [3]=> string(1) "8" [4]=> string(1) "9" [5]=> string(2) "10" }

Originally, I was imploding this array so that I could pass it off as a value to use in $_POST:

$id = implode(",", $values['ids']);

Result:

string(12) "4,6,7,8,9,10"

In my $_POST page, I need to revert back to the original array, but when I use explode, I now get this result:

$id2 = explode(" ", $id);
//array(1) { [0]=> string(12) "4,6,7,8,9,10" }

I think the issue is how I am imploding this in the first place - it's making the individual values as one string, but not sure what to do in order to get the result I need. I need to pass the array on as a value, and then put it back into the original format that it was in as the array. Does anyone know what I should do?

7
  • Please add your current code/attempt into your question. Commented May 16, 2015 at 22:47
  • What delimiter did you use when you tried explode()? Commented May 16, 2015 at 22:52
  • Actually - I think I answered my own question. If I find that I am still having issues I will re-ask this with the code so as to not cause confusion. Commented May 16, 2015 at 22:54
  • 1
    @Wes Just add your code to your question and add more details if you think you have to specify your problem more. Commented May 16, 2015 at 22:55
  • I've updated it to reflect the issue. I was thinking these needed to be ints but they don't. Hope it makes more sense now. Commented May 16, 2015 at 23:12

3 Answers 3

2

Use explode and array_map with intval as callback:

$nums = "4,6,7,8,9,10";
$intsArray = array_map('intval', explode(',', $nums));
var_dump($intsArray);

Output:

array(6) {
  [0]=>
  int(4)
  [1]=>
  int(6)
  [2]=>
  int(7)
  [3]=>
  int(8)
  [4]=>
  int(9)
  [5]=>
  int(10)
}
Sign up to request clarification or add additional context in comments.

Comments

0

I think I just answered my own question. It was in the way I was using explode. Should be:

$id2 = explode(',', $id);

And not:

$id2 = explode(" ", $id);

I'll give credit to Pedro who caused me to realize this when he pasted his example.

4 Comments

Thanks for the mention but array $id2 still contains strings and not ints as the OP want, use var_dump($id2); to confirm that.
@PedroLobito OP doesn't need integers: stackoverflow.com/questions/30281507/… And ^^ this is OP...
Hi @Rizier123, almost at 30k ;) The question was updated, the original contained a request for an array with ints. I didn't realize this was the OP.... I probably miss a coffee...
@PedroLobito Well I had the same answer prepared as you have, but I didn't posted it, because the: string(12) "4,6,7,8,9,10" was suspicious for me. (FYI: ^ See the blue box around the user name? That's the indication, that the post is from OP)
0

Easily you can use json_encode and json_decode functions. For example:

$a = array(1,2,3,4,5);

$b = json_encode($a);
var_dump($b);

$c = json_decode($b);
var_dump($c);

that result of run is:

meysampg@freedom:~/www/testFunc$ php json_deEncode.php 
string(11) "[1,2,3,4,5]"
array(5) {
  [0] =>
  int(1)
  [1] =>
  int(2)
  [2] =>
  int(3)
  [3] =>
  int(4)
  [4] =>
  int(5)
}

Comments

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.