2017-05-04 50 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