How to convert string variable to an array in php? - php

I have one string variable as
$p_list="1,2,3,4";
i want to convert it to an array such as
$a[0]='1';
$a[1]='2';
$a[2]='3';
$a[3]='4';
how to do this in php?

Use explode.
$p_list = "1,2,3,4";
$array = explode(',', $p_list);
See Codepad.

Try explode $a=explode(",","1,2,3,4");

Try this:
In PHP, explode function will convert string to array, If string has certain pattern.
<?php
$p_list = "1,2,3,4";
$noArr = explode(",", $p_list);
var_dump($noArr);
?>
You will get array with values stores in it.
Thanks

Related

How to store the string values into an array in php?

I have one parameter every time it comes from the request object in a standard order,I want to store that string into an array by splitting \for that i am using explode function it's throwing an error,please help me to explode with backslash
$obj = "App\Http\Data\User";
$array = explode("\",$obj);
You can use this:
$obj = "App\Http\Data\User";
$array = explode("\\",$obj);
print_r($array);
example here

using PHP convert the string values to array and get the values

my $data returns following values.the gettype() of the following values is a String.
["0:12:23", 0.000000],["0:12:43", 0.000000],["0:13:03", 0.000000]
I want to convert the string vlaues to an array.and have to out the time values "0:12:23" , "0:12:43" and "0:13:03"
How can i conver the string values to array using php and get the time vlaues only.
I hope that your input is in single string.So do like below:-
<?php
$string = '["0:12:23", 0.000000],["0:12:43", 0.000000],["0:13:03", 0.000000]'; // i assume it's a single string
preg_match_all('/\d:\d{2}:\d{2}/',$string,$matches);
print_r($matches);
Output:- https://eval.in/895248
Or as #deceze said use json_decode("[$string]") like below:-
<?php
$string = '["0:12:23", 0.000000],["0:12:43", 0.000000],["0:13:03", 0.000000]';
$string_array=json_decode("[$string]");
$time_array = array_column($string_array,0);
print_r($time_array);
?>
Output:- https://eval.in/895251
Reference:-
preg_match_all()
json_decode()
array_column()
You can use json_decode to convert it into array structure.and use foreach loop to filter out .try below,
<?php
$string = '["0:12:23", 0.000000],["0:12:43", 0.000000],["0:13:03", 0.000000]';
$string_to_array=json_decode("[$string]");
foreach($string_to_array as $values){
print_r($values[0]);
}
?>

how to convert string into single dimension array in php?

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

How to explode vaules in $_GET['']=1#5#6 in php?

Hi in my url i have variables stored in ../index.php?cat=1#5#8 in so on how to separate them using explode function so out put could be
arr[0]=1
arr[1]=5
arr[2]=8
Try with explode like
$arr = explode("#",$_GET['cat']);
But as #Bora said after # the remaining string will may not be sent ,so better to use '_' in place of '#' (1_5_8_....)and can explode it like
$arr = explode("_",$_GET['cat']);
Try this:
$array=$_GET['cat'];
$result = explode("#",$array);

How do I place a comma between each character in a string with PHP? [duplicate]

hey there I have this,
$following_user_id .= $row['following_user_id'];
and I get
44443344330
then I use the implode() function and seperate with commans
44,44,33,44,33,0,
but I don't want the last comma on the last number?
Is this possible?
$following_user_ids = array();
//loop this:
$following_user_ids[] = $row['following_user_id'];
$user_ids_string = implode(',',$following_user_ids);
You can split the string into an array of characters, then implode the array.
$array = preg_split('//', $following_user_id, -1, PREG_SPLIT_NO_EMPTY);
echo implode( ',', $array );
Collect your data into an array of strings and use the implode function:
$uids = array();
while($row = mysql_fetch_assoc($result)){
array_push($uids, $row['following_user_id']);
}
$following_user_id = implode(',', $uids);
Check implode: http://php.net/manual/en/function.implode.php
Code example: I'm assuming your using some sort of loop?
$arrUsers = new array();
... your loop code here ...
array_push($arrUsers, $row['following_user_id']);
... end loop code ..
$following_user_id = impload(",", $arrUsers);
Implode should not be inserting a comma at the end of that string there. Are you sure there isn't an empty string at the end of your array sequence?
Either way, to fix the string you have, just get rid of the last character of the string:
$concatUserIds = "44,44,33,44,33,0,";
$concatUserIds = substr($concatUserIds, 0, strlen($concatUserIds) - 1);
Further, if you're not going to be using the non-comma delimited number set, why don't you just add a comma every time you add a user id. That way you don't even have to use the implode function.
This works for me:
<?php
$following_user_id.= $row['following_user_id'];
$following_user_id=preg_replace('/(?<=\d)(?=(\d)+(?!\d))/',',',$following_user_id);
echo $following_user_id."<br>";
?>
Try using arrays, example
<?php
$arr = array();
$arr[] = 'foo';
$arr[] = 'bar';
echo implode(',', $arr);

Categories