Hello,
Since you cannot redeclare a function in PHP, how would I go about to change the hyphens to underscores in my loop?
The above will result in the following error:
Fatal error: Cannot redeclare replaceDashes() (previously declared
Since you cannot redeclare a function in PHP, how would I go about to change the hyphens to underscores in my loop?
PHP:
foreach ($websites as $website) {
$website_name = $website['name']; //The-New-York-Times
function replaceDashes (&$website_name) {
$vars = get_object_vars($website_name);
foreach ($vars as $key => $val) {
if (strpos($key, "-") !== false) {
$newKey = str_replace("-", "_", $key);
$website_name->{$newKey} = $val;
unset($website_name->{$key});
}
}
}
$url = "http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info&exintro&titles=";
$url .= $website_name;
$url .= "&format=json&explaintext&redirects&inprop=url&indexpageids";
$json = file_get_contents($url);
$data = json_decode($json);
$pageid = $data->query->pageids[0];
echo $data->query->pages->$pageid->extract;
}
The above will result in the following error:
Fatal error: Cannot redeclare replaceDashes() (previously declared
what he said