将相似的字符串值组合成唯一的
Combine Similar Strings Value into Unique One
我正在从 Wordpress CPT 中获取一个 post 元数据,它会单独返回多个字符串形式而不是数组,如下所示:
$data = $calendar->data();
foreach ($data as $event) {
$days = get_post_meta( $event->ID, 'sp_day', true );
var_dump($days);
}
string(1) "1" string(1) "1" string(1) "1" string(1) "1" string(1) "1"
string(1) "1" string(1) "2" string(1) "2" string(1) "2" string(1) "2"
string(1) "2" string(1) "2" string(1) "3" string(1) "3" string(1) "3"
string(1) "3" string(1) "3" string(1) "3" string(1) "4" string(1) "4"
string(1) "4" string(1) "4" string(1) "4" string(1) "4" string(0) ""
string(0) "" string(0) "" string(0) "" string(0) "" string(0) ""
我尝试将其转换成数组,如 $days = array(get_post_meta( $event->ID, 'sp_day', true ));
转储值是:
array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" }
array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" }
array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" }
array(1) { [0]=> string(1) "2" } array(1) { [0]=> string(1) "2" }
array(1) { [0]=> string(1) "2" } array(1) { [0]=> string(1) "2" }
array(1) { [0]=> string(1) "2" } array(1) { [0]=> string(1) "2" }
array(1) { [0]=> string(1) "3" } array(1) { [0]=> string(1) "3" }
array(1) { [0]=> string(1) "3" } array(1) { [0]=> string(1) "3" }
array(1) { [0]=> string(1) "3" } array(1) { [0]=> string(1) "3" }
array(1) { [0]=> string(1) "4" } array(1) { [0]=> string(1) "4" }
array(1) { [0]=> string(1) "4" } array(1) { [0]=> string(1) "4" }
array(1) { [0]=> string(1) "4" } array(1) { [0]=> string(1) "4" }
array(1) { [0]=> string(0) "" } array(1) { [0]=> string(0) "" }
array(1) { [0]=> string(0) "" } array(1) { [0]=> string(0) "" }
array(1) { [0]=> string(0) "" } array(1) { [0]=> string(0) "" }
比我尝试了 array_merge()
和 array_unique()
None 以上步骤已解决问题。如您所见,我们多次重复值,而不是我希望它们是唯一的,以便每个值仅获得一次,例如“1、2、3、4、5 等”
非常感谢您的建议
你可以在 for 循环之前添加一个空数组,在 for 循环内部你可以检查数组中是否存在该项目,如果不存在则将项目添加到数组中。
$data = $calendar->data();
$result = [];
foreach ($data as $event) {
$days = get_post_meta( $event->ID, 'sp_day', true );
if(! in_array($days, $result)) {
$result[] = $days;
}
}
var_dump($result);
我正在从 Wordpress CPT 中获取一个 post 元数据,它会单独返回多个字符串形式而不是数组,如下所示:
$data = $calendar->data();
foreach ($data as $event) {
$days = get_post_meta( $event->ID, 'sp_day', true );
var_dump($days);
}
string(1) "1" string(1) "1" string(1) "1" string(1) "1" string(1) "1" string(1) "1" string(1) "2" string(1) "2" string(1) "2" string(1) "2" string(1) "2" string(1) "2" string(1) "3" string(1) "3" string(1) "3" string(1) "3" string(1) "3" string(1) "3" string(1) "4" string(1) "4" string(1) "4" string(1) "4" string(1) "4" string(1) "4" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) ""
我尝试将其转换成数组,如 $days = array(get_post_meta( $event->ID, 'sp_day', true ));
转储值是:
array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "2" } array(1) { [0]=> string(1) "2" } array(1) { [0]=> string(1) "2" } array(1) { [0]=> string(1) "2" } array(1) { [0]=> string(1) "2" } array(1) { [0]=> string(1) "2" } array(1) { [0]=> string(1) "3" } array(1) { [0]=> string(1) "3" } array(1) { [0]=> string(1) "3" } array(1) { [0]=> string(1) "3" } array(1) { [0]=> string(1) "3" } array(1) { [0]=> string(1) "3" } array(1) { [0]=> string(1) "4" } array(1) { [0]=> string(1) "4" } array(1) { [0]=> string(1) "4" } array(1) { [0]=> string(1) "4" } array(1) { [0]=> string(1) "4" } array(1) { [0]=> string(1) "4" } array(1) { [0]=> string(0) "" } array(1) { [0]=> string(0) "" } array(1) { [0]=> string(0) "" } array(1) { [0]=> string(0) "" } array(1) { [0]=> string(0) "" } array(1) { [0]=> string(0) "" }
比我尝试了 array_merge()
和 array_unique()
None 以上步骤已解决问题。如您所见,我们多次重复值,而不是我希望它们是唯一的,以便每个值仅获得一次,例如“1、2、3、4、5 等”
非常感谢您的建议
你可以在 for 循环之前添加一个空数组,在 for 循环内部你可以检查数组中是否存在该项目,如果不存在则将项目添加到数组中。
$data = $calendar->data();
$result = [];
foreach ($data as $event) {
$days = get_post_meta( $event->ID, 'sp_day', true );
if(! in_array($days, $result)) {
$result[] = $days;
}
}
var_dump($result);