This question already has answers here:
How to trim white spaces of array values in php
(15 answers)
Closed 9 years ago.
If I have this array:
array(" hey ", "bla ", " test");
and I want to trim all of them, How can I do that?
The array after the trim:
array("hey", "bla", "test");
array_map() is what you need:
$result = array_map('trim', $source_array);
array_map() applies a given callback to every value of an array and return the results as a new array.
$array = array_map('trim', $array);
Related
This question already has answers here:
Parse query string into an array
(12 answers)
Closed 4 years ago.
Wi,
I have string:
sortAfter=1&searchString=vsfweew
How can I convert it to array:
Array
(
[sortAfter] => 1
[searchString] => vsfweew
)
?
$str = 'sortAfter=1&searchString=vsfweew';
parse_str($str, $arr);
print_r($arr);
$str = 'sortAfter=1&searchString=vsfweew';
parse_str($str, $arr);
While parse_str() is certainly convenient, there is another option using preg_split() as follows:
<?php
$str = "sortAfter=1&searchString=vsfweew";
list($e1,$sortAfter,$e2,$searchString) = preg_split("/=|&/",$str);
$arr[$e1] = $sortAfter;
$arr[$e2] = $searchString;
print_r($arr);
See live code
Preg_split() splits a string into an array whose values can then be assigned to a list of variables which include in this case variables that correspond to element names while others refer to element values. The list variables are then used to build a new array $arr.
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
)
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`
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);
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);