I have an array that I pull that looks like this:
[157966745,275000353,43192565,305328212]...
How do I go about taking that "string" and converting it to a PHP array which I can then manipulate.
This looks like JSON, so you can use json_decode:
$str = "[157966745,275000353,43192565,305328212]";
$data = json_decode($str);
$s = "[157966745,275000353,43192565,305328212]";
$matches;
preg_match_all("/\d+/", $s, $matches);
print_r($matches);
With exact that code...
$string='[157966745,275000353,43192565,305328212]';
$newString=str_replace(array('[', ']'), '', $string); // remove the brackets
$createArray=explode(',', $newString); // explode the commas to create an array
print_r($createArray);
PHP explode is built just for this.
$result = explode(',', $input)
preg_replace('/([\[|\]])/','',$strarr);
$new_arr=explode(','$strarr);
Related
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
)
My string:
'KCP-PRO;first_name last_name;address;zipcode;country' //for example: 'KCP-PRO;Jon Doe;Box 564;02201;USA'
or
'KCP-SAT-PRO;first_name last_name;address;zipcode;country'
How can i change the first part (KCP-PRO or KCP-SAT-PRO) and change it to (KCP,PRO or KCP,SAT,PRO)? The outcome has to be:
'KCP,PRO;first_name last_name;address;zipcode;country'
or
'KCP,SAT,PRO;first_name last_name;address;zipcode;country'
I haven't tried the code myself but I guess this will do the trick
$string = 'KCP-SAT-PRO;first_name last_name;address;zipcode;country';
$stringExploded = explode(';', $string);
$stringExploded[0] = str_replace('-', ',', $stringExploded[0]);
$output = implode(';', $stringExploded);
//output should be KCP,SAT,PRO;first_name last_name;address;zipcode;country
Hope this helps :)
Or you can use preg_replace_callback function with the following regex
^[^;]*
So your code looks like as
echo preg_replace_callback("/^[^;]*/",function($m){
return str_replace("-",',',$m[0]);
},"KCP-SAT-PRO;first_name last_name;address;zipcode;country");
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);
I have a string in an array like format.
["1","Data","time"] ["2","Data","time"] ["3","Data","time"] ["4","Data","time"]
I am trying to split the code after every ] - The data inside the [] could vary everytime so i cant use str_split();
I want to keep all the brackets in tack so they dont cut off so i cant use explode
Thank You
Easy regex:
$s = '["1","Data","time"] ["2","Data","time"] ["3","Data","time"] ["4","Data","time"]';
preg_match_all('/\[[^\]]+\]/', $s, $m);
print_r($m[0]);
But actually it's almost json, so:
$s = '["1","Data","time"] ["2","Data","time"] ["3","Data","time"] ["4","Data","time"]';
$s = '[' . str_replace('] [', '],[', $s) . ']';
print_r(json_decode($s));
Maybe you have a fragment or modified json, so it may be easier if you have actual json.
$str = '["1","Data","time"] ["2","Data","time"] ["3","Data","time"] ["4","Data","time"]';
$newStr = trim($str,'[');
$newStr1 = trim($newStr,']');
$arr = explode('] [',$newStr1);
print_r($arr);
Easiest solution (if you don't want regular expression) would be using trim 1 char from both sides and then explode by ']['.
Example:
$string = "[123][456][789]";
$string = substr($string,1,strlen($string)-1);
$array = explode('][',$string);
I have a string like this:
key=value, key2=value2
and I would like to parse it into something like this:
array(
"key" => "value",
"key2" => "value2"
)
I could do something like
$parts = explode(",", $string)
$parts = array_map("trim", $parts);
foreach($parts as $currentPart)
{
list($key, $value) = explode("=", $currentPart);
$keyValues[$key] = $value;
}
But this seems ridiciulous. There must be some way to do this smarter with PHP right?
If you don't mind using regex ...
$str = "key=value, key2=value2";
preg_match_all("/([^,= ]+)=([^,= ]+)/", $str, $r);
$result = array_combine($r[1], $r[2]);
var_dump($result);
<?php parse_str(str_replace(", ", "&", "key=value, key2=value2"), $array); ?>
if you change your string to use & instead of , as the delimiter, you can use parse_str()
If you can change the format of the string to conform to a URL query string (using & instead of ,, among other things, you can use parse_str. Be sure to use the two parameter option.
Here's a single command solution using array_reduce formatted in multple lines for readability:
<?php
$str = "key=value, key2=value2";
$result = array_reduce(
explode(',', $str),
function ($carry, $kvp) {
list($key, $value)=explode('=', $kvp);
$carry[trim($key)]=trim($value);
return $carry;
}, []);