使用 func_get_args 编辑数组

发布于 2024-09-19 20:26:02 字数 945 浏览 2 评论 0原文

我希望使用具有任意数量参数的函数来编辑数组。到目前为止我的代码是:

 function setProperty()
 {
  $numargs = func_num_args();
  $arglist = func_get_args();
  $toedit = array();
  for ($i = 0; $i < $numargs-1; $i++)
  {
   $toedit[] = $arglist[$i];
   }
   $array[] = $arglist[$numargs-1];
 }

代码的想法是我可以执行以下操作:

setProperty('array', '2nd-depth', '3rd', 'value1');
setProperty('array', 'something', 'x', 'value2');
setProperty('Another value','value3');

产生以下数组:

Array
(
    [array] => Array
        (
            [2nd-depth] => Array
                (
                    [3rd] => value1
                )

            [something] => Array
                (
                    [x] => value2
                )

        )

    [Another Value] => value3
)

我认为问题在于该行:

$toedit[] = $arglist[$i];

为了实现所需的功能,该行需要什么?

干杯,

I wish to use a function with an arbitrary number of arguments to edit an array. The code I have so far is:

 function setProperty()
 {
  $numargs = func_num_args();
  $arglist = func_get_args();
  $toedit = array();
  for ($i = 0; $i < $numargs-1; $i++)
  {
   $toedit[] = $arglist[$i];
   }
   $array[] = $arglist[$numargs-1];
 }

The idea of the code being I can do the following:

setProperty('array', '2nd-depth', '3rd', 'value1');
setProperty('array', 'something', 'x', 'value2');
setProperty('Another value','value3');

Resulting in the following array:

Array
(
    [array] => Array
        (
            [2nd-depth] => Array
                (
                    [3rd] => value1
                )

            [something] => Array
                (
                    [x] => value2
                )

        )

    [Another Value] => value3
)

The issue I believe is with the line:

$toedit[] = $arglist[$i];

What does this line need to be to achieve the required functionality?

Cheers,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何必那么矫情 2024-09-26 20:26:16

首先尝试使用 foreach 循环遍历数组。然后,为了处理子项,您可以将其传递给一个子函数,该子函数将获取所有内容。

我还建议首先使用函数 sizeof() 来确定数组有多大,这样您就会知道上限。

Try using foreach to loop through your array first. Then to handle children, you pass it to a child function that will grab everything.

I also recommend using the function sizeof() to determine how big your arrays are first, so you'll know your upper bounds.

演出会有结束 2024-09-26 20:26:13
class foo {
private $storage;
function setProperty()
{
    $arglist = func_get_args();
    if(count($argslist) < 2) return false;
    $target = &$this->storage;
    while($current = array_shift($arglist)){
        if(count($arglist)==1){
             $target[$current] = array_shift($arglist);
             break;
        }
        if(!isset($target[$current])) $target[$current] = array();
        $target = &$target[$current];
    }
}
}
class foo {
private $storage;
function setProperty()
{
    $arglist = func_get_args();
    if(count($argslist) < 2) return false;
    $target = &$this->storage;
    while($current = array_shift($arglist)){
        if(count($arglist)==1){
             $target[$current] = array_shift($arglist);
             break;
        }
        if(!isset($target[$current])) $target[$current] = array();
        $target = &$target[$current];
    }
}
}
舂唻埖巳落 2024-09-26 20:26:09

在存储新值之前,您需要沿着路径走到目的地。您可以通过参考来做到这一点:

function setProperty() {
    $numargs = func_num_args();
    if ($numargs < 2) return false; // not enough arguments
    $arglist = func_get_args();

    // reference for array walk    
    $ar = &$array;
    // walk the array to the destination
    for ($i=0; $i<$numargs-1; $i++) {
        $key = $arglist[$i];
        // create array if not already existing
        if (!isset($ar[$key])) $ar[$key] = array();
        // update array reference
        $ar = &$ar[$key];
    }

    // add value
    $ar = $arglist[$numargs-1];
}

但是这个 $array 应该存储在哪里的问题仍然存在。

You need to walk the path to the destination before storing the new value. You can do this with a reference:

function setProperty() {
    $numargs = func_num_args();
    if ($numargs < 2) return false; // not enough arguments
    $arglist = func_get_args();

    // reference for array walk    
    $ar = &$array;
    // walk the array to the destination
    for ($i=0; $i<$numargs-1; $i++) {
        $key = $arglist[$i];
        // create array if not already existing
        if (!isset($ar[$key])) $ar[$key] = array();
        // update array reference
        $ar = &$ar[$key];
    }

    // add value
    $ar = $arglist[$numargs-1];
}

But the question where this $array should be stored still remains.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文