Remove all array elements except what I want?

后端 未结 8 2019
挽巷
挽巷 2021-01-31 14:13

I have controller that takes post parameters from HTML form, it will then send them to model that will insert the array into Cassandra database.

It is SQLInjection proof

相关标签:
8条回答
  • 2021-01-31 14:59

    You are looking for array_intersect:

    $good = ['parent_id', 'type', 'title', 'body', 'tags'];
    $post = ['parent_id', 'type', 'title', 'body', 'tags', 'one', 'two', 'three'];
    
    print_r(array_intersect($good, $post));
    

    See it in action.

    Of course this specific example does not make much sense because it works on array values, but there is also array_intersect_key that does the same based on keys.

    0 讨论(0)
  • 2021-01-31 14:59

    This will output the same as $post_allowed. What it does is only allow the values in $post_input that are also present in $post_allow.

    $post_allowed = ['parent_id', 'type', 'title', 'body', 'tags'];
    $post_input   = ['parent_id', 'type', 'title', 'body', 'tags', 'one', 'two', 'three'];
    $post = array_intersect($post_input, $post_allowed);
    
    0 讨论(0)
提交回复
热议问题