从数组php中只提取所需的数据

时间:2017-09-19 13:18:23

标签: php arrays

我正在使用API​​并且它返回数组的大数据,但我只需要来自该数组的某些项目并取消其他项目,如何做到这一点?

这是我的数组格式

我只需要DIGS,EXP和GI,并且从阵列中取消设置其他项目,如何从大型数组中完成?有没有快速的方法来做到这一点?

以下是我从API获得的数组

https://www.cryptocompare.com/api/data/coinlist

  Array
(
    [DIGS] => Array
        (
            [Id] => 12192
            [Url] => /coins/digs/overview
            [ImageUrl] => /media/20706/digs.png
            [Name] => DIGS
            [CoinName] => Diggits
            [FullName] => Diggits (DIGS)
            [Algorithm] => Scrypt
            [ProofType] => PoS
            [FullyPremined] => 1
            [TotalCoinSupply] => 100000000
            [PreMinedValue] => N/A
            [TotalCoinsFreeFloat] => N/A
            [SortOrder] => 464
        )

    [EXP] => Array
        (
            [Id] => 12194
            [Url] => /coins/exp/overview
            [ImageUrl] => /media/20707/exp.png
            [Name] => EXP
            [CoinName] => Expanse
            [FullName] => Expanse (EXP)
            [Algorithm] => Ethash
            [ProofType] => PoW
            [FullyPremined] => 0
            [TotalCoinSupply] => N/A
            [PreMinedValue] => N/A
            [TotalCoinsFreeFloat] => N/A
            [SortOrder] => 465
        )

    [GCR] => Array
        (
            [Id] => 12217
            [Url] => /coins/gcr/overview
            [ImageUrl] => /media/20708/gcr.png
            [Name] => GCR
            [CoinName] => Global Currency Reserve
            [FullName] => Global Currency Reserve (GCR)
            [Algorithm] => N/A
            [ProofType] => PoS
            [FullyPremined] => 0
            [TotalCoinSupply] => 0
            [PreMinedValue] => N/A
            [TotalCoinsFreeFloat] => N/A
            [SortOrder] => 466
        )

    [MAPC] => Array
        (
            [Id] => 12223
            [Url] => /coins/mapc/overview
            [ImageUrl] => /media/20710/mapc.png
            [Name] => MAPC
            [CoinName] => MapCoin
            [FullName] => MapCoin (MAPC)
            [Algorithm] => X11
            [ProofType] => PoS
            [FullyPremined] => 0
            [TotalCoinSupply] => 2228921.184
            [PreMinedValue] => N/A
            [TotalCoinsFreeFloat] => N/A
            [SortOrder] => 467
        )

    [MI] => Array
        (
            [Id] => 12225
            [Url] => /coins/mi/overview
            [ImageUrl] => /media/20711/mi.png
            [Name] => MI
            [CoinName] => XiaoMiCoin
            [FullName] => XiaoMiCoin (MI)
            [Algorithm] => Scrypt
            [ProofType] => PoW/PoS
            [FullyPremined] => 0
            [TotalCoinSupply] => 400000000
            [PreMinedValue] => N/A
            [TotalCoinsFreeFloat] => N/A
            [SortOrder] => 468
        )

    [CON] => Array
        (
            [Id] => 12985
            [Url] => /coins/con_/overview
            [ImageUrl] => /media/20717/con_.png
            [Name] => CON_
            [CoinName] => Paycon
            [FullName] => Paycon (CON_)
            [Algorithm] => X13
            [ProofType] => PoW/PoS
            [FullyPremined] => 0
            [TotalCoinSupply] => 50000000
            [PreMinedValue] => N/A
            [TotalCoinsFreeFloat] => N/A
            [SortOrder] => 469
        )
)

Tried Code below:

 $data_url = 'https://www.cryptocompare.com/api/data/coinlist/';
  $data_json = cp_get_url_data_curl($data_url);
  $data_all_currencies_raw = json_decode($data_json, true);
  $data_all_currencies = $data_all_currencies_raw['Data'];


$search = ["SUB","USC"];



array_walk($data_all_currencies, function(&$item, $key) use($search){
echo "<pre>";
print_r($item); 
  if(!in_array($key, $search)){

     unset($item);
  }
});
echo "<pre>";
print_r($data_all_currencies);  
exit;

2 个答案:

答案 0 :(得分:1)

如果您只想要MAPCMI&amp; CON个对象,您可以在Object['Data']中遍历主对象,同时拥有所需对象的if clauses

E.g(将使用C#以便您知道该怎么做)

我假设您正在检索某种JSON或XML文件,如果是这样,如果您正确反序列化它,您可以将其用作字典。 有点像:

foreach( var subObjectData in Object['Data'] ){
     if(subObjectData['MAPC']){
          //Do something
     }else if(subObjectData['MI']){
          //Do something else
     }else{
          //Destroy unneeded objects
     }

     //etc
}

答案 1 :(得分:0)

我想说,你应该将你的对象转换为数组。然后在其上应用以下代码以仅获取所需数据。

$array = json_decode(json_encode($array), true);
$search = ["MPC","CON","GI"];
array_walk($array['data'], function(&$item, $key) use($search){
  if(!in_array($key, $search)){
     unset($item);
  }
});