This question already has answers here:
comma-separated string to array
(3 answers)
Closed 8 years ago.
$MyValue = "1111,5555";
I want result this.
$Value_1 = "1111";
$Value_2 = "5555";
Please help me.
You can use explode():
$MyValue = '1111,5555';
$MyValue = explode(',', $MyValue);
echo $MyValue[0]; //1111
echo $MyValue[1]; //5555
See demo
Related
This question already has answers here:
Php how to parse Json
(4 answers)
Closed 4 months ago.
I have this array output :
{"NoID":5645656956,"dispositionMessage":"ISSUED","binarySecurityToken":"TUlJQ1VEQ0NBZldnQXdJQkFnSUdBWVBDdjV4OE1Bb0dDQ3FHU000OUJBTUNNQlV4RXpBUkJnTlZCQU1NQ21WSmJuWnZhV05wYm1jd==","number":"1234568","errors":null}
and want to save these array outputs inside these variables:
$NoID = 5645656956;
$binarySecurityToken = TUlJQ1VEQ0NBZldnQXdJQkFnSUdBWVBDdjV4OE1Bb0dDQ3FHU000OUJBTUNNQlV4RXpBUkJnTlZCQU1NQ21WSmJuWnZhV05wYm1jd==;
$number = 1234568;
Thanks for all
You need to decode the string into an array or object. Then you can access all the properties.
$jsonString = '{"NoID":5645656956,"dispositionMessage":"ISSUED","binarySecurityToken":"TUlJQ1VEQ0NBZldnQXdJQkFnSUdBWVBDdjV4OE1Bb0dDQ3FHU000OUJBTUNNQlV4RXpBUkJnTlZCQU1NQ21WSmJuWnZhV05wYm1jd==","number":"1234568","errors":null}';
$json = json_decode($jsonString);
$noId = $json->NoID;
$binarySecurityToken = $json->binarySecurityToken;
$number = $json->number;
This question already has answers here:
How to get a substring between two strings in PHP?
(39 answers)
Closed 5 years ago.
I'm trying to only get the first 19.45 from this string `` Is there a way to grab everything in between ..."raw": AND ,"fmt... so i only get the number. Hope someone can help me...
UPDATE: found what I was looking for.
How to get a substring between two strings in PHP?
<?php
$str = "targetMeanPrice":{"raw":19.45,"fmt":"19.45"}";
function GetBetween($var1='',$var2='',$pool){
$temp1 = strpos($pool,$var1)+strlen($var1);
$result = substr($pool,$temp1,strlen($pool));
$dd=strpos($result,$var2);
if($dd == 0){
$dd = strlen($result);
}
return substr($result,0,$dd);
}
echo GetBetween('raw":',',"fmt', "$str" );
?>
output = 19.45
$decodedJSON = json_decode($yourJson);
$raw = $decodedJSON->targetMeanPrice->raw;
$raw will now contain the value of raw, which is 19.45
or
$decodedJSON = json_decode($yourJson,true);
$raw = $decodedJSON['targetMeanPrice']['raw']
source
Try this:
<?php
$json = '{"targetMeanPrice":{"raw":19.45,"fmt":"19.45"}}';
$json = json_decode($json);
echo $json->targetMeanPrice->raw;
?>
You can use strpos(...) to find "raw": and second strpos(..) to find ,"fmt, than calculate length/difference from returned indexes by strpos(...) and use substr(...) to extract expected value
This question already has answers here:
Using PHP to split a URL
(4 answers)
Closed 8 years ago.
if(isset($_SERVER['HTTP_REFERER'])) {
$sitio = $_SERVER['HTTP_REFERER'];
}
I need to split $sitio so i have "example" instead of "http://example.com/subfolder" or "http://www.example.com/subfolder"!
You can use parse_url.
if(isset($_SERVER['HTTP_REFERER'])) {
$sitio = parse_url($_SERVER['HTTP_REFERER'])['host'];
}
This question already has answers here:
PHP string to array
(4 answers)
Closed 8 years ago.
I have a complete string how can i get some part of it and insert into an array this is my string in php
[{"albumid":"ASaBFzCtl8","albumname":"anni","type":"3","access":"2","itemcount":"2"},{"albumid":"EmgsZ43ehT","albumname":"testalbum","type":"1","access":"1","itemcount":"0"},{"albumid":"Jf4H4SvFGk","albumname":"test2album","type":"3","access":"1","itemcount":"0"},{"albumid":"k3pacBSmIl","albumname":"testalbumpvt","type":"3","access":"2","itemcount":"0"}]
i want something similar to this
$value1 = $array[0];
// {"albumid":"ASaBFzCtl8","albumname":"anni","type":"3","access":"2","itemcount":"2"}
is this possible to get each value like this
$value1 = $array[0]['albumid'];
// ASaBFzCtl8
Yes use json_decode()
$j = '[{"albumid":"ASaBFzCtl8","albumname":"anni","type":"3","access":"2","itemcount":"2"},{"albumid":"EmgsZ43ehT","albumname":"testalbum","type":"1","access":"1","itemcount":"0"},{"albumid":"Jf4H4SvFGk","albumname":"test2album","type":"3","access":"1","itemcount":"0"},{"albumid":"k3pacBSmIl","albumname":"testalbumpvt","type":"3","access":"2","itemcount":"0"}]';
$data = json_decode($j,true);
You can use loop to read the data as
foreach($data as $key=>$val){
echo $val["albumid"]."<br />";
}
Above will just get the albumid you can read whatever you want from this array.
This question already has answers here:
Add a prefix to each item of a PHP array
(7 answers)
Closed 9 years ago.
I have an array in PHP:
$pbx01_connection = array("customer/voip_extensions.php");
how can i add a prefix and suffix to each item in the array?
for example,
$pbx01_connection = '/admin/'.array("customer/voip_extensions.php");
so /admin/ is added before each item in the array?
Use array_map():
<?php
function addPrefix($value)
{
return '/admin/' . $value
}
$new_array = array_map("addPrefix", $array);
print_r($new_array);
?>