php 查询数据库关联数组变量

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 查询数据库关联数组变量相关的知识,希望对你有一定的参考价值。

 $itembrand_with_dups = [];
    if($data['search']['total']['natural'] > 0) {
      foreach($data['search']['natural']['results'] as $key=>$search_result) {
        if(isset($search_result['attributes']['brand']) && !empty($search_result['attributes']['brand'])) {
          $brand_url_path = parse_url($search_result['attributes']['brand_url_url'] , PHP_URL_PATH);
          $total_items_for_brand = 0;
          if(strlen($brand_url_path) > 0) {  
            $item_count_for_brand_url = DB::select("
                SELECT count(item_id) as total_items_for_brand FROM longtail_client_36_taxonomy 
                WHERE category_id = (SELECT category_id FROM longtail_client_36_categories 
                WHERE category_url = '".$brand_url_path."')");
            
            if($item_count_for_brand_url) {
                $total_items_for_brand = $item_count_for_brand_url[0]->total_items_for_brand;
            }
          }
          $itembrand_with_dups[] = array(
              'brand' => ucwords($search_result['attributes']['brand']),
              'slug' => basename($search_result['attributes']['brand_url_url']), // keep lart part of url
              'total_items'=> $total_items_for_brand
          );
        }
      }
    }
    if($data['search']['total']['expanded']> 0) {
      foreach($data['search']['expanded']['results'] as $key=>$search_result) {
        if(isset($search_result['attributes']['brand']) && !empty($search_result['attributes']['brand'])) {
            $brand_url_path = parse_url($search_result['attributes']['brand_url_url'] , PHP_URL_PATH);
            $total_items_for_brand = 0;
            if(strlen($brand_url_path) > 0) {  
                $item_count_for_brand_url = DB::select("
                    SELECT count(item_id) as total_items_for_brand FROM longtail_client_36_taxonomy 
                    WHERE category_id = (SELECT category_id FROM longtail_client_36_categories 
                    WHERE category_url = '".$brand_url_path."')");
                
                if($item_count_for_brand_url) {
                    $total_items_for_brand = $item_count_for_brand_url[0]->total_items_for_brand;
                }
            }
            $itembrand_with_dups[] = array(
                'brand' => ucwords($search_result['attributes']['brand']),
                'slug' => basename($search_result['attributes']['brand_url_url']), // keep lart part of url
                'total_items'=> $total_items_for_brand
            );
        }
      }
    }
    // dedup brands/urls - array_unique handles associative arrays with the SORT_REGULAR option
    
    $itembrand=array_unique($itembrand_with_dups, SORT_REGULAR);
    // sort brands/urls
    usort($itembrand, function ($a, $b) { return strcmp($a['brand'], $b['brand']); });
    $data['itembrand'] = $itembrand;
    }    
        return $data;
    }
}

以上是关于php 查询数据库关联数组变量的主要内容,如果未能解决你的问题,请参考以下文章

Cake PHP 连接查询以关联数组形式返回

PHP cURL 表单数据:多个变量,同名,差异。价值观

比较 php MySQL 查询结果数组以从其中一个数组中获取关联值

PHP数组

将关联数组做成类似于 Eloquent 查询集合的集合

sql 读取数组问题