I wonder if someone can help me extract values from an array by key value rather than array number.
我想知道是否有人可以帮助我通过键值而不是数组值从数组中提取值。
$json = json_decode($stock, true);
print_r($json);
$sims = $json['stock'][1]['sims'];
foreach ($sims as $sim)
{
echo nl2br($sim . "\n");
}
The output from print_r($json)
is:
print_r($ json)的输出是:
Array ( [stock] => Array (
[0] => Array
( [operator] => ECL [sims] => Array
( [0] => 8944122616994 [1] => 89650264517182 [2] => 894412265075 [3] => 894412 ) )
[1] => Array
( [operator] => JT [sims] => Array
( [0] => 89445023065 [1] => 894156673081 [2] => 8944501 [3] => 89445027 ) ) ) )
It appears that sometimes the data I want is not in array number 1 hence I would like to extract it based on "[operator] => JT"
I've been trying various ideas but it never seems to work.
似乎有时我想要的数据不在数组1中,因此我想基于“[operator] => JT”提取它我一直在尝试各种各样的想法,但它似乎永远不会起作用。
3 个解决方案
#1
2
You can accomplice it by using array_search and array_column
您可以使用array_search和array_column来帮助它
This will give you the multidimensional array key
这将为您提供多维数组键
$key = array_search("JT", array_column($json['stock'], 'operator'));
Then you can do
那你可以做
$sims = $json['stock'][$key]['sims'];
print_r($sims) //this will print desired array
#2
0
Try:
尝试:
$json = json_decode($stock, true);
foreach($json["stock"] as $arr){
echo $arr["operator"]."\n";
foreach($arr["sims"] as $sim){
echo $sim."\n";
}
echo "\n";
}
This would output (for example):
这将输出(例如):
ECL
8944122616994
89650264517182
894412265075
894412ECL 8944122616994 89650264517182 894412265075 894412
JT
89445023065
894156673081
8944501
89445027JT 89445023065 894156673081 8944501 89445027
#3
0
Write a function like this:
写一个这样的函数:
function getSims(array $array) {
$sims = [];
foreach ($array as $data) {
if ($data['operator'] == 'JT') {
return $data['sims'];
}
}
// here you could also throw an exception or return something else
return [];
}
and use it like this:
并像这样使用它:
$json = json_decode($stock, true);
$sims = getSims($json['stock']);
#1
2
You can accomplice it by using array_search and array_column
您可以使用array_search和array_column来帮助它
This will give you the multidimensional array key
这将为您提供多维数组键
$key = array_search("JT", array_column($json['stock'], 'operator'));
Then you can do
那你可以做
$sims = $json['stock'][$key]['sims'];
print_r($sims) //this will print desired array
#2
0
Try:
尝试:
$json = json_decode($stock, true);
foreach($json["stock"] as $arr){
echo $arr["operator"]."\n";
foreach($arr["sims"] as $sim){
echo $sim."\n";
}
echo "\n";
}
This would output (for example):
这将输出(例如):
ECL
8944122616994
89650264517182
894412265075
894412ECL 8944122616994 89650264517182 894412265075 894412
JT
89445023065
894156673081
8944501
89445027JT 89445023065 894156673081 8944501 89445027
#3
0
Write a function like this:
写一个这样的函数:
function getSims(array $array) {
$sims = [];
foreach ($array as $data) {
if ($data['operator'] == 'JT') {
return $data['sims'];
}
}
// here you could also throw an exception or return something else
return [];
}
and use it like this:
并像这样使用它:
$json = json_decode($stock, true);
$sims = getSims($json['stock']);