This question already has answers here:
Parse query string into an array
(12 answers)
Closed 8 months ago.
I have a string like this:
"birs_appointment_price=&birs_appointment_duration=&birs_appointment_alternative_staff=&birs_shortcode_page_url=http%3A%2F%2Flocalhost%2Fstreamline-new%2Fworkshop%2F&_wpnonce=855cbbdefa&_wp_http_referer=%2Fstreamline-new%2Fworkshop%2F&birs_appointment_location=17858&birs_appointment_staff=-1&birs_appointment_avaliable_staff=-1%2C17859&birs_appointment_service=-1&birs_appointment_date=&birs_appointment_notes=&birs_appointment_fields%5B%5D=_birs_appointment_notes&birs_client_type=new&birs_client_name_first=&birs_client_fields%5B%5D=_birs_client_name_first&birs_client_name_last=&birs_client_fields%5B%5D=_birs_client_name_last&birs_client_email=&birs_client_fields%5B%5D=_birs_client_email&birs_field_1=&birs_client_fields%5B%5D=_birs_field_1&birs_client_fields%5B%5D=_birs_field_6&s="
I want to parse it to array in php. How to do it ?. Thanks for your help so much.
You use parse_str for this. (http://php.net/manual/en/function.parse-str.php)
$output = array();
parse_str($str, $output);
echo $output['first']; // value
echo $output['arr'][0]; // foo bar
echo $output['arr'][1]; // baz
Or you can parse it into local variables
$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str);
echo $first; // value
echo $arr[0]; // foo bar
echo $arr[1]; // baz
You can try the following :
inside this foreach you will get all the get parameters and then you can store them in an array.
foreach($_GET as $key => $value) {
//your code here
}
use
parse_str( $string );
Refer: http://php.net/manual/en/function.parse-str.php
$your_str = 'birs_appointment_price=&birs_appointment_duration=&birs_appointment_alternative_staff=&birs_shortcode_page_url=http%3A%2F%2Flocalhost%2Fstreamline-new%2Fworkshop%2F&_wpnonce=855cbbdefa&_wp_http_referer=%2Fstreamline-new%2Fworkshop%2F&birs_appointment_location=17858&birs_appointment_staff=-1&birs_appointment_avaliable_staff=-1%2C17859&birs_appointment_service=-1&birs_appointment_date=&birs_appointment_notes=&birs_appointment_fields%5B%5D=_birs_appointment_notes&birs_client_type=new&birs_client_name_first=&birs_client_fields%5B%5D=_birs_client_name_first&birs_client_name_last=&birs_client_fields%5B%5D=_birs_client_name_last&birs_client_email=&birs_client_fields%5B%5D=_birs_client_email&birs_field_1=&birs_client_fields%5B%5D=_birs_field_1&birs_client_fields%5B%5D=_birs_field_6&s=';
$_VARS = array();
parse_str($your_str, $_VARS);
echo $_VARS['birs_appointment_price'];
Related
This question already has answers here:
array_walk_recursive to apply function to each array element not working at all
(3 answers)
Closed 4 months ago.
I have a string in encoded array format. What I want is to replace the curly bracket in its value only. I have tried like this
$str = '[{"id":"{3e71209}","elType":"section","settings":[],"elements":[{"id":"70e5fb7","elType":"column","settings":{"_column_size":100,"_inline_size":null},"elements":[{"id":"70e09a1","elType":"widget","settings":{"title":"{title1|title2|title3}"},"elements":[],"widgetType":"heading"}],"isInner":false}],"isInner":false}]';
echo 'str asli: '.$str;
echo '<br><br>';
$strOlah = str_replace("{", "xx", json_decode($str));
echo 'str olah: '. json_encode($strOlah);
it's resulting the exact str. nothing change.
My expected result is the str become like this
[{"id":"xx3e71209}","elType":"section","settings":[],"elements":[{"id":"70e5fb7","elType":"column","settings":{"_column_size":100,"_inline_size":null},"elements":[{"id":"70e09a1","elType":"widget","settings":{"title":"xxtitle1|title2|title3}"},"elements":[],"widgetType":"heading"}],"isInner":false}],"isInner":false}]
How to do that without looping through an array because the structure and key of the array are very dynamic.
You need to loop through the array, calling str_replace() on the specific object properties.
$array = json_decode($str);
foreach ($array as $obj) {
$obj->id = str_replace("{", "xx", $obj->id);
foreach ($obj->elements as $el1) {
foreach ($el1->elements as $el2) {
$el2->settings->title = str_replace("{", "xx", $el2->settings->title);
}
}
}
echo 'str olah: ' . json_encode($array);
I recieve information with file_get_contents() and assign it to var like this:
$values = array();
$values = trim(file_get_contents($newurl));
echo gettype($values); // will show "string"
When i echo $values, i see that it is a string like:
[["EUR", "80"],["TRY", "50"],["USD", "40"],["GBP", "60"]];
And gettype($values) also shows "string" instead of array. But if i do
$values = [["EUR", "80"],["TRY", "50"],["USD", "40"],["GBP", "60"]];
echo gettype($values); // will show "array"
It will show me that $values is an array.
How can i tell php that trim(file_get_contents($newurl)) is an array, not a string?
Try this:
$values = json_decode(trim(file_get_contents($newurl)), 1);
echo gettype($values); // Array
1 either use the web-style parameter encoding
$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str, $output);
echo $output['first']; // value
echo $output['arr'][0]; // foo bar
echo $output['arr'][1]; // baz
2 use the explode functions
$string = 'Hello World';
$arr = str_split($string, 3);
print_r($arr);
//Array ( [0] => Hel [1] => lo [2] => Wor [3] => ld )
3 use json ( favoured since you can have assiocative arrays/objects parsed from there
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
for 1 and 2 you will have to apply other tricks since this won't easily work recursive
You can add a function which test if it's a json string :
function isJsonString($ent){
if(!is_string($ent))
return false;
json_decode($ent);
return json_last_error() === JSON_ERROR_NONE;
}
and then, test :
if(isJsonString($values))
$values = json_decode($values); // --> stdObject
This question already has answers here:
explode string into variables
(2 answers)
Closed 5 years ago.
I need to use php and extract the items from the string and assign them to variables.
$string = "76305203 ;124400884 ;109263187 ;current ;18.44";
How can I get:
$var1 = 76305203
$var2 = 124400884
To create variables use list()
<?php
$string = "76305203 ;124400884 ;109263187 ;current ;18.44";
list($var1,$var2) = explode(';', $string);
echo $var1;
echo PHP_EOL;
echo $var2;
Output:- https://eval.in/928536
Or use explode() to get array and use that array
<?php
$string = "76305203 ;124400884 ;109263187 ;current ;18.44";
$array = explode(';', $string);
echo $array[0];
echo PHP_EOL;
echo $array[1];
Output:-https://eval.in/928537
This question already has answers here:
Imploding an associative array in PHP
(10 answers)
Closed 8 years ago.
I have a named array that look like this:
$arr = array('name'=>'somename','value'=>'somevalue');
I want to turn that array into something like this:
name='somename' value='somevalue'
I tried http_build_query() to do it.
echo http_build_query($arr, '', ' ');
But this is the result I get:
name=somename%21 value=somevalue%21
How can I get the result I want with http_build_query()? Or, is there any PHP function to do it?
Thank you.
http_build_query returns a urlencoded string. If you don't want that you can run it through urldecode
$arr = array('name'=>"'somename'",'value'=>"'somevalue'");
print urldecode(http_build_query($arr,null,' '));
Try with foreach()
$arr = array('name'=>'somename','value'=>'somevalue');
$str = '';
foreach($arr as $k=>$v) {
$str.= "$k='$v' ";
}
echo $str;
foreach ($array as $key => $value) {
$result .= "$key='$value' ";
}
echo rtrim($result,' ');
I need to change it into :
$arr['id']=1;
$arr['type']=2;
Use: parse_str().
void parse_str(string $str [, array &$arr])
Parses str as if it were the query string passed via a URL and sets variables in the current scope.
Example:
<?php
$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str);
echo $first; // value
echo $arr[0]; // foo bar
echo $arr[1]; // baz
parse_str($str, $output);
echo $output['first']; // value
echo $output['arr'][0]; // foo bar
echo $output['arr'][1]; // baz
?>
Assuming you want to parse what looks like a query string, just use parse_str():
$input = 'id=1&type=2';
$out = array();
parse_str($input, $out);
print_r($out);
Output:
Array
(
[id] => 1
[type] => 2
)
You can optionally not pass in the second parameter and parse_str() will instead inject the variables into the current scope. Don't do this in the global scope. And I might argue don't do it at all. It's for the same reason that register_globals() is bad.
See parse_str.
$arr = array();
$values = explode("&",$string);
foreach ($values as $value)
{
array_push($arr,explode("=",$value));
}
Use parse_str() with the second argument, like this:
$str = 'id=1&type=2';
parse_str($str, $arr);
$arr will then contain:
Array
(
[id] => 1
[type] => 2
)