Php 如何从数组转换隐式嵌套

Php 如何从数组转换隐式嵌套,php,arrays,Php,Arrays,给定一个如下所示的PHP数组: [ 'foo' => 1, 'bar[0]' => 6, 'bar[1]' => 7, 'bar[2]' => 8, 'baz' => 'anything', ... and so on ] 我希望将“隐含”嵌套转换为实际数组,同时保持其余部分不变,从而产生: [ 'foo' => 1, 'bar' => [6, 7, 8], 'baz' => 'anything', ] 我已

给定一个如下所示的PHP数组:

[
  'foo' => 1,
  'bar[0]' => 6,
  'bar[1]' => 7,
  'bar[2]' => 8,
  'baz' => 'anything',
  ... and so on
]
我希望将“隐含”嵌套转换为实际数组,同时保持其余部分不变,从而产生:

[
  'foo' => 1,
  'bar' => [6, 7, 8],
  'baz' => 'anything',
]

我已经搜索了php文档,但找不到用于此的实用程序。我相信我可以编写一个函数来实现这一点,但感觉就像是重新发明了轮子。这样的功能肯定已经存在了吗?

只是随便玩玩而已。有关代码的解释,请参见注释

  /*
  our source array
  */
  $a = array(
     'foo' => 1,
     'bar[0]' => 6,
     'bar[1]' => 7,
     'bar[2]' => 8,
     'baz' => 'anything'
  );

  // an array declared to hold all variable names present in array.
  $vars = array();

  /*
  http://php.net/manual/en/function.extract.php
  extract all values from the array with keys are variable names. Keys like 
  bar[0] do not make sense to extract function so it ignores them.
  */
  extract($a);

  /*
  Now that we've got all variables we possibly could using extract(), we
  traverse the source array to create the $bar array ourselves.
  */
  foreach($a as $k => $v) {
     /* 
     if key contains a [
     this check could be rigorous, but I leave that to the production code developer
     */
     if(strstr($k, '[')) {

        /*
        replace the [number] part from key to get the array name, i.e., "bar"
        */
        $arr_name = preg_replace('/\[\d+\]/', '', $k);

        /*
        using variable variables feature (http://php.net/manual/en/language.variables.variable.php)
        check if we've created the array already. if not, create now. and
        record the variable name in $vars array for future reference
        */
        if(!is_array($$arr_name)) {
           $$arr_name = array();
           $vars[] = $arr_name;
        }

        /*
        formulate and eval() (http://php.net/manual/en/function.eval.php) 
        a statement that inserts current $v into our created array
        eval is evil so do some rigorous testing before using it
        */
        eval('$' . $k . '=' . $v . ';');
     }
     else{
        //otherwise just record the variable.
        $vars[] = $k;
     }
  }

  /* $vars holds names of all variables you got from stream */
  var_dump($vars);
  /* display the variables */
  var_dump($foo, $bar, $baz);


 /* almost forgot, http://php.net/manual/en/function.var-dump.php */    

只是玩玩而已。有关代码的解释,请参见注释

  /*
  our source array
  */
  $a = array(
     'foo' => 1,
     'bar[0]' => 6,
     'bar[1]' => 7,
     'bar[2]' => 8,
     'baz' => 'anything'
  );

  // an array declared to hold all variable names present in array.
  $vars = array();

  /*
  http://php.net/manual/en/function.extract.php
  extract all values from the array with keys are variable names. Keys like 
  bar[0] do not make sense to extract function so it ignores them.
  */
  extract($a);

  /*
  Now that we've got all variables we possibly could using extract(), we
  traverse the source array to create the $bar array ourselves.
  */
  foreach($a as $k => $v) {
     /* 
     if key contains a [
     this check could be rigorous, but I leave that to the production code developer
     */
     if(strstr($k, '[')) {

        /*
        replace the [number] part from key to get the array name, i.e., "bar"
        */
        $arr_name = preg_replace('/\[\d+\]/', '', $k);

        /*
        using variable variables feature (http://php.net/manual/en/language.variables.variable.php)
        check if we've created the array already. if not, create now. and
        record the variable name in $vars array for future reference
        */
        if(!is_array($$arr_name)) {
           $$arr_name = array();
           $vars[] = $arr_name;
        }

        /*
        formulate and eval() (http://php.net/manual/en/function.eval.php) 
        a statement that inserts current $v into our created array
        eval is evil so do some rigorous testing before using it
        */
        eval('$' . $k . '=' . $v . ';');
     }
     else{
        //otherwise just record the variable.
        $vars[] = $k;
     }
  }

  /* $vars holds names of all variables you got from stream */
  var_dump($vars);
  /* display the variables */
  var_dump($foo, $bar, $baz);


 /* almost forgot, http://php.net/manual/en/function.var-dump.php */    

只是玩玩而已。有关代码的解释,请参见注释

  /*
  our source array
  */
  $a = array(
     'foo' => 1,
     'bar[0]' => 6,
     'bar[1]' => 7,
     'bar[2]' => 8,
     'baz' => 'anything'
  );

  // an array declared to hold all variable names present in array.
  $vars = array();

  /*
  http://php.net/manual/en/function.extract.php
  extract all values from the array with keys are variable names. Keys like 
  bar[0] do not make sense to extract function so it ignores them.
  */
  extract($a);

  /*
  Now that we've got all variables we possibly could using extract(), we
  traverse the source array to create the $bar array ourselves.
  */
  foreach($a as $k => $v) {
     /* 
     if key contains a [
     this check could be rigorous, but I leave that to the production code developer
     */
     if(strstr($k, '[')) {

        /*
        replace the [number] part from key to get the array name, i.e., "bar"
        */
        $arr_name = preg_replace('/\[\d+\]/', '', $k);

        /*
        using variable variables feature (http://php.net/manual/en/language.variables.variable.php)
        check if we've created the array already. if not, create now. and
        record the variable name in $vars array for future reference
        */
        if(!is_array($$arr_name)) {
           $$arr_name = array();
           $vars[] = $arr_name;
        }

        /*
        formulate and eval() (http://php.net/manual/en/function.eval.php) 
        a statement that inserts current $v into our created array
        eval is evil so do some rigorous testing before using it
        */
        eval('$' . $k . '=' . $v . ';');
     }
     else{
        //otherwise just record the variable.
        $vars[] = $k;
     }
  }

  /* $vars holds names of all variables you got from stream */
  var_dump($vars);
  /* display the variables */
  var_dump($foo, $bar, $baz);


 /* almost forgot, http://php.net/manual/en/function.var-dump.php */    

只是玩玩而已。有关代码的解释,请参见注释

  /*
  our source array
  */
  $a = array(
     'foo' => 1,
     'bar[0]' => 6,
     'bar[1]' => 7,
     'bar[2]' => 8,
     'baz' => 'anything'
  );

  // an array declared to hold all variable names present in array.
  $vars = array();

  /*
  http://php.net/manual/en/function.extract.php
  extract all values from the array with keys are variable names. Keys like 
  bar[0] do not make sense to extract function so it ignores them.
  */
  extract($a);

  /*
  Now that we've got all variables we possibly could using extract(), we
  traverse the source array to create the $bar array ourselves.
  */
  foreach($a as $k => $v) {
     /* 
     if key contains a [
     this check could be rigorous, but I leave that to the production code developer
     */
     if(strstr($k, '[')) {

        /*
        replace the [number] part from key to get the array name, i.e., "bar"
        */
        $arr_name = preg_replace('/\[\d+\]/', '', $k);

        /*
        using variable variables feature (http://php.net/manual/en/language.variables.variable.php)
        check if we've created the array already. if not, create now. and
        record the variable name in $vars array for future reference
        */
        if(!is_array($$arr_name)) {
           $$arr_name = array();
           $vars[] = $arr_name;
        }

        /*
        formulate and eval() (http://php.net/manual/en/function.eval.php) 
        a statement that inserts current $v into our created array
        eval is evil so do some rigorous testing before using it
        */
        eval('$' . $k . '=' . $v . ';');
     }
     else{
        //otherwise just record the variable.
        $vars[] = $k;
     }
  }

  /* $vars holds names of all variables you got from stream */
  var_dump($vars);
  /* display the variables */
  var_dump($foo, $bar, $baz);


 /* almost forgot, http://php.net/manual/en/function.var-dump.php */    

您可以使用
array\u walk()
preg\u match
查看键是否应为“数组”。然后,我们可以通过引用传入最终数组,以允许我们对其进行编辑

比如说

<?php

$a = [
  'foo' => 1,
  'bar[0]' => 6,
  'bar[1]' => 7,
  'bar[2]' => 8,
  'baz' => 'anything',
];

$end = [];

array_walk($a, function($val, $key) use(&$end) {
    //See if the key is something like "bar[1]"
    if( preg_match("/^([a-z]+)\[[0-9]+\]$/", $key, $match) ) {
       //See if "bar" key exists in our final array, if not create it.
       if( array_key_exists($match[1], $end) == FALSE ) {
         return $end[$match[1]] = array($val);
       }
       //Add value to array we created above
       return $end[$match[1]][] = $val;
    }
    //It's just a normal key, so just add it to our final array
    return $end[$key] = $val;
});

print_r($end);

您可以使用
array\u walk()
preg\u match
查看键是否应为“数组”。然后,我们可以通过引用传入最终数组,以允许我们对其进行编辑

比如说

<?php

$a = [
  'foo' => 1,
  'bar[0]' => 6,
  'bar[1]' => 7,
  'bar[2]' => 8,
  'baz' => 'anything',
];

$end = [];

array_walk($a, function($val, $key) use(&$end) {
    //See if the key is something like "bar[1]"
    if( preg_match("/^([a-z]+)\[[0-9]+\]$/", $key, $match) ) {
       //See if "bar" key exists in our final array, if not create it.
       if( array_key_exists($match[1], $end) == FALSE ) {
         return $end[$match[1]] = array($val);
       }
       //Add value to array we created above
       return $end[$match[1]][] = $val;
    }
    //It's just a normal key, so just add it to our final array
    return $end[$key] = $val;
});

print_r($end);

您可以使用
array\u walk()
preg\u match
查看键是否应为“数组”。然后,我们可以通过引用传入最终数组,以允许我们对其进行编辑

比如说

<?php

$a = [
  'foo' => 1,
  'bar[0]' => 6,
  'bar[1]' => 7,
  'bar[2]' => 8,
  'baz' => 'anything',
];

$end = [];

array_walk($a, function($val, $key) use(&$end) {
    //See if the key is something like "bar[1]"
    if( preg_match("/^([a-z]+)\[[0-9]+\]$/", $key, $match) ) {
       //See if "bar" key exists in our final array, if not create it.
       if( array_key_exists($match[1], $end) == FALSE ) {
         return $end[$match[1]] = array($val);
       }
       //Add value to array we created above
       return $end[$match[1]][] = $val;
    }
    //It's just a normal key, so just add it to our final array
    return $end[$key] = $val;
});

print_r($end);

您可以使用
array\u walk()
preg\u match
查看键是否应为“数组”。然后,我们可以通过引用传入最终数组,以允许我们对其进行编辑

比如说

<?php

$a = [
  'foo' => 1,
  'bar[0]' => 6,
  'bar[1]' => 7,
  'bar[2]' => 8,
  'baz' => 'anything',
];

$end = [];

array_walk($a, function($val, $key) use(&$end) {
    //See if the key is something like "bar[1]"
    if( preg_match("/^([a-z]+)\[[0-9]+\]$/", $key, $match) ) {
       //See if "bar" key exists in our final array, if not create it.
       if( array_key_exists($match[1], $end) == FALSE ) {
         return $end[$match[1]] = array($val);
       }
       //Add value to array we created above
       return $end[$match[1]][] = $val;
    }
    //It's just a normal key, so just add it to our final array
    return $end[$key] = $val;
});

print_r($end);

为什么?你为什么要这么做?但这感觉就像是重新创造了我们所做的一切:
$bar[0]=6?!我没有写数组的内容,它来自上游。我只需要分析一下,为什么?你为什么要这么做?但这感觉就像是重新创造了我们所做的一切:
$bar[0]=6?!我没有写数组的内容,它来自上游。我只需要分析一下,为什么?你为什么要这么做?但这感觉就像是重新创造了我们所做的一切:
$bar[0]=6?!我没有写数组的内容,它来自上游。我只需要分析一下,为什么?你为什么要这么做?但这感觉就像是重新创造了我们所做的一切:
$bar[0]=6?!我没有写数组的内容,它来自上游。我只需要解析它们。