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.
Related
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 7 months ago.
I have a drag and drop JS plugin that saves strings as the following:
["кровать","бегемот","корм","валик","железосталь"]
I have found that I can use str_replace in an array to remove both brackets and " char. The issue that I now have is that I have a invalid argument for passing through the foreach loop as it cannot distinguish each individual word.
$bar = '["кровать","бегемот","корм","валик","железосталь"]';
$new_str = str_replace(str_split('[]"'), '', $bar);
foreach($new_str as $arr){
echo $arr;
}
So the data now outputted looks as follows (if I were to echo before the foreach loop):
кровать,бегемот,корм,валик,железосталь
Is there anyway in which I can use a comma as a delimeter to then pass this through the foreach, each word being it's own variable?
Is there an easier way to do this? Any guidance greatly appreciated!
Technically, you can use explode, but you should recognize that you're getting JSON, so you can simply do this:
$bar = '["кровать","бегемот","корм","валик","железосталь"]';
$new_str = json_decode($bar);
foreach($new_str as $arr){
echo $arr;
}
With no weird parsing of brackets, commas or anything else.
The function you need is explode(). Take a look here:
http://www.w3schools.com/php/func_string_explode.asp
Look at the following code:
$bar = '["кровать","бегемот","корм","валик","железосталь"]';
$new_str = str_replace(str_split('[]"'), '', $bar);
$exploding = explode(",", $new_str);
foreach($exploding as $token){
echo $token;
}
It looks like you've got a JSON string and want to convert it to an array. There are a few ways to do this. You could use explode
like this:
$bar = '["кровать","бегемот","корм","валик","железосталь"]';
$new_str = str_replace(str_split('[]"'), '', $bar);
$new_str_array = explode($new_str);
foreach($new_str_array as $arr){
echo $arr;
}
or you could use json_decode
like this:
$bar = '["кровать","бегемот","корм","валик","железосталь"]';
$new_str_array = json_decode($bar);
foreach($new_str_array as $arr){
echo $arr;
}
<?php
$aa = '["кровать","бегемот","корм","валик","железосталь"]';
$bb = json_decode($aa);
foreach($bb as $b)
echo $b."\n";
?>
and the results is,
кровать
бегемот
корм
валик
железосталь
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);
In a comma delimited string, in php, as such: "1,2,3,4,4,4,5" is it possible to say:
if(!/*4 is in string bla*/){
// add it via the .=
}else{
// do something
}
In arrays you can do in_array(); but this isn't a set of arrays and I don't want to have to convert it to an array ....
Try exploding it into an array before searching:
$str = "1,2,3,4,4,4,5";
$exploded = explode(",", $str);
if(in_array($number, $exploded)){
echo 'In array!';
}
You can also replace numbers and modify the array before "sticking it back together" with implode:
$strAgain = implode(",", $exploded);
You could do this with regex:
$re = '/(^|,)' + preg_quote($your_number) + '(,|$)/';
if(preg_match($re, $your_string)) {
// ...
}
But that's not exactly the clearest of code; someone else (or even yourself, months later) who had to maintain the code would probably not appreciate having something that's hard to follow. Having it actually be an array would be clearer and more maintainable:
$values = explode(',', $your_string);
if(in_array((str)$number, $values)) {
// ...
}
If you need to turn the array into a string again, you can always use implode():
$new_string = implode(',', $values);
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 returning values that give me double digits, tripple digits etc. I want to put a "w" in between those numbers. Any kind of help I get on this is greatly appreciated.
$value = "444";
I want it to give me back this:
$value = "4w4w4w";
You could convert the string into an array and them implode it:
echo implode("w", str_split("444")) . "w";
Lets say $inp holds your number.
$arr = str_split($inp);
$result = implode('w',$arr);
str_split() and implode() will help.
$var = "444";
$array = str_split($var);
$final = implode("w", $array);
In this case you $final = "4w4w4";, so you might want to append an extra "w" to the end.
Here's a short way to do it with regex:
$str = '444';
echo preg_replace ( '/(.)/', '$1w', $str );
try this
$value="444";
$out='';
for($i=0;$i<strlen($value);$i++){
$out.=$value[$i]. "w";
}
echo $out;
output
4w4w4w