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

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.

Related

How do I extracting the multidimensional array value in php? [duplicate]

This question already has answers here:
Looping a multidimensional array in php
(3 answers)
Output multidimensional array with keys and values
(3 answers)
Closed 4 months ago.
How do I get the value of the associative arrays for the year and quarter in a loop in php? I want to extract the numbers like 2021 4, the other value doesn't matter.
$this->financials[2021][4] = 5;
$this->financials[2022][1] = 7;
$this->financials[2022][2] = 9;
$this->financials[2022][3] = 11;
I attempted this method, but it didn't work, but hopefully gives you a better idea of what I am trying to achieve. I was hoping for the result e.g. 20214
foreach($this->financials as $f[$y][$q]) {
echo $y.$q;
}
$q in your foreach loop is an associative array so you have to break it down with another loop.
foreach ($this->financials as $year => $quarterArray) {
foreach ($quarterArray as $quarter => $value) {
echo $year.$quarter;
}
}

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;

Array is not getting sorted with sort() function PHP [duplicate]

This question already has answers here:
Sort array not working
(2 answers)
Closed 10 months ago.
I'm trying to sort an array numerically. Here's my code
<?php
$data = '9#Saul,7#Jesse,1#Skyler,6#Walter';
$exp = explode(",",$data);
$expsort = sort($exp);
print_r($expsort);
?>
But it is not working. The output is showing only "1".
You are assigning the value of the sort function -which sorts the argument array itself, and it always returns true and thus you got 1 as a result.
So if you print your original exploded array, it will be sorted. Please note, sort overrides your original array
$data = '9#Saul,7#Jesse,1#Skyler,6#Walter';
$exp = explode(",",$data);
sort($exp);
print_r($exp);

Laravel WhereIn array returns only first index results [duplicate]

This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 10 months ago.
$dean_ids = Auth::user()->dean_id; // "9,11"
$subjects = Subject::whereIn('dean_id', [$dean_ids])->select('id')->get();
returns only data for "9" but when i trying like this:
$subjects = Subject::whereIn('dean_id', [9,11])->select('id')->get();
//it returns all data that what i want.
how can i fix it?
As I see, this line $dean_ids = Auth::user()->dean_id; returns a comma-separated string. So when you make $dean_ids array by using [$dean_ids] it actually makes an array like:
array(
'9,11'
)
Instead of
array(
9,
11
)
There is only one value inside the array. So what you can do just use explode for splitting the string by using a comma and it also returns an array.
You can try this:
$subjects = Subject::whereIn('dean_id', explode(',', $dean_ids))->select('id')->get();

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));

Categories