我有一个如下所示的JSON响应。
显示了一个print_r结果的示例Lerp
我只想从响应中提取名称。
我应该在数组中循环使用,从数组中的每个对象中提取每个名称,然后将其推入数组中,还是应该使用以下代码?
(
[0] => stdClass Object
(
[name] => Venezuela (Bolivarian Republic of)
[topLevelDomain] => Array
(
[0] => .ve
)
[alpha2Code] => VE
[alpha3Code] => VEN
[callingCodes] => Array
(
[0] => 58
)
[capital] => Caracas
[cioc] => VEN
),
[1] => stdClass Object
(
[name] => Venezuela (Bolivarian Republic of)
[topLevelDomain] => Array
(
[0] => .ve
)
[alpha2Code] => VE
[alpha3Code] => VEN
[callingCodes] => Array
(
[0] => 58
)
[capital] => Caracas
[cioc] => VEN
),
[2] => stdClass Object
(
[name] => Venezuela (Bolivarian Republic of)
[topLevelDomain] => Array
(
[0] => .ve
)
[alpha2Code] => VE
[alpha3Code] => VEN
[callingCodes] => Array
(
[0] => 58
)
[capital] => Caracas
[cioc] => VEN
),
....
)
哪个将是最佳选择,为什么?
答案 0 :(得分:1)
根据我的研究,您应该使用foreach()提取属性,
处理每条上百万条记录的巨大数组比array_map()快得多
有关更多信息,请遵循this link。
答案 1 :(得分:0)
我只是通过一个简单的foreach循环来做到这一点:
static int resolve(int... nums) {
int[] dp = new int[nums.length];
dp[0] = nums[0];
int right = 0;
for (int i = 1; i < nums.length; i++) {
if (nums[i] >= i) {
int realNum = nums[i] - i;
if (realNum >= dp[right]) {
right++;
dp[right] = realNum;
} else {
dp[binarySearch(dp, 0, right, realNum)] = realNum;
}
}
}
return nums.length - (right + 1);
}
static int binarySearch(int[] nums, int left, int right, int key) {
while (left <= right) {
int mid = (left + right) >>> 1;
if (nums[mid] > key) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return left;
}
答案 2 :(得分:0)
我使用了此脚本,生成了具有500,000个寄存器的数组/ json:
<?php
ini_set('memory_limit', '-1');
set_time_limit(0);
for ($i = 0; $i < 500000; $i++) {
$response[] = [
'name' => uniqid(),
'topLevelDomain' => ['ve'],
'alpha2Code' => 'VE',
'alpha3Code' => 'VEN',
'callingCodes' => [58],
'capital' => 'Caracas',
'cioc' => 'VEN',
];
}
$response = json_encode($response);
//for
$time = microtime(true);
$data = json_decode($response);
$namesFor = [];
for($i = 0, $c = count($data); $i < $c; $i++) {
$namesFor[] = $data[$i]->name;
}
echo "<br/> Time with for loop: ";
echo microtime(true) - $time;
//array_column
$time = microtime(true);
$data = json_decode($response, true);
$namesArrayColumn = array_column($data, 'name');
echo "<br/> Time with array_column: ";
echo microtime(true) - $time;
//foreach
$time = microtime(true);
$data = json_decode($response);
$namesForeach = [];
foreach($data as $d) {
$namesForeach[] = $d->name;
}
echo "<br/> Time with foreach: ";
echo microtime(true) - $time;
//array_map
$time = microtime(true);
$data = json_decode($response);
$namesArrayMap = [];
$namesArrayMap = array_map(function($d) {
return $d->name;
}, $data);
echo "<br/> Time with array_map: ";
echo microtime(true) - $time;
输出为
for循环的时间:2.0891849994659
使用array_column的时间:7.5789909362793
每次上架时间:6.3916020393372
使用array_map的时间:7.6288249492645
因此,因为最快,所以foreach,array_column和array_map方法要慢得多。但是,使用100,000个寄存器运行时,差异最小:
for循环的时间:0.40081810951233
array_column的时间:0.40819096565247
使用foreach的时间:0.44123411178589
使用array_map的时间:0.58325409889221
无论如何,请使用for
,它将始终更快。