如何在php、mysql中获取父数组中的子数组数据
下面是我的数组,但我希望在父数组和如何在php、mysql中获取父数组中的子数组数据,php,mysql,arrays,Php,Mysql,Arrays,下面是我的数组,但我希望在父数组和 [16363] => Array ( [15] => Array ( [account_id] => 19321 [aum] => 104853.92 [cl_user_id] => 16363 [te
[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
)
)
)
我不知道您的任何变量名,但假设您的原始数组名为
$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']
);
}
}
?>
是[16363]
根,还是该数组还有更多?我想是的。我投票结束这个问题,因为它不是一个代码编写服务。@MarcoBonelli不是吗?!不,我必须从头开始研究和构建我自己的代码!这就是伊斯兰国!我花了这么多时间在这里,希望有人能帮我做一些独特的东西<代码>:-/
ha;哈哈ㅤㅤㅤㅤ这是一个完美的解决方案,但其中一个问题是在第二个数组中我无法获得fname和lnameHello@Robbie这是完美的答案谢谢您的“text”=>$children[0]['fname'].”$孩子们[0]['lname']我没有得到fname,而lname我只得到了两张唱片,那怎么办other@vaibhavPatel,我很高兴我的例子有帮助。但我不太明白你的名字和名字问题。你是说,$output[16363]['cl_id']
最终是空的,因为$children[0]['fname']
和$children[0]['lname']
是空的?您提到您在“仅两条记录”中获得了它,但我在原始帖子的示例中只看到了两条记录。下面是我的输出[16348]=>Array([text]=>KEVIN MCGOVERN[cl_id]=>16348[nodes]=>Array([0]=>Array)([账户id]=>19304[cl\U id]=>16348[aum]=>0.00[text]=>KEVIN MCGOVERN TRUST[账户编号]=>922037608)))下面是我没有fname lnam的第二个数组,因为有些数组可能有两个或更多子数组,这就是为什么[16349]=>array([text]=>[cl\u id]=>[nodes]=>array([0]=>array([account\u id]=>19305[cl_id]=>16349[aum]=>0.00[text]=>KEVIN MCGOVERN TRUST[account_number]=>922037610])