PHP string array to php array [duplicate] - php

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>';
}

Related

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

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;

How to convert string to json, and get all key's value in PHP [duplicate]

This question already has answers here:
convert query String to json in php
(3 answers)
Closed 3 years ago.
I have some strings, the format like this
name=bob&age=10&sex=male&weight=80&...
And I want to convert it to json format
{
"name":"bob",
"age":"10",
"sex":"male",
"weight":"80",
//and more
}
I wrote some codes, but I don't know how to continue
$co="name=bob&age=10&sex=male&weight=80&...";
$toarray = explode("&", $co);
Does someone give some tips? Many thanks.
You can use parse_str for the same,
parse_str("name=bob&age=10&sex=male&weight=80", $output);
echo json_encode($output);
print_r($output); // if you need normal array.
Explanation: It will capture all URL string as an array to output. As I see you need JSON string I used json_encode to convert array to string.
Here is link you can refer for details.
Demo.
$co="name=bob&age=10&sex=male&weight=80&...";
$result = [];
foreach (explode('&', $co) as $item) {
list($key, $value) = explode('=', $item);
$result[$key] = $value;
}
echo json_encode($result);

How to Convert Range of Integers into PHP Array [duplicate]

This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 4 years ago.
I'm trying to convert these integers/numbers:
$groups = '1707,1723,1733,16254,16256,18704,19792,29268,34956';
into an array in PHP array:
array("1707","1723","1733","29268","34956");
I'm using this
$tags = array();
foreach($groups as $index){
array_push($tags, $index);
}
But keep getting the below error.
Error: Invalid argument supplied for foreach()
simple explode call:
$array=explode(',',$groups);
The explode() function breaks a string into an array.
$groups = '1707,1723,1733,16254,16256,18704,19792,29268,34956';
print_r(explode(",",$groups));

Converting a JSON object into an array and returning array item [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 7 years ago.
I Want to convert the string into an array and return the rating value. I've been able to return it as a string so its nothing wrong with the first line of code.
JSON:
{
"id": "23",
"rating": "31"
}
Code:
$json = file_get_contents('http:*****='.$film->id.'') ;
$array = json_decode($json,TRUE);
echo $array[1] ;
Line 3 should be:
echo $array['rating'];

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