我試圖使用array_combine()
數組作爲關鍵($filenames)
兩個數組作爲合併對象數組($tags and $cfContents)
:爲什麼我不能array_combine一個數組與鍵與對象數組?
$filenames = array();
$tags = array();
$cfContents = array();
// For loop creates three arrays based on each of the set objects
foreach(new DirectoryIterator('./cf_templates/') as $cfFile)
{
if ($cfFile->isDot() || !$cfFile->isFile()) continue;
$filenames[] = $cfFile->getBasename('.txt');
$tags[] = array("<!-- " . $cfFile->getBasename('.txt') . " CF BEGIN -->",
"<!-- " . $cfFile->getBasename('.txt') . " CF END -->");
$cfContents[] = file_get_contents('./cf_templates/' . $cfFile. '.txt');
}
// $sets = array_combine($filenames, $tags) // This works.
$setContent = array_merge($tags, $cfContents);
$sets = array_combine($filenames, $setContent); // Errors on "Both parameters should have an equal number of elements"
print_r($sets);
當我運行這一點,但是,我一直在數組$組得到警告(見註釋)。我會想象$ setContent合併兩個數組就好了,但問題是$ sets? (請參閱http://php.net/manual/en/function.array-combine.php)
幫助 - 爲什麼array_combine()上的$集不起作用?
它不起作用,因爲'$ filenames'具有與'$ setContent'不同的元素數量。我不確定它有多簡潔。 –