-7

I have a string with numbers like this:

$string = "694,852,94,94,743,202,127,127";

What is the best and quickest way to get an integer array from this string in PHP?

Array ( [0] => 694,
        [1] => 852,
        [2] => 94,
        [3] => 94,
        [4] => 743,
        [5] => 202,
        [6] => 127,
        [7] => 127 )
Tschallacka
  • 27,901
  • 14
  • 88
  • 133
mihabek
  • 13
  • 2
  • http://in3.php.net/manual/en/function.explode.php – Abhishek Bhatia Jul 03 '13 at 09:42
  • 4
    Absoulutely no research done. Atleast look it up on google. – user568109 Jul 03 '13 at 09:45
  • 1
    @HamZa What if he/she didn't know about explode? When searching for the "split" equivalent of php explode doesn't automatically pop up, and a type conversion from string to int isn't exactly obvious if you're used to other languages. So your comment is a tad bit rude. – Tschallacka Jul 03 '13 at 09:46
  • 2
    @MichaelDibbets If you type in google `split string into array php`, the first result will lead you to explode. That's way faster than posting a question on SO. – HamZa Jul 03 '13 at 09:49
  • @HamZa I think the question asker had a more specific problem, that he wanted to convert the numbers into integers and couldn't find how to do the type conversion. I've edited the question to reflect that part of the question. – Tschallacka Jul 03 '13 at 09:51
  • @MichaelDibbets If you noticed there were 9 answers to this question within few minutes. So digging this up is not too hard if you ask me. If you think that way then you should also google it. – user568109 Jul 03 '13 at 09:52
  • 3
    9 answers exactly the same to a question asked millions of times... – CSᵠ Jul 03 '13 at 09:53
  • @MichaelDibbets I know how you feel, I was like you but I got enough of those helpvampires who can't or don't want to even bother searching *a little* bit. If he's that concerned about integers, he could have said that in his post. Basically someone who did no research (at least did not show it), did not bother to properly ask what he wants, did not bother to format properly his question. That's all more than enough for me to vote to close this question and leave a hint behind. – HamZa Jul 03 '13 at 09:54
  • @HamZa I also know how you feel, but I can't help but wanting to help the n00b's who make their first ventures in the world of programming. Try to remember your own first steps in the world of programming and how foreign everything was and any help was helpfull to guide you along on your path to become more experienced. – Tschallacka Jul 03 '13 at 09:57
  • 1
    @MichaelDibbets It was a while ago and honestly I didn't even know SO and didn't even register at any forum. I was able to find it all with a simple search. Basically those kind of questions pollute the website and make it harder to find the good questions/answers – HamZa Jul 03 '13 at 09:59

9 Answers9

4
$string = "694,852,94,94,743,202,127,127";

$array = explode(',',$string);

PHP Official Documentation

abhshkdz
  • 6,335
  • 1
  • 21
  • 31
2
$string = "694,852,94,94,743,202,127,127";
$array = explode(",", $string);
fehnomenal
  • 509
  • 3
  • 10
2

Try this

$string = "694,852,94,94,743,202,127,127";

$num  = explode(",", $string);

$num will contain the array of the numbers split from the string.

Refer the PHP Documentation .And Please google it once before asking here.

Ashrith Sheshan
  • 654
  • 4
  • 17
2
$string = "694,852,94,94,743,202,127,127";
$arr = explode(",",$string);

Php will automatically do a type conversion if you try to use the numbers in a calculation. How do I convert a string to a number in PHP?

Community
  • 1
  • 1
Tschallacka
  • 27,901
  • 14
  • 88
  • 133
1

You should use explode in order to array you expect:

$arr = explode(',',$string);
Javid
  • 2,755
  • 2
  • 33
  • 60
1

you can use explode to convert the string to an array using a delimeter.

for example

 $array = explode(",", $string);
DevZer0
  • 13,433
  • 7
  • 27
  • 51
1
<?php
    $string = "694,852,94,94,743,202,127,127";
    $arr = explode(",", $string);
Amani
  • 519
  • 2
  • 5
1

Use explode():

$string = "694,852,94,94,743,202,127,127";

$array = explode(",",$string);
Timmetje
  • 7,641
  • 18
  • 36
0

Try with explode() like

$string = "694,852,94,94,743,202,127,127";
$my_array = explode("," , $string); 
print_r($my_array);

Refer this explode

GautamD31
  • 28,552
  • 10
  • 64
  • 85