PHP: How to make an array by a comma separated string? [duplicate] - php

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);

Related

I want to convert this array like this [duplicate]

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));

How to convert string to PHP array? [duplicate]

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().

Getting the integer from a string in form of a comma separated/decorated number? [duplicate]

This question already has answers here:
php converting formatted int to int
(2 answers)
Closed 6 years ago.
I am parsing a stream of strings like '1,985' and I want to convert them to the respective integer, like 1985 in this case.
Is there any built-in/efficient PHP function which does that?
Use intval and str_replace for getting your desire result. Intval for your integer number and str_replace for remove the comma from string.
$str = '1,985';
var_dump(intval(str_replace(",", "", $str))); //int(1985)

Get specifics value from chain in PHP [duplicate]

This question already has answers here:
Parse query string into an array
(12 answers)
Closed 8 years ago.
My chain in PHP is like the following:
$chain = "m=toto&i=12&a=new";
How to get m, i and a values ?
Thanks.
Try This:
<?php
$chain = "m=toto&i=12&a=new";
parse_str($chain,$array);
This will create an array named $array containing all values you can access them as $array['m']
You can Print all this by:
print_r($array);

How to grab seperated integers from long string [duplicate]

This question already has answers here:
Separate space-delimited words in a string [duplicate]
(4 answers)
Closed 11 months ago.
I am working on a enrollment system for a judo club. I am using a string to store the ID's of people, who showed up. It looks somthing like this:
0,3,4,7,8,10,
Now, I want to seperate each number into an integer array in PHP. What would the simplest solution to this be? :)
Use explode()
$ids = explode(',', '0,3,4,7,8,10');
$str = '0,3,4,7,8,10';
$arr = explode(',',$str);
use EXPLODE
$num = "0,3,4,7,8,10";
$pieces = explode(",", $num );
Explode Manual

Categories