Remove all array elements except what I want?

后端 未结 8 2022
挽巷
挽巷 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:36

    Its worth remembering that while array_intersect and array_intersect_key are good they might well be overkill. In my situation I only wanted 1 element left, therefore the simplest option was just to rebuild the array I wanted based on the key/values I needed. I wonder at what point therefore the array_intersect's don't become worth it and you are simply better off with $new = array('whatI'=>'want');. I believe in the OP this is worth it but in smaller cases it might be overkill.

    Alternatively in response to the original question simply using unset might have been a cheaper option - unset($post['one'],$post['two'],$post['three']). Again though, it relates to the point at which this becomes too inefficient and the array_intersect functions are better.

提交回复
热议问题