简体   繁体   中英

Php combine variable and array value

I have an array. I want to generate dynamic variable with array values. When I run the code below, it doesn't work. I want to generate $urlsHome and $urlsOffice . How can I do?

$arr= ["Home", "Office"];

foreach ($arr as $key=>$type) {
    echo $urls.type;
}

You must use this fine { } brackets.

$urlsHome="this";
$urlsOffice="that";

$arr= ["Home", "Office"];

foreach ($arr as $key=>$type) {
    echo ${'urls'.$type}."<BR>";
}

results :

this
that

I belive that's what you are looking for.

$arr = ["Home", "Office"];

foreach ($arr as $key=>$type) {
    ${'url' . $type} = $type;
    echo $urlHome;
}

This is the simple way to create dynamic variable in PHP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM