【发布时间】:2023-01-13 22:57:23
【问题描述】:
我有一系列链接:
Array (
[link] => Array (
[title] => FY 2020/21
[url] => http://local.abc.com/app/uploads/2022/01/A.pdf
[target] =>
)
[show_icon] =>
)
我必须检查 [show_icon] 是否有值并向 [link] 附加一行,称为类。
我希望它看起来像这样:
[link] => Array (
[title] => FY 2020/21
[url] => http://local.abc.com/app/uploads/2022/01/A.pdf
[target] =>
[class] => 'A string of classes'
)
[show_icon] =>
)
我尝试运行大量不同的方法来追加,例如 array_push、array_merge、交换到 stdObject ...
这是我的代码:
$class= ['class' => 'btn-apply type2'];
if ($link['show_icon']) {
$class = ['class' => 'btn-apply type2 show-icon'];
}
if (is_array($link['link'])) :
array_push($link['link'], $class);
endif;
输出为:
Array (
[link] => Array (
[title] => FY 2020/21
[url] => http://local.abc.com/app/uploads/2022/01/A.pdf
[target] =>
[0] => Array (
[class] => btn-apply type2
)
)
[show_icon] =>
)
如何在没有 [0] => Array ( 环绕 [class] 的情况下添加到数组?
【问题讨论】: