代码之家  ›  专栏  ›  技术社区  ›  Rafay Hassan

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

  •  -1
  • Rafay Hassan  · 技术社区  · 7 年前

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

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

    3 回复  |  直到 7 年前
        1
  •  1
  •   Tech Kid    7 年前

    虽然foreach处理大量的百万条记录要比array_map()快得多

    • 每小时:0.7秒

    有关更多信息,请参见 this link .

        2
  •  0
  •   prafferty    7 年前

    我只需要使用一个简单的foreach循环:

    $nameArr = [];
    $arr = json_decode($theObject);
    
    foreach ($arr as $name) {
        array_push($nameArr, $name->name);
    }
    
        3
  •  0
  •   Community Mohan Dere    6 年前

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

    输出是

    循环时间:6508842

    带数组_列的时间:7.5789909362793

    与foreach的时间:6.3916020393372

    因此,对于最快的方法,foreach、array\u column和array\u map方法的速度要慢得多。但是,使用100000个寄存器运行时,差异最小:

    用于循环的时间:0.40081810951233

    带数组_列的时间:0.40819096565247

    阵列映射时间:0.58325409889221

    不管怎样,跟我一起去吧 for