我相信这对你有好处.任何帮助将不胜感激.
我想将一个数组传递给包含参数的PHP函数,所有参数都是可选的.我正在使用代码点火器,绝不是专家.以下是我到目前为止使用的内容:
function addLinkPost($postDetailArray) { if (isset($postDetailArray['title'])) { $title = $postDetailArray['title']; } else { $title = "Error: No Title"; } if (isset($postDetailArray['url'])) { $url = $postDetailArray['url']; } else { $url = "no url"; } if (isset($postDetailArray['caption'])) { $caption = $postDetailArray['caption']; } else { $caption = ""; } if (isset($postDetailArray['publish'])) { $publish = $postDetailArray['publish']; } else { $publish = TRUE; } if (isset($postDetailArray['postdate'])) { $postdate = $postDetailArray['postdate']; } else { $postdate = "NOW()"; } if (isset($postDetailArray['tagString'])) { $tagString = $postDetailArray['tagString']; } else { $tagString = ""; }
你可以这样做:
function addLinkPost(array $postDetailArray) { $fields = array( 'key' => 'default value','title' => 'Error: No Title',); foreach ($fields as $key => $default) { $$key = isset($postDetailArray[$key]) ? $postDetailArray[$key] : $default; } }
只需使用您的密钥及其默认值编辑$fields数组.