2017-05-04 46 views
-1

下面是我的數組,但我想在父子數據以及如何讓孩子陣列數據在PHP父陣列,mysql的

[16363] => Array 
     (
      [15] => Array 
       (
        [account_id] => 19321 
        [aum] => 104853.92 
        [cl_user_id] => 16363 
        [text] => MICHAEL ANDRISANO INDIVIDUAL 
        [fname] => MICHAEL 
        [lname] => ANDRISANO 
        [account_number] => 906566540 
       ) 

      [16] => Array 
       (
        [account_id] => 19322 
        [aum] => 196539.78 
        [cl_user_id] => 16363 
        [text] => MICHAEL ANDRISANO INDIVIDUAL 
        [fname] => MICHAEL 
        [lname] => ANDRISANO 
        [account_number] => 906566600 
       ) 

     ) 

我想輸出像下面

[16363] => Array 
     (
      [text] => MICHAEL ANDRISANO 
      [cl_id] => 16363 
      [nodes] => Array 
       (
        [0] => Array 
         (
        [account_id] => 19321 
        [aum] => 104853.92 
        [cl_user_id] => 16363 
        [text] => MICHAEL ANDRISANO INDIVIDUAL 
        [fname] => MICHAEL 
        [lname] => ANDRISANO 
        [account_number] => 906566540 
         ) 

        [1] => Array 
         (
          [cl_id] => 16363 
          [account_id] => 19322 
          [aum] => 196539.78 
          [text] => MICHAEL ANDRISANO INDIVIDUAL (906566600) 
          [account_number] => 906566600 

         ) 
       ) 

     ) 
+0

是「[16363]」的根,還是有更多的數組?我認爲這是。 –

+2

我正在投票結束這個問題作爲題外話,因爲SO不是一個代碼寫作服務。 –

+0

@MarcoBonelli它不是?! *** Nooooooooooooooooooo ***我將不得不從頭開始研究和構建自己的代碼!這就是ISIS!我在這裏度過的所有這些時間希望有人能幫助我做出獨特的東西! ': - /' – Martin

回答

0

我不知道您的任何變量名稱,但假設您的原始數組被稱爲$parent,並且您想將其解析爲$clients

簡單地循環遍歷$parent中的每條記錄,然後遍歷子數組,並將它們放在一個新數組中。下面是你如何做到這一點的例子。對於不同的[node]元素,您問題中的預期輸出具有不同數量的鍵,所以我不確定您想要達到的目標,但希望這會讓您開始。

<?php 
//This is what will be our final array (your expected output) 
$clients = array(); 

foreach ($parent as $key=>$children) 
{ 
    // Build the initial $clients meta data. 
    $clients[$key] = array (
     // It seems the following data is identical, so we'll just grab from the first child 
     'text' => $children[key($children)]['fname'] . ' ' . $children[key($children)]['lname'], 
     'cl_id' => $children[key($children)]['cl_user_id'], 
     'nodes' => array() 
    ); 

    // Now populate the nodes 
    foreach ($children AS $child) 
    { 
     $clients[$key]['nodes'][] = array (
      'account_id' => $child['account_id'], 
      'cl_id' => $child['cl_id'], 
      'aum' => $child['aum'], 
      'text' => $child['text'], 
      'account_number' => $child['account_number'] 
     ); 
    } 
} 
?> 
+0

這是一個完美的解決方案,但有一個問題,這是在第二個數組我無法獲得fname和lname –

+0

你好@Robbie這是完美的答案謝謝你'text'=> $ children [0] ['fname']。 ''。 $ children [0] ['lname']我沒有獲得fname和lname我只在兩個記錄中獲得其他 –

+0

@vaibhavPatel,我很高興我的示例有所幫助。但我不太瞭解你的fname和lname問題。因爲'$ children [0] ['fname']'和'$ children [0] ['lname']'是空白的,你是說'$ output [16363] ['cl_id']'最終會變成空白嗎?你提到你只在「兩條記錄」中得到它,但我在原始文章中只看到兩個記錄。 – RToyo