1

I currently am trying to figure out how to split the values inside my array. I have looked for some code to put me on the right path, but have had no luck. Ideally what I would like to do is take the following input and split it to two separate arrays. each pair listed below ie: (12,13) is one array value.

12,13
12,14
12,15
12,16
12,17
12,18
12,21
12,22

any assistance you can give is muchly appreciated!

5
  • 1
    Can you show us the expected output? I'm not quite sure what you're trying to do here. Commented Sep 4, 2013 at 0:24
  • Yes give us more details , and also is your values inside a text file or what Commented Sep 4, 2013 at 0:29
  • The first array would contain 8 values, all listed as 12 as per the left hand column, and them the other array would also have eight values with the right hand side populating those. @FaceOfJock yes these values that you see in the example are from a text file. Commented Sep 4, 2013 at 0:31
  • What have you tried? (Hint: explode might help you) Commented Sep 4, 2013 at 0:33
  • I have used explode, however it seems to still put those values into an array as array ( [0] => 12, 13 ) which is not exactly what I am looking for. Commented Sep 4, 2013 at 0:37

1 Answer 1

2

Assuming you're having one array with those values above, eg:

$foo = array("12,13", "12,14", "12,15", ...);
$outA = array();
$outB = array();
foreach($foo as $value)
{
    list($x, $y) = explode(",",$value);
    $outA[] = $x;
    $outB[] = $y;
}
print_r($outA);
print_r($outB);

You'd probably want some error checking in there somewhere though.

If you don't (yet) have the numbers in an array but in a text file just use PHPs file function to get them there.

Sign up to request clarification or add additional context in comments.

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.