展平索引数组

时间:2012-12-30 22:26:12

标签: php mysql arrays function multidimensional-array

我正在尝试将PHP数组插入到mysql数据库中,但是我遇到了这个特殊数组的问题。我正在尝试创建一个带

的函数
 array( 0 => array ( 'col1' => 'value', 'col2' => 'value', 'col3' => 'value', 'col4' => value', 'col5' => 'value', 'col6' => array ( 'string' => array ( 'col7' => 'value' , 'col8' => 'value'), ), ), 

    1 => array ( 'col1' => 'value', 'col2' => 'value', 'col3' => 'value', 'col4' => array ( ), 'col5' => 'value', 'col6' => array ( 'string' => array ( ), ),  ),

     2 => array ( 'col1' => 'value', 'col2' => 'value', 'col3' => 'value', 'col4' => array ( ), 'col5' => 'value', 'col6' => array ( 'string' => array ( ), ), ), )

并分隔每个带编号的数组并将其格式化为:

array( 'col1' => 'value', 'col2' => 'value', 'col3' => 'value', 'col4' => 'value', 'col5' => 'value', ['col6' => 'value','col7' => 'value',] )

array( 'col1' => 'value', 'col2' => 'value', 'col3' => 'value', 'col4' => 'value', 'col5' => 'value', ['col6' => 'value','col7' => 'value',] )

array( 'col1' => 'value', 'col2' => 'value', 'col3' => 'value', 'col4' => 'value', 'col5' => 'value', ['col6' => 'value','col7' => 'value',] )

取决于有多少行。请记住这些条件:

+8 cols is just an example, the range can fluctuate greatly. 
+Cols containing a array must be non existent if it empty, like in [1] and [2], but not [0].
+Any column could contain a empty or full array. if it contains a full array, it needs to be flatten while retaining it's value.
+Some arrays might have greater then 2 nested arrays. (elements)  
+Not all of the arrays have nested arrays, they are already formatted correctly. These arrays can not be affected by the PHP function i'm trying to create.

我很难过,我创造的每个功能都失败了。提前谢谢大家。

UPDATE 我使用Var_export函数来获取上面的数组。

    function flatten($array, $preserve_keys = false)
{
  if (!$preserve_keys) {
 $array = array_values($array);

  }
  $flattened_array = array();

  foreach ($array as $k => $v) {
   $flattened_array[$k] = $v;
    if (is_array($v)) {

      $flattened_array = array_merge($flattened_array, call_user_func(__FUNCTION__, $v, $preserve_keys));
    } elseif ($preserve_keys) {
      $flattened_array[$k] = $v;
    } else {
      $flattened_array[] = $v;
    }
  }
  return (array)$flattened_array;
}

1 个答案:

答案 0 :(得分:0)

您需要的是所谓的递归函数(有关详细信息,请参阅此问题:What is a RECURSIVE Function in PHP?)。

似乎数组中的每个“col”都是一个字符串(而不是数字)。

您可能想要尝试的是检查数组中的第一个(或任何)键是否为数字。

代码看起来像这样:

public function extractArray($myArray){

    $extractedArray = array(); // create a new array where all the flattened data will go
    $arrayKeys = array_keys($myArray); // grab all the keys from the array
    if (is_numeric($arrayKeys[0])): // if the first key is a number (it's nested)
        $extractedArray = $myArray[0]; // remove one level of the array by grabbing everything from the first element
        extractArray($extractedArray); // run our function again to check if we need to remove another layer
    else:

        $extractedArray = $myArray; // seems like there are no layers that need to be removed
    endif;
    return $extractedArray;
}

修改

这是问题第二部分的可能解决方案(从可能的子数组中提取值)

以下是我们要采取的步骤:

1)循环遍历$ extractedArray中的每个元素(来自第1部分)并查看它是否是子数组

2)如果它是一个数组,我们提取它的内容并将它放在一个临时变量(现在是一个数组)

3)然后我们再次(递归地)调用自己传递新的临时变量(直到不再有子arrrays

4)一旦我们提取了所有值并将它们展平为一个数组,我们就可以循环遍历该数组并将它们存储为字符串(或者我们喜欢的任何其他值)

更新代码 这是代码:

public function flattenArrayValues($extractedArray){
     $extractedValues = array();
     foreach($extractedArray as $key => $eA):
          if(is_array($eA)): // check whether current element is an array
            reset($eA); // go to the first element of the array and grab it
            $first_key = key($eA); // ...
            $extractedValues[$key] = $eA[$first_key]; // this allows us to preserve the "cols" for each element
            flattenArrayValues($extractedValues); //call ourselves again to check whether there are any sub-arrays
          endif;
     endforeach;
     return $extractedValues; // seems like we extracted all the values and flattened them 
     // if we want we can loop through this new array and build a string out of it etc.
}

希望这是有帮助的