2012-12-30 84 views
0

我試圖插入一個PHP數組到一個MySQL數據庫,但我遇到了這個特定數組的麻煩。我試圖創建一個函數,它Flatten索引數組

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; 
} 
+0

你怎麼想他們被夷爲平地,我們應該添加的cols,還是會與所有的值(而不是陣列的字符串)> – pzirkind

+0

Thanks @pzirkind,包含數組的單個鍵/值需要用包含的列和值替換。 所以,它需要仍然是一個數組。 –

+0

啊,那麼我們要扁平化(如果數組仍然存在) – pzirkind

回答

0

你需要的是什麼所謂的遞歸功能(詳情參見這個問題:What is a RECURSIVE Function in PHP?)。

而且似乎每個「欄」陣列中是一個字符串(和非數字)。

你可能想嘗試什麼是檢查數組中的第一個(或任何)鍵是否是數字。

的代碼看起來是這樣的:

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)循環遍歷$ extra中的每個元素ctedArray(從第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. 
} 

希望這是有幫助的

+0

我正在使用xml-> json解碼,所以是的,cols和values都是雙引號字符串。 –

+0

這就是我說的問題。當你知道數組中存在什麼索引號以及有多少個巢時,你的函數非常適用。現在它只提取索引[0]和共享無巢的格式的數組,而忽略所有其他列和值,如[1]和[2]。 –

+0

它會繼續刪除索引[0](儘可能多的圖層),因爲函數通過調用自己來檢查結果 – pzirkind