How to assign series of numbers to a single array [duplicate] - php

This question already has answers here:
Separate space-delimited words in a string [duplicate]
(4 answers)
Closed 7 years ago.
I have a from input that contains series of numbers separate by white space that looks like this:
$input = "1243 984 4692 9465 ";
Is there any way to convert this to an array that keeps those numbers like this looks like this:
$array_numbers[0] = 1243
$array_numbers[1] = 984
$array_numbers[2] = 4692
$array_numbers[3] = 9465
Any help on this will be much appreciated,
Thank you

Try
$array_numbers = explode(' ', trim($input));

Trim and explode will do that for you.
http://php.net/manual/en/function.explode.php
http://php.net/manual/en/function.trim.php
Trim removes the whitespace at the end, which if it stays will cause an empty entry in the array.
$input = "1243 984 4692 9465 ";
$array_numbers = explode(' ', trim($input));

The best way to do this is to use the explode function in php that separate the content of your string into an array of string, then convert each elements of array items into integer using intVal() function in php
See sample code below
<?php
$hello = "12 34 45 56 78";
$result = array();
$result = explode(" ", $hello);
for($j=0; $j<count($result); $j++){
echo intval( $result[$j] )."</br>";
}
?>

Related

how to create simple array(without key) by putting available values in php [duplicate]

This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 1 year ago.
I have values like
"34,37"
and I want to create array using this values, array like
array('34','37')
How to create such array if I have values.
hope this will help you :
you can use explode function if you have values as string;
$string = '34,37';
$data = explode(',',$string):
print_r($data); /*output array*/
for more : http://php.net/manual/en/function.explode.php
If you have a string like this:
$str = "1,2,3,4,5,6";
And you want to convert it into array, just use explode()
$myArray = explode(',', $str);
var_dump($myArray);
Have a look here for more information
As per my comment, you should use preg_split function. for more details about preg_split function please read PHP manual and also you can use explode function Explode function PHP manual
<?php
$string = '34,37';
$keywords = preg_split("/[\s,]+/", $string);
//OR $keywords = preg_split("/,/", $string); separated by comma only
print_r($keywords);
you can check your desired Output here
Try this,
$val = "34,37"
$val = explode(',', $val);
print_r($val);
output of above array:
Array
(
[0] => 34,
[1] => 37
)

How To Split String Before After Comma? [duplicate]

This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 7 years ago.
i have string like this:
<?php $string = "52.74837280745686,-51.61665272782557"; ?>
i want access to first string before comma and second string after comma like this:
<?php string1 = "52.74837280745686"; $string2 = "-51.61665272782557" ; ?>
thank you !
This is quite straigtforward
<?php
list($string1, $string2) = explode(",", $string);
or more excant
<?php
$splitTokens = explode(",", $string); // this is what "split" is in other languages, splits an spring by a SEP and retrieving an array
$string1 = $splitTokens[0];
$string2 = $splitTokens[1];

How to split a string into two variables using PHP? [duplicate]

This question already has answers here:
how to split a comma seperated string into a two variables using php [closed]
(10 answers)
Closed 8 years ago.
Using PHP how can I split the
Szombathely, Hungary
into two variables
Szombathely
Hungary
Thank you!
array explode ( string $delimiter , string $string [, int $limit ] )
Source: http://php.net/manual/en/function.explode.php
$split = explode(',','Szombathely, Hungary');
That will, however, leave you with space before _Hungary
So if all are ,_ you could use
$split = explode(', ','Szombathely, Hungary');
or use trim on each of array elements.
There is a native command called explode, in which you pass as parameters the delimiter and also the string. More info here
In your case it would be:
$result = explode ( ',', 'Szombathely, Hungary')
as a result you get:
$result[0] = 'Szombathely'
$result[1] = 'Hungary'
Hope it helps!
$string = "Szombathely, Hungary";
$string = explode(", ", $string);
list($Szombathely, $Hungary) = $string;
var_dump($Szombathely); //Szombathely
var_dump($Hungary);//Hungary`

How can i make all array values in variable [duplicate]

This question already has answers here:
How to represent a PHP array as a comma-delimited string? [duplicate]
(2 answers)
Closed 8 years ago.
i want to make all array values in one variable
Example:
i have a title
$title = "my name is medo";
$words = explode(' ', $title);
The result is a
$words['0'] = my
$words['1'] = name
$words['2'] = is
$words['3'] = medo
i want to make it like that
$allwords = "my,name,is,medo";
thanks all
You can use implode:
implode ( "," , $words );
You can also do this with string functions, which is much easier for your case.
$originalString = "my name is medo";
$finalString = str_replace(" ", ",", $originalString);

how can I split a string with different delimiters into an array? [duplicate]

This question already has answers here:
PHP - split String in Key/Value pairs
(5 answers)
Closed 8 years ago.
How can I split a string with different delimiters into an array?
i.e. convert this: 'web:427;French:435' to this:
'web' => 427,
'french' => 435
This will work as long as your string does not contain & or =.
$str = 'web:427;French:435';
$str = str_replace([';', ':'], ['&', '='], $str);
parse_str($str, $array);
print_r($array);
As mario pointed out, if you don't mind using regex you can amend this answer to fit your needs. If you wish to do it without regex try this: (will work as long as your string doesn't have : and ; inside the variable names or values)
$str = 'web:427;French:435';
$array = explode(';',$str); // first explode by semicolon to saparate the variables
$result = array();
foreach($array as $key=>$value){
$temp = explode(':',$value); // explode each variable by colon to get name and value
$array[$temp[0]]= $temp[1];
}
print_r($result);

Categories