想要根据值过滤数组

时间:2016-06-29 11:10:44

标签: php arrays

我有一个变量$a='san-serif'和一个数组Font_list[]现在我只想要类别为' san-serif'的数组。将被过滤。我尝试了很多代码似乎没有什么工作在这里是我的代码: -

public function filterFont() {

    $a = $_POST['key'];
    $url = "https://www.googleapis.com/webfonts/v1/webfonts?key=''";
    $result = json_decode(file_get_contents( $url ));
    $font_list = "";
    foreach ( $result->items as $font )
    {
        $font_list[] = [
            'font_name' => $font->family,
            'category' => $font->category,
            'variants' => implode(', ', $font->variants),
            // subsets
            // version
            // files
        ];
    }
    $filter = filter($font_list);

    print_r(array_filter($font_list, $filter));

}

请帮帮我: - (

1 个答案:

答案 0 :(得分:1)

根据你想要的东西,我理解的是: -

<?php
$a='san-serif'; // category you want to search
$font_list=Array('0'=>Array('font_name' => "sans-sherif",'category' => "san-serif"),'1'=>Array('font_name' => "times-new-roman",'category' => "san-serif"),'2'=>Array('font_name' => "sans-sherif",'category' => "roman")); 
// your original array seems something like above i mentioned
echo "<pre/>";print_r($font_list); // print original array
$filtered_data = array(); // create new array
foreach($font_list as $key=>$value){ // iterate through original array

  if($value['category'] == $a){  // if array category name is equal to serach category name
   $filtered_data[$key] = $value; // assign that array to newly created array
  }
}
echo "<pre/>";print_r($filtered_data); // print out new array

输出: - https://eval.in/597605