我应该使用哪种方法从巨大的响应中提取一个属性?

Which method should I use to extract the one attribute from a huge response?

我有一个 JSON 回复,如下所示。 print_r 结果的示例如下所示

(
[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
),
....
)

我只想从响应中提取姓名。

我应该在数组中使用循环并从数组中的每个对象中提取每个名称并将其推送到数组中还是应该使用以下代码?

$language = array_map(function($object)
{
    return $object->name; 
}, $jsonReponse); 

哪个是最好的选择,为什么?

根据我的研究,您应该使用 foreach() 来提取属性,

同时为每个处理数百万条记录的庞大数组比 array_map()

快得多
  • Foreach:0.7 秒
  • 映射函数名称:1.2 秒

有关更多信息,请关注 this link

我只想用一个简单的 foreach 循环来完成:

$nameArr = [];
$arr = json_decode($theObject);

foreach ($arr as $name) {
    array_push($nameArr, $name->name);
}

我使用了这个脚本,生成了一个有 500,000 个寄存器的 array/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;

输出为

Time with for loop: 2.0891849994659

Time with array_column: 7.5789909362793

Time with foreach: 6.3916020393372

Time with array_map: 7.6288249492645

因此,for 是最快的,foreach、array_column 和 array_map 方法要慢得多。但是,运行 100,000 个寄存器,差异最小:

Time with for loop: 0.40081810951233

Time with array_column: 0.40819096565247

Time with foreach: 0.44123411178589

Time with array_map: 0.58325409889221

无论如何,选择 for 总是会更快。