how to convert string into single dimension array in php? - 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);

Related

PHP find if numbers contained in a string are present in another string

Hi,
I have one string like this:
4,21
and the other like this:
,4,5,6,14,21,22,
I need to find out if any of the numbers contained in the first string are present in the second string but since each number is separated by a comma Im a little confused. What function should I use?
Thank you.
You could convert each string into arrays.
Try this:
$str1 = "4,21";
$array1 = explode(",", $str1);
$str2 = "4,5,6,14,21,22";
$array2 = explode(",", $str2);
$common = array_intersect($array1, $array2);
echo "Common numbers:<br/>";
echo implode(",", $common);

Efficient way to split a string into an array - PHP

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

How to convert string variable to an array in 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

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

Convert basic array to PHP array

I have an array that I pull that looks like this:
[157966745,275000353,43192565,305328212]...
How do I go about taking that "string" and converting it to a PHP array which I can then manipulate.
This looks like JSON, so you can use json_decode:
$str = "[157966745,275000353,43192565,305328212]";
$data = json_decode($str);
$s = "[157966745,275000353,43192565,305328212]";
$matches;
preg_match_all("/\d+/", $s, $matches);
print_r($matches);
With exact that code...
$string='[157966745,275000353,43192565,305328212]';
$newString=str_replace(array('[', ']'), '', $string); // remove the brackets
$createArray=explode(',', $newString); // explode the commas to create an array
print_r($createArray);
PHP explode is built just for this.
$result = explode(',', $input)
preg_replace('/([\[|\]])/','',$strarr);
$new_arr=explode(','$strarr);

Categories