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
Related
This question already has answers here:
Fastest way of deleting a value in a comma separated list
(4 answers)
Closed 6 months ago.
I'm doing a cart system, when a client select various items the data is sent as string with IDs and comma as delimiter, example: 1,2,3
I save that to the database and I can show each item description using a custom function and foreach. The problem is if a client want to remove a item, how should I proceed to update the new changes to the DB? Because I tried with str_replace and I can only remove the ID but keeps the delimiter (comma), example: 1,2, or 1,,3
Any ideas or suggestions how can I improve this? Thank you for your time.
Regards.
You should really normalise the schema, but...
There are many ways to do this. Here's one using using explode() and implode().
function removeIdFromString(string $str, string$id):string {
$arr = explode(',',$str);
if (($key = array_search($id, $arr)) !== false) {
unset($arr[$key]);
return implode(',',$arr);
}
}
$str = "3,5,7,9";
echo removeIdFromString($str,7); // 3,5,9
echo removeIdFromString($str,9); // 3,5,7
Good for PHP7+
Demo:https://3v4l.org/qBP9q
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 2 years ago.
I Have a string like this
{"Account":{"Currency":"SGD","CreditLimit":0.0000,"Balance":1649.5700},"Status":36,"Code":891,"Message":"Success"}-
I need the value of Balance.
I tried like this.
$string = '{"Account":{"Currency":"SGD","CreditLimit":0.0000,"Balance":1649.5700},"Status":1,"Code":1,"Message":"Success"}-';
$withCharacter = strstr($string, 'Balance":');
echo substr($withCharacter, 1);
Tried to use explode also but no luck.
This seems like a valid JSON, why not json_decode and find the value:
$i = json_decode('{"Account":{"Currency":"SGD","CreditLimit":0.0000,"Balance":1649.5700},"Status":36,"Code":891,"Message":"Success"}');
echo $i->Account->Balance;
This question already has answers here:
How do I count comma-separated values in PHP?
(5 answers)
Why does counting an empty string return 1?
(3 answers)
Count() returning 1 with comma separated string with explode [duplicate]
(2 answers)
Closed 3 years ago.
i have this data which when tested with direct input it works and file_put_contents confrims that data is exactly same but when i try to get the value through site it gives only 1
i tried to declare array but everytime it gives 1 only
this array count gives count as 6
$total_id_counttt = count(array(13068,13067,13066,13065,13064,13063));
echo $total_id_counttt;
but when i use this here it return 1
$str_finalppo ='13068,13067,13066,13065,13064,13063';
$schools_array = array($str_finalppo);
$total_id_counttt = count($schools_array);
it returns 1 can anyone tell where i am doing mistake
First of all, Convert the string into an array. Look it in this code you may get the idea.
Use explode function to convert string to array.
$str_finalppo ='13068,13067,13066,13065,13064,13063';
$schools_array = explode(',',$str_finalppo);
$total_id_counttt = count($schools_array);
Check more about explode
In PHP, value in quotes('') consider as string.
So when you try to count this is always returning the 1.
Try to convert the string into an array by using explode().
for example:
$str_finalppo ='13068,13067,13066,13065,13064,13063';
$schools_array = explode(',',$str_finalppo);
echo $total_id_counttt = count($schools_array);
this will give you the right answer.
And if you still confuse try to print #schools_array before using dd($schools_array);
It will definitely remove your confusion.
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:
json_decode to array
(12 answers)
How can I access an array/object?
(6 answers)
Closed 11 months ago.
I have a script which records your WAN and LAN IP, but it also has a bunch of unnecessary characters around the printed answer. How can I split the IPs and get rid of these characters?
Printed Answer: {"ip":[["WANIP","LANIP"]]}
What I want is 2 different variables, 1 to print wan and 1 to print lan.
I've tried with str_split and explode, maybe I didn't do it right or I can't do it with these, so any answers would help.
$json = '{"ip":[["WANIP","LANIP"]]}';
$decoded = json_decode($json, true); // true means it will format it into an assoc array
Then you'll be able to access your wanted strings by simply using $decoded['ip'][0][0] and $decoded['ip'][0][1] .
It looks like your "printed answer" is JSON. In which case parse the json then extract the needed values.
You could use something like json_decode()
try this code:
$data = "{\"ip\":[[\"WANIP\",\"LANIP\"]]}";
$jdecode = json_decode($data,true);
echo $jdecode['ip'][0][0];
echo $jdecode['ip'][0][1];
Hope this helps :)
I would first strip out some of the unnecessary clutter, then explode:
$response = '{"ip":[["WANIP","LANIP"]]}'; //or however you load your variable
$arryRemove = array('"', 'ip:[[', ']]}'); //specify an array of things to remove
$response = str_replace($arryRemove, "", $response);
$arryIPs = explode(",", $response);
$arryIPs[0] will contain WANIP,
$arryIPs[1] will contain LANIP