Ranjith M
Ranjith M

Reputation: 529

Want to Filter an array according to value

I have a variable $a='san-serif' and an array Font_list[] now I want only the arrays whose category is 'san-serif' will be filtered. I tried a lot of codes nothing seems working here is my code:-

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

}

Please help me :-(

Upvotes: 1

Views: 62

Answers (1)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

What i understood according to that you want something like below:-

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

Output:- https://eval.in/597605

Upvotes: 1

Related Questions