0

我正在尝试将 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.

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

更新我用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;
}
4

1 回答 1

0

您需要的是所谓的递归函数(有关更多信息,请参阅此问题: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)然后我们再次(递归地)调用自己,将新的临时变量传递给它(直到没有更多的子数组离开

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.
}

希望这有帮助

于 2012-12-30T23:23:13.533 回答