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
Related
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]);
}
?>
I want to get the count of each variable in an array.
Am getting the object like [1,1,2,2,3,4,5,4,6]
$array = array(12,43,66,21,56,43,43,78,78,100,43,43,43,21);
$vals = array_count_values($array);
This function is the solution. But I can't make the collection object to string or array.
I noticed this is tagged with laravel, in that case you could do collect($vals)->toArray(). If I remember correctly.
edit: you are looking for the php function implode.
http://php.net/manual/en/function.implode.php
string implode ( string $glue , array $pieces )
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 can I add a variable to an array? Let say I have variable named $new_values:
$new_values=",543,432,888"
And now I would like to add $new_values to function. I tried in that way:
phpfunction1(array(114,763 .$new_values. ), $test);
but I got an error Parse error: syntax error, unexpected T_VARIABLE, expecting ')'
How my code should look if I would like to have array(114,763,543,432,888)?
$new_values=",543,432,888";
should be converted to an array:
$new_values= explode(',', "543,432,888");
and merged to existing values with:
array_merge(array(114,763), $new_values);
Whole code should looks like:
$new_values = explode(',', "543,432,888");
$values = array(114,763);
$values = array_merge($values, $new_values);
phpfunction1($values, $test);
If you pass to explode a string that is starting with , you will get first empty element, so avoid it.
if you have an array already i.e.
$values = array(543,432,888);
You can add to them by : $values[]=114; $values[]=763;
Apologies if I missed the point there...
In your example, $new_values is a string, but, since it's comma delimited, you can create an array directly from it. Use $new_array = explode(',', $new_values); to create an array from the string.
You need to convert the string into an array using the explode function and then use the array_merge function to merge the two arrays into one:
$new_values=",543,432,888";
$currentArray=array(114,763);
$newArray=array_merge($currentArray,explode(',',$new_values));
functionX($newArray...)
But watch out for the empty array element because of the first comma.
For that use "trim($new_values, ',')" - see answer from rajesh.
you can do like this.
$old_values = array(122,555);
$new_values=",543,432,888";
$values = explode(',', trim($new_values, ','));
$result = array_merge($old_values, $values);
print_r($result);
try array merge
looks like this
phpfunction1(array_merge(array(114,763) ,$new_values), $test);
and yes your first array is not an array
change it to this
$new_values=Array(543,432,888);
How to convert a string format into an array format?
I have a string, $string = 'abcde'
I want to convert it to a 1 element array
$string[0] = 'abcde'
Is there a built in function for this task? Or the shortest way is to
$string = 'abcde';
$array[0] = $string;
$string = $array;
TIA
All kinds of ways in php..
$array = array('abcde');
$array[] = 'abcde';
Etc... not too sure what you're going for.
Edit: Oh, I think you might want to convert the first variable? Like this?
//Define the string
$myString = 'abcde';
//Convert the same variable to an array
$myString = array($myString);
Edit 2: Ahh, your comment above I think clears it up a little. You're getting back either an array or a string and don't know which. If you do what I just said above, you might get an array inside an array and you don't want that. So cast instead:
$someReturnValue = "a string";
$someReturnValue = (array)$someReturnValue;
print_r($someReturnValue);
//returns
Array
(
[0] => a string
)
$someReturnValue = array("a string inside an array");
$someReturnValue = (array)$someReturnValue;
print_r($someReturnValue);
//returns
Array
(
[0] => a string inside an array
)
Lets not forget the standard way of declaring a string as an array in PHP, before adding elements..
$string = array();