What is the best way to split the below $string to get the array $cars in PHP?
$string = '{xa}{y}{z12}{123}{2}{aabb}';
$cars = array("{xa}","{y}","{z12}", "{123}", "{2}", "{aabb}");
I need each array element with brackets eg : {xa}
$string = str_replace("}{","},{",$string);
$x = explode(',',$string);
Related
I have string:
$my_string = 'kqxs.mt={run:1,tinh:"39,31",ntime:153801582,delay:2000,kq:{39:{lv:"K42",8:"69",7:"985",6:["7772","4105","0258"],5:"8965",4:["03787","86098","45665"]}}};';
Please help me convert this string to array in PHP.
I want result:
array(){
[8]{
[0]=>69
},
[7]{
[0]=>985
},
[6]{
[0]=>7772
[1]=>4105
[2]=>0258
}
.............
}
Thank you!
You can just treat the string like it's an array:
<?php
$my_string = 'kqxs.mt={run:1,tinh:"39,31",ntime:153801582,delay:2000,kq:{39:{lv:"K42",8:"69",7:"985",6:["7772","4105","0258"],5:"8965",4:
"03787","86098","45665"]}}};';
//
for ($x = 0; $x < strlen($my_string); $x++){
echo "<br>".$my_string[$x];
}
?>
You need a separator to make a string to became array.
For example: $string = "Hello, beautiful, world";
to make the $string array you need to use explode.
For example: $string_array = explode(",", $string);
The first parameter in explode will be your separator and your second parameter is the string you want to become array.
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
)
Hi,
I have one string like this:
4,21
and the other like this:
,4,5,6,14,21,22,
I need to find out if any of the numbers contained in the first string are present in the second string but since each number is separated by a comma Im a little confused. What function should I use?
Thank you.
You could convert each string into arrays.
Try this:
$str1 = "4,21";
$array1 = explode(",", $str1);
$str2 = "4,5,6,14,21,22";
$array2 = explode(",", $str2);
$common = array_intersect($array1, $array2);
echo "Common numbers:<br/>";
echo implode(",", $common);
Please help me to convert a string into single dimension array...
example
$str = abc,xyz,pqr;
convert to
$arr = array('abc','xyz','pqr');
Try with explode like
$str = 'abc,xyz,pqr';
$str_arr = explode(',',$str);
print_r($str_arr);
Try this EXPLODE
use php function
explode(',',$str);
How to convert a string format into an array format?
I have a string, $string = 'abcde'
I want to convert it to a 1 element array
$string[0] = 'abcde'
Is there a built in function for this task? Or the shortest way is to
$string = 'abcde';
$array[0] = $string;
$string = $array;
TIA
All kinds of ways in php..
$array = array('abcde');
$array[] = 'abcde';
Etc... not too sure what you're going for.
Edit: Oh, I think you might want to convert the first variable? Like this?
//Define the string
$myString = 'abcde';
//Convert the same variable to an array
$myString = array($myString);
Edit 2: Ahh, your comment above I think clears it up a little. You're getting back either an array or a string and don't know which. If you do what I just said above, you might get an array inside an array and you don't want that. So cast instead:
$someReturnValue = "a string";
$someReturnValue = (array)$someReturnValue;
print_r($someReturnValue);
//returns
Array
(
[0] => a string
)
$someReturnValue = array("a string inside an array");
$someReturnValue = (array)$someReturnValue;
print_r($someReturnValue);
//returns
Array
(
[0] => a string inside an array
)
Lets not forget the standard way of declaring a string as an array in PHP, before adding elements..
$string = array();