我难倒 - 严格的标准:只有变量应当参照在
问题描述:
我目前正在学习PHP的传递,这难倒我:(我难倒 - 严格的标准:只有变量应当参照在
我会很感激,如果有人可以解释这意味着什么,可能解决方案
的爱所有
// Get post category info
$category = get_the_category();
if(!empty($category)) {
// Get last category post is in
$last_category = end(array_values($category));
// Get parent any categories and create array
$get_cat_parents = rtrim(get_category_parents($last_category->term_id, true, ','),',');
$cat_parents = explode(',',$get_cat_parents);
// Loop through parent categories and store in variable $cat_display
$cat_display = '';
foreach($cat_parents as $parents) {
$cat_display .= '<li>'.$parents.'</li>';
}
}
答
$last_category = end(array_values($category));
将引发错误,因为end
接受参考:!
$vals = array_values($category);
$last_category = end($vals);
但我不认为你需要数组的array_values
。它会将关联数组转换为索引数组。
+0
感谢Justinas抽出时间回应,我将着眼于最后一段。非常感谢 :) –
你的问题是什么? – Justinas
'end()'函数是通过引用传递的,并且需要一个实参变量作为参数传递,而不是表达式的结果:更改'$ last_category = end(array_values($ category));'to'$ categoryValues = array_values($ category); $ last_category = end($ categoryValues);' –
谢谢你马克:)这是有道理的,你的解决方案的工作原理。我真的很感谢