[英]Check if something exists in array
I have a little problem coding if statement in wordpress. 我在wordpress中的if语句编码时遇到一些问题。 My plugin stores custom registration fields in one single row in database, lets say "custom_fields".
我的插件将自定义注册字段存储在数据库的一行中,假设为“ custom_fields”。 When I print custom_fields using get_user_meta I got an array of all information stored there, eg:
当我使用get_user_meta打印custom_fields时,我得到了存储在那里的所有信息的数组,例如:
Array ( [0] => Array ( [invoice_company_name] => x [invoice_nip] => x [invoice_street] => x [invoice_street_number] => x [invoice_post_code] => x [invoice_city] => x [invoice_country] => x [birth_date] => x [birth_city] => x [district] => x ) )
I want to check if all fields starting with invoice exists. 我想检查是否所有以发票开头的字段都存在。 Of course where are the 'x' there are real values.
当然,“ x”在哪里呢?
Well I found function in_array(), so tried to do something like this, but it doesn't work 好吧,我发现了函数in_array(),所以试图做这样的事情,但是它不起作用
$user_ID = get_current_user_id();
$all_meta_for_user = get_user_meta( $user_ID, 'wp_s2member_custom_fields', false );
print_r( $all_meta_for_user );
if (in_array("[invoice_company_name]", $all_meta_for_user)) {
echo "exist";} else {echo 'dont exist';}
And I got 'dont exist' :) What's going wrong? 我得到了“不存在” :)怎么了? Also, can I check all the values at once?
另外,我可以一次检查所有值吗? Something like in_array([1st]&&[2nd]&&[3rd])?
像in_array([1st] && [2nd] && [3rd])之类的东西?
Thanks! 谢谢!
尝试像
if (in_array("invoice_company_name", $all_meta_for_user[0])) {
If your array is multi-dimensional, you could search by key using array_key_exists() : 如果数组是多维的,则可以使用array_key_exists()通过键进行搜索:
foreach($all_meta_for_user as $key=>$val){
if (array_key_exists('invoice_company_name', $val))
{
echo "exist in key ".$key;
}
else
{
echo "does not exist in key ".$key;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.