简体   繁体   中英

Search for PHP function which read array values

I try to create instead of the if conditions a function which read all values and positions of an array and call then a function. I check the documentation but cant find a solution. Is there any php function which could I take here?

function one() {
    echo '#btn1 {';
        echo 'animation-name:example;';
        echo 'animation-duration:1s;';
        echo 'animation-delay:0.5s;';
    echo '}';       
}

function two() {
    echo '#btn2 {';
        echo 'animation-name:example;';
        echo 'animation-duration:1s;';
        echo 'animation-delay:0.5s;';
    echo '}';       
}

$code = 12;
$arr1 = str_split($code);

if ($arr1[0] == 1) {
    one();
}
if ($arr1[0] == 2) {
    two();
}
if ($arr1[1] == 1) {
    one();
}
if ($arr1[1] == 2) {
    two();
}
if ($arr1[2] == 1)....
// Continues like this for about 36 times

Something like this?

function one() {
    echo '#btn1 {';

        echo 'animation-name:example;';
        echo 'animation-duration:1s;';
        echo 'animation-delay:0.5s;';
    echo '}';       
}
function two() {
    echo '#btn2 {';

        echo 'animation-name:example;';
        echo 'animation-duration:1s;';
        echo 'animation-delay:0.5s;';
    echo '}';       
}

$code = 12;
$arr1 = str_split($code);

foreach ($arr1 as $value) {
    switch($value) {
        case 1:
            one();
        break;
        case 2:
            two();
        break;
    }
}

Everything made a bit more compact

$code = 12;
$arr1 = str_split($code);
$css = '';
foreach ($arr1 as $value) {
    $css .= '#btn' . $value . ' {';
    $css .= '    animation-name:example;';
    $css .= '    animation-duration:1s;';
    $css .= '    animation-delay:0.5s;';
    $css .= '}'; 
}

echo $css; // output variable

Is this what you are looking for?

$map = array(1 => 'one', 2 => 'two', 3 => 'tri', 4 => 'four');

function one(){
    echo 'I am inside function one()';
}

function two(){
    echo 'I am inside function two()';
}

function tri(){
    echo 'I am inside function tri()';
}

$str = '1324';
for($i = 0; $i<strlen($str); $i++){
    echo 'Number: ' . $str[$i] . '<br>';
    echo 'Function: ' . $map[$str[$i]] . '<br>';
    echo 'CALLING IT: ';
    $map[$str[$i]]();
    echo '<br> ---------------------------- <br>';
}

Note that if the function does not exist ( four() in this case), it causes an error.

Output:

Number: 1
Function: one
CALLING IT: I am inside function one()
---------------------------- 
Number: 3
Function: tri
CALLING IT: I am inside function tri()
---------------------------- 
Number: 2
Function: two
CALLING IT: I am inside function two()
---------------------------- 
Number: 4
Function: four
CALLING IT: E_ERROR : type 1 -- Call to undefined function four() -- at line 21

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