我正在使用Roots Sage 9,它在WordPress中使用Laravel,并将其与高级自定义字段相结合。
为此,我创建了一个控制器,在这里我使用array\\u映射遍历返回的数组,以提取所有日期。
根据页面的不同,我有时返回一个包含第二级数组的数组,而在其他情况下,我只返回一个包含单个级别的数组。
我遇到的问题是,在我只有一个单层数组的情况下,第二个array\\u map返回错误(array\\u map():参数#2应该是数组),因为不存在数组。
我以为三元数可以解决这个问题,但事实似乎并非如此
我的问题是,是否可以创建一个使用array\\u map实现这些目的的函数?
我的数据在运行时如下所示:
Array (
[0] => Array (
[identifier] => xyz
[description] => xyz
[bib_sources] => Array ( [0] =>
Array (
[type] => Book [year] => 1956
)
)
)
)
我的数据在不起作用时如下所示:
Array (
[0] => Array (
[identifier] => xyz
[description] => xyz
[bib_sources] =>
)
)
我的代码如下所示:
public function states()
{
//type
$type = get_field(\'c_p_c_p_s_e_type\', $post_id, false, false);
if($type == \'State & Edition\') {
return array_map(function($states) {
return [
\'identifier\' => $states[\'s_identifier\'] ?? null,
\'description\' => $states[\'s_description\'] ?? null,
\'impression_description\' => $states[\'s_impression_description\'] ?? null,
\'component_of_image\' => $states[\'s_textual_numerical_comp_of_image\'] ?? null,
\'inscriptions\' => $states[\'s_inscriptions\'] ?? null,
\'artists_comments\' => $states[\'s_artists_comments\'] ?? null,
\'authors_comments\' => $states[\'s_authors_comments\'] ?? null,
\'image\' => wp_get_attachment_image( $states[\'s_image\'], \'medium\' ) ?? null,
\'related_works\' => $states[\'s_related_works\'] ?? null,
\'final_state\' => $states[\'s_final_state\'] ?? null,
\'ashkas\' => $states[\'s_bib_source_c_bib_sources\'] ?? null,
\'bib_sources\' => array_map(function($bib_sources) {
return [
\'type\' => $bib_sources[\'c_bib_sources_type\'] ?? null,
\'year\' => $bib_sources[\'c_bib_sources_year\'] ?? null,
\'abb_ref\' => get_term_by( \'id\', $bib_sources[\'c_bib_sources_abbreviated_reference\']->term_id, \'bib_sources\') ?? null,
\'citation\' => $bib_sources[\'c_bib_sources_citation\'] ?? null,
];
}, $states[\'s_bib_source_c_bib_sources\'] ?? []),
];
}, get_field(\'c_states\') ?? []);
}
}
最合适的回答,由SO网友:Sally CJ 整理而成
返回时出错(array\\u map():参数#2应为数组),因为不存在数组
这个$states[\'s_bib_source_c_bib_sources\'] ?? []
是唯一设置$states[\'s_bib_source_c_bib_sources\']
如果尚未设置。如果它已设置且不是数组,则array_map
将引发“应为数组”错误。因此,您最好使用类型转换:
isset( $states[\'s_bib_source_c_bib_sources\'] ) ? // wrapped
(array) $states[\'s_bib_source_c_bib_sources\'] : []
编辑:如果$bib_sources[\'c_bib_sources_abbreviated_reference\']
是not 一WP_Term
实例这个get_term()
这里的部分也令人困惑,因为术语ID永远不会相同,而且无论分类法是什么,分类法的术语都保存在同一个数据库表中:
get_term_by( \'id\', $bib_sources[\'c_bib_sources_abbreviated_reference\']->term_id, \'bib_sources\') ?? null
整个代码可能被重写为:
$bib_sources[\'c_bib_sources_abbreviated_reference\'] ?? null
但是,如果要重新验证数组项
c_bib_sources_abbreviated_reference
是有效期限,则可以执行以下操作:
array_map( function( $bib_sources ) {
$abb_ref = isset( $bib_sources[\'c_bib_sources_abbreviated_reference\'] ) ?
get_term( $bib_sources[\'c_bib_sources_abbreviated_reference\'], \'bib_sources\' ) : null;
return [
\'type\' => $bib_sources[\'c_bib_sources_type\'] ?? null,
\'year\' => $bib_sources[\'c_bib_sources_year\'] ?? null,
\'abb_ref\' => is_wp_error( $abb_ref ) ? null : $abb_ref, // <- this
\'citation\' => $bib_sources[\'c_bib_sources_citation\'] ?? null,
];
}, isset( $states[\'s_bib_source_c_bib_sources\'] ) ? (array) $states[\'s_bib_source_c_bib_sources\'] : [] );