how to save array outputs inside variables in php [duplicate] - php

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;

Related

How to convert string on format array to array [duplicate]

This question already has answers here:
How to get element from json with php? [duplicate]
(2 answers)
Closed 3 years ago.
I have a string variable in this format:
$array = '[{"name":"jack","address":"who knows"},{"name":"jill","address":"who knows too"}]';
how can I get value from
$array[0]['name']; //value is 'jack'
https://juderosario.com/2016/09/01/parsing-json-with-php/
ok I get it from there
$json = '[ {"id":"1", "name":"foo" }, {"id":"2", "name":"bar" } ]';
$baz = json_decode($json,true);
echo($baz[1]['name']); //bar

PHP string array to php array [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
I have this string array:
["652","110","111","1032","118","416","147","121","112","1033","113","1031","868"]
I need to read each value so to get
652
110
111
1032
i try to convert string array using explode and then foreach but is not working...
$channels = explode('"', $string_array);
foreach($channels as &$channel) {
echo $channel.'<br>';
}
it's an JSON format, so use json_decode
$json = '["652","110","111","1032","118","416","147","121","112","1033","113","1031","868"]';
$array = json_decode($json, true);
foreach($array AS $channel) {
echo $channel.'<br>';
}

How to extract specific part of string where the length of the part changes? [duplicate]

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

How to cut string before and after comma PHP [duplicate]

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

convert string to array and get value in php [duplicate]

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.

Categories