将部分代码转换为函数php

时间:2012-03-29 22:11:22

标签: php

这可能很容易,但出于某种原因,我似乎无法弄清楚这一点。假设我有这样的代码:

$elements = array('a', 'b', 'c', 'd');

$myValues = array(
    'values' => array(
        'a' => array(
            'xx' => 3,
            'yy' => ''
        ),
        'b' => array(
            'xx' => '',
            'yy' => ''
        ),
        'c' => array(
            'xx' => 8.4,
            'yy' => ''
        ),
        'd' => array(
            'xx' => 18.4,
            'yy' => ''
            )
        )
);

foreach($elements as $elem)
{
    if($myValues['values'][$elem]['xx'] != '')
    {
        if($myValues['values'][$elem]['xx'] < 6)
        {
            $myValues['values'][$elem]['yy'] =  'less than 6';
        }
        elseif($myValues['values'][$elem]['xx'] >= 6 && $myValues['values'][$elem]['xx'] < 15)
        {
            $myValues['values'][$elem]['yy'] =  'between 6 and 16';
        }
        else
        {
            $myValues['values'][$elem]['yy'] = 'greater than 15';
        }

            testFunc($myValues['values'][$elem]['xx']); // This is how I would call my function once I replace the code above
    }
}

正如您在此处所看到的,我要做的是根据某些条件更改$myValues['values'][$elem]['yy']的值。我想要做的是用执行相同操作的函数替换代码的if elseif else部分。

我尝试过类似的事情:

function testFunc($xx)
{
    if($xx < 6)
    {
        $yy = 'less than 6';
    }
    elseif($xx >= 6 && $xx < 15)
    {
        $yy =  'between 6 and 16';
    }
    else
    {
        $yy = 'greater than 15';
    }

    return $yy;
}

但显然这不起作用,因为我没有在我的函数中更改$myValues['values'][$elem]['yy']的值。

注意我真的想在我的函数中仅传递$myValues['values'][$elem]['xx']的值,并返回$myValues['values'][$elem]['yy']的更改值。

有人可以帮我吗?

提前致谢

3 个答案:

答案 0 :(得分:3)

编辑:重新阅读这个问题,我乍一看似乎误解了它。这里似乎没有必要提及。这会不起作用吗?

foreach ($elements as $element) {
    $myValues['values'][$element]['yy'] = testFunc($myValues['values'][$element]['xx']);
}

同样,如果$elements始终与array_keys($myValues['values'])相同,则可以使用引用:

foreach ($myValues['values'] as &$value) {
    $value['yy'] = testFunc($value['xx']);
}

答案 1 :(得分:0)

function testFunc()

function testFunc(&$arr) {
  foreach ($arr['values'] as &$values) {
    if ($values['xx'] != '') {
      if ($values['xx'] < 6)
        $values['yy'] = 'less than 6';
      elseif ($values['xx'] >= 6 && $values['xx'] <= 15)
        $values['yy'] = 'between 6 and 15';
      else
        $values['yy'] = 'greater than 15';
      }
    }
  }

用法:

testFunc($myValues);
print_r($myValues);

输出:

Array
(
  [values] => Array
    (

      [a] => Array
        (
          [xx] => 3
          [yy] => less than 6
        )

      [b] => Array
        (
          [xx] => 
          [yy] => 
        )

      [c] => Array
        (
          [xx] => 8.4
          [yy] => between 6 and 15
        )

      [d] => Array
        (
          [xx] => 18.4
          [yy] => greater than 15
        )

    )

)

答案 2 :(得分:0)

foreach($elements as $elem){
    $xx = $myValues['values'][$elem]['xx'];
    if(empty($xx)) continue;
    $myValues['values'][$elem]['yy'] = testFunc($xx);
}

function testFunc($xx){
    if($xx < 6) return 'less than 6';
    return ($xx > 15) ? 'greater than 15' : 'between 6 and 16';
}