我目前在layout.php中有这个数组
<?php
$arrLayout = array(
"section1" => array(
"wControl" => array(
"title" => "Control",
"display" => ""
),
"wRecentTV" => array(
"title" => "Recent TV",
"display" => ""
)
),
"section2" => array(
"wXBMCLibrary" => array(
"title" => "XBMC Library",
"display" => ""
)
),
"section3" => array(
"wSearch" => array(
"title" => "Search",
"display" => ""
),
"wRSS" => array(
"title" => "RSS Feed",
"display" => ""
)
),
);
?>
我想要的是这样说。如果($ wControl ==“true”)和'wControl'不在任何部分,那么写
,
"wControl" => array(
"title" => "Control",
"display" => ""
)
到)和)之间的x部分的末尾,并重写文件。
基本上wControl可能存在也可能不存在于任何一个部分中。因此,如果它不存在,则必须将其添加到数组中,并且必须重写layout.php以显示它。希望一切都有意义。
从我可以收集到的内容中,我可以检查它是否在数组中,然后用
推送它if (!in_array( wControl, $arrLayout ))
array_push(..)
我如何正确编码?
由于
答案 0 :(得分:0)
<?php
/**
* @param array $sections
* @param string $name name of new key (like wControl)
* @param mixed $value value of new key
* @param string $into_section if empty, last section will be used
* @return bool
*/
function addUnique(array &$sections, $name, $value, $into_section = '')
{
$last_section = '';
foreach ($sections as $section_name => $section)
{
if (key_exists($name, $section)) return false;
$last_section = $section_name;
}
if (empty($into_section)) $into_section = $last_section;
$sections[$into_section][$name] = $value;
}
$arrLayout = array(
"section1" => array(
"wControl" => array(
"title" => "Control",
"display" => ""
),
"wRecentTV" => array(
"title" => "Recent TV",
"display" => ""
)
),
"section2" => array(
"wXBMCLibrary" => array(
"title" => "XBMC Library",
"display" => ""
)
),
"section3" => array(
"wSearch" => array(
"title" => "Search",
"display" => ""
),
"wRSS" => array(
"title" => "RSS Feed",
"display" => ""
)
),
);
$wControl = true;
if ($wControl)
{
addUnique($arrLayout, 'wControlNew', array(
"title" => "Control",
"display" => ""
),
'section2');
}
var_dump($arrLayout);