This question already has answers here:
Convert flat array to a delimited string to be saved in the database
(13 answers)
Closed 10 months ago.
I want to convert this array ["Sleep","Wake"] to string like this Sleep Wake .
What is the way to do this?
Use implode,
$array = ['Sleep', 'Wake'];
var_dump(implode(" ", $array));
Related
This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 5 years ago.
I have this code: 37,40,42,46,49,54,56,57 now I wanna separate each number and convert all numbers to an Array.
The array that I want is:
array(37,40,42,46,49,54,56,57)
You can use explode() like this:
$string = "37,40,42,46,49,54,56,57";
$array = explode("," , $string);
This question already has answers here:
Is there a PHP function to convert a query string to an array?
(2 answers)
Convert backslash-delimited string into an associative array
(4 answers)
Closed 6 years ago.
I have the following string
sender=48&destination=51&message=hi+good&sender=48&destination=49&message=good+boy
Please help me convert that into PHP array as following
array = array(
'sender'=>48,
'destination'=>51,
'message'=>hi+good,
'sender'=>48,
'destination'=>49,
'message'=>good+boy
);
Note: Its not PHP GET.
This should work as inteded, to solve this problem, you just need to use explode() correctly, otherwise it's easy.
Here you go :
$firstarr=explode('&',$yourstring);
$desiredarr=array();
foreach($firstarr as $pair)
{
$exp_pair=explode('=',$pair);
$desiredarr[$exp_pair[0]]=$exp_pair[1];
}
print_r($desiredarr);
If it is from query string then you can just use $_REQUEST otherwise you need to explode() string using & as separator. Then for each item in array that explode() generate, you split with = and add it to final array
or using parse_str().
This question already has answers here:
Parse query string into an array
(12 answers)
Closed 7 years ago.
This is the value i get from db.
pkid=1&ordernumber=54322&ordervalue=12345&response=2&scheduleId=1
Want to extract response from this.That is 2.
Here it is
$str ='pkid=1&ordernumber=54322&ordervalue=12345&response=2&scheduleId=1';
parse_str($str);
echo $response; // output :- 2
This question already has answers here:
How to "flatten" a multi-dimensional array to simple one in PHP? [duplicate]
(23 answers)
Closed 9 years ago.
how to split received values of an array into a new array?
so from this:
[["+","+","+"],["-","+","+"],["*","+","+"],["\/","+","+"],
to this:
["+"],["+"],["+"],["-"],["+"],["+"],["*"],["+"],["+"],
can someone help me?
Flatten your array by looping through it
$aFlattened = array();
foreach($aOriginalArray AS $aOperators){
$aFlattened = array_merge($aFlattened, $aOperators);
}
This question already has answers here:
How do I count comma-separated values in PHP?
(5 answers)
Closed 9 years ago.
$string = "oidjdssd , odi,jdois, 3089u,, oisdjsd";
How do i find out if theres more than 3 commas in the string above in the best way?
I would suggest substr_count. You can see if the result is >3 to see if there's more than three.
echo count_chars($string)[ord(',')];
Or for PHP<5.4
$chars = count_chars($string);
echo $chars[ord(',')];
BTW: As it seems, that you are handling CSV-data, you should have a look at str_getcsv()