我为wordpress博客创建了一个自定义类别小部件,但需要一些帮助,因为有些选项很少搞乱。
更新函数创建一个数组,但不是我需要的方式。以下是此类数组的示例:
Array
(
[title] => Blog Categories
[cat_visibility] => Array
(
[0] => 7
)
[categories] => Array
(
[0] => 1
[1] => 9
)
[URL_term_id] => Array
(
[0] => 1
[1] => 7
[2] => 8
[3] => 9
[4] => 10
)
[URL_title] => Array
(
[0] => Category 1
[1] => Category 2
[2] => Category 3
[3] => Category 4
[4] => Category 5
)
)
这是我的PHP代码:
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance[ 'title' ] = strip_tags( $new_instance[ 'title' ] );
$instance[ 'cat_visibility' ] = $new_instance[ 'cat_visibility' ];
$instance[ 'categories' ] = $new_instance['categories'];
$instance[ 'URL_term_id' ] = $new_instance[ 'URL_term_id' ];
$instance[ 'URL_title' ] = $new_instance['URL_title'];
return $instance;
}
最后,这是我想要实现的目标:
Array
(
[0] => Array
(
[category_id] => 1
[term_id] => 1
[title] => Category 1
[cat_visibility] =>
)
[1] => Array
(
[category_id] =>
[term_id] => 7
[title] => Category 2
[cat_visibility] => 7
)
[2] => Array
(
[category_id] =>
[term_id] => 8
[title] => Category 3
[cat_visibility] =>
)
[3] => Array
(
[category_id] => 9
[term_id] => 9
[title] => Category 4
[cat_visibility] =>
)
[4] => Array
(
[category_id] =>
[term_id] => 10
[title] => Category 5
[cat_visibility] =>
)
)
到目前为止,我尝试使用以下代码转换初始数组:
$new_object = [];
for($i=0;$i<count($URL_term_id);$i++){
array_push($new_object,
array(
"category_id"=>$cat_id[$i],
"term_id"=>$URL_term_id[$i],
"title"=>$URL_title_id[$i],
"cat_visibility"=>$cat_visibility[$i]
)
);
}
它几乎创造了我所需要的东西,但由于键的不同而没有创建我需要的数组。
以下是我从中获得的信息:
Array
(
[0] => Array
(
[category_id] => 1
[term_id] => 1
[title] => Category 1
[cat_visibility] => 7
)
[1] => Array
(
[category_id] => 9
[term_id] => 7
[title] => Category 2
[cat_visibility] =>
)
[2] => Array
(
[category_id] =>
[term_id] => 8
[title] => Category 3
[cat_visibility] =>
)
[3] => Array
(
[category_id] =>
[term_id] => 9
[title] => Category 4
[cat_visibility] =>
)
[4] => Array
(
[category_id] =>
[term_id] => 10
[title] => Category 5
[cat_visibility] =>
)
)
感谢任何建议和帮助。