1

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 :-(

6
  • $key = $_POST['key']; and you're using a GET method ?key. That POST should be GET or REQUEST. Check for errors also. That could be part of the problem. Commented Jun 29, 2016 at 11:17
  • Post your array is it. Is a category is unique or it has multiple value for same category?? Commented Jun 29, 2016 at 11:17
  • Plus, how is $a being used? and where is the font array? Commented Jun 29, 2016 at 11:21
  • Sorry for the confuse Actually $key should be $a, Sorry for the confusion@Fred-ii- Commented Jun 29, 2016 at 11:23
  • 1
    Thank you That worked@Anant Commented Jun 29, 2016 at 11:31

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.