PHP搜索多维数组和返回键-超大可变数组

PHP搜索多维数组和返回键-超大可变数组,php,arrays,dynamic,multidimensional-array,search,Php,Arrays,Dynamic,Multidimensional Array,Search,大家好,我现在正在使用一个API,它可以从MMO中获取价格,并将其显示在一个数组中。 目前,这是我的代码: <?php ini_set('max_execution_time', 0); $data = json_decode(file_get_contents("http://www.gw2spidy.com/api/v0.9/json/all-items/all"), true); foreach($data as $item) { echo '<pre>

大家好,我现在正在使用一个API,它可以从MMO中获取价格,并将其显示在一个数组中。 目前,这是我的代码:

    <?php 
ini_set('max_execution_time', 0);
$data = json_decode(file_get_contents("http://www.gw2spidy.com/api/v0.9/json/all-items/all"), true);
foreach($data as $item) {
    echo '<pre>';
    print_r($data);
    echo '</pre>';
}
?>

如果有人能给我指出正确的方向,那就太棒了!我已经找了好几天了,但是找不出答案。。。我敢打赌这也很简单:(

首先,最好知道是否有办法从GW2 API服务器检索自上次“全部获取”以来更新的项目。这意味着您只需检索整个数组一次,然后更新每次更新的少数记录

  • 可以使用类似于array\u chunk的方法拆分数组
  • 使用这些拆分数组为数据库(如MySQL)生成批插入语句
  • 使用简单的SELECT查询检索语句
  • 如果你想在没有DB的情况下完成它,你只需要使用for循环搜索该项

    ini_set('max_execution_time', 0);
    $data = json_decode(file_get_contents("http://www.gw2spidy.com/api/v0.9/json/all-items/all"), true);
    $new_arrry = array();
    $i = 0;
    foreach($data as $item) {
        $new_arry[$i] = $item['name'];
        $new_arry[$i] = $item['data_id'];
        $i++;
    }
    
    for($i=0;$i<$array['count'];$i++)
    {
    if($array['results'][$i]['name']=='dash')
    //你找到了物品
    }
    
    试试这个

    在这里,您可以创建一个新数组并访问它

    function search($array, $key, $value)
    {
        $results = array();
    
        if (is_array($array))
        {
            if (isset($array[$key]) && $array[$key] == $value)
                $results[] = $array;
    
            foreach ($array as $subarray)
                $results = array_merge($results, search($subarray, $key, $value));
            }
    
        return $results;
    }
    

    尝试递归数组搜索。下面是一个使用php.net页面上发布的用于数组搜索的递归函数的示例。请注意,该函数实际上没有使用数组搜索php函数,但提供了支持多维数组的类似功能

    首先是功能:

    $data = json_decode(file_get_contents("http://www.gw2spidy.com/api/v0.9/json/all-items/all"), true);
    $results = $data['results'];
    $foundItems = search($results, 'name', 'Dusk');
    print_r($foundItems);
    
    $foundItems = search($results, 'data_id', '12345');
    
    现在使用以下功能:

    $data = json_decode(file_get_contents("http://www.gw2spidy.com/api/v0.9/json/all-items/all"), true);
    $results = $data['results'];
    $foundItems = search($results, 'name', 'Dusk');
    print_r($foundItems);
    
    $foundItems = search($results, 'data_id', '12345');
    
    这将为您提供结果中与名称匹配的所有项目的数组。类似地,对id的搜索将是:

    $url = "http://www.gw2spidy.com/api/v0.9/json/all-items/all";
    var_dump(get_headers($url));
    

    首选解决方案

    使用类似MySQL的数据库存储所有请求,并且仅在JSON被修改时更新数据库。为什么

    array (size=13)
      0 => string 'HTTP/1.1 200 OK' (length=15)
      1 => string 'Server: cloudflare-nginx' (length=24)
      2 => string 'Date: Mon, 03 Dec 2012 11:52:41 GMT' (length=35) <--- See Date
      3 => string 'Content-Type: application/json' (length=30)
      4 => string 'Content-Length: 8621869' (length=23)
      5 => string 'Connection: close' (length=17)
      6 => string 'cache-control: no-cache' (length=23)
      7 => string 'X-Varnish-TTL: 300.000' (length=22)
      8 => string 'Accept-Ranges: bytes' (length=20)
      9 => string 'Age: 135' (length=8)
      10 => string 'X-Varnish-Cache: HIT' (length=20)
      11 => string 'Set-Cookie: __cfduid=d249da7538d37614f7bc206fd407142eb1354535561; expires=Mon, 23-Dec-2019 23:50:00 GMT; path=/; domain=.gw2spidy.com' (length=133)
      12 => string 'CF-RAY: 286a7d677b004f85' (length=24)
    
    输出

    $url = "http://www.gw2spidy.com/api/v0.9/json/all-items/all";
    $info = get_headers($url,true);
    
    $lastDate = new DateTime();
    $lastDate->setDate(2012, 12, 03); 
    
    $dateModified = DateTime::createFromFormat("D, d M Y g:i:s \G\M\T", $info['Date']);
    if($dateModified > $lastDate)
    {
    
        //Parse Jason
        //Update Database etc 
    }
    
    
    // Select * from result where name = 'WHAT YOU WANT' 
    
    *教育目的*

    总时间


    除非我看不清楚您的需求,否则为什么不在API端过滤结果呢?例如(请参见)
    foreach($data as$item){print\r($data);}
    …这是打字错误吗?
    $filter = new \ProductIterator("http://www.gw2spidy.com/api/v0.9/json/all-items/all");
    $filter->find("name", "dusk"); //<---- What you want to find 
    
    echo "<pre>";
    foreach ( $filter as $value ) {
        var_dump($value);
    }
    
    Dusky Dye - 20428
    Dusk Dye - 20564
    Dusk - 29185
    
    1.4957299232483  <------------------ Total Time 
    
    class ProductIterator extends FilterIterator {
        private $key;
        private $value;
    
        public function __construct($url) {
            $data = json_decode(file_get_contents($url), true);
            parent::__construct(new ArrayIterator($data['results']));
            unset($data);
        }
    
        public function find($key, $value) {
            $this->key = $key;
            $this->value = $value;
        }
    
        public function accept() {
            $user = $this->getInnerIterator()->current();
            if (isset($user[$this->key])) {
                return stripos($user[$this->key], $this->value) !== false;
            }
            return false;
        }
    }