Php 从数组中使用递归函数获取一行中的所有数据
我有一个这样的数组Php 从数组中使用递归函数获取一行中的所有数据,php,arrays,recursion,Php,Arrays,Recursion,我有一个这样的数组 Array ( [0] => Array ( [id] => 1 [category_name] => One [parent_id] => [children_recursive] => Array ( ) ) [1] => Arr
Array
(
[0] => Array
(
[id] => 1
[category_name] => One
[parent_id] =>
[children_recursive] => Array
(
)
)
[1] => Array
(
[id] => 2
[category_name] => Two
[parent_id] =>
[children_recursive] => Array
(
)
)
[2] => Array
(
[id] => 3
[category_name] => Three
[parent_id] =>
[children_recursive] => Array
(
[0] => Array
(
[id] => 17
[category_name] => Three and one
[parent_id] => 3
[children_recursive] => Array
(
)
)
[1] => Array
(
[id] => 19
[category_name] => Three and two
[parent_id] => 3
[children_recursive] => Array
(
[0] => Array
(
[id] => 21
[category_name] => Three and two and one
[parent_id] => 19
[children_recursive] => Array
(
)
)
)
)
[2] => Array
(
[id] => 20
[category_name] => Three and three
[parent_id] => 3
[children_recursive] => Array
(
)
)
)
)
[3] => Array
(
[id] => 4
[category_name] => Four
[parent_id] =>
[children_recursive] => Array
(
[0] => Array
(
[id] => 18
[category_name] => Four and one
[parent_id] => 4
[children_recursive] => Array
(
)
)
)
)
)
我想从这个数组中得到什么
One
Two
Three
Three >> Three and one
Three >> Three and two
Three >> Three and two >> Three and two and one
Three >> Three and three
Four
Four >> Four and one
我试过的
$category = myarray;
renderNode($category);
function renderNode($node) {
foreach($node as $cat){
echo $cat['category_name'].'<br >';
if(!empty($cat[children_recursive'])){
renderNode($cat[children_recursive']);
}
}
}
编辑
这里是使用var\u export
的完整数组列表,因此您只需复制和粘贴即可
array (
0 =>
array (
'id' => 1,
'category_name' => 'One',
'parent_id' => NULL,
'children_recursive' =>
array (
),
),
1 =>
array (
'id' => 2,
'category_name' => 'Two',
'parent_id' => NULL,
'children_recursive' =>
array (
),
),
2 =>
array (
'id' => 3,
'category_name' => 'Three',
'parent_id' => NULL,
'children_recursive' =>
array (
0 =>
array (
'id' => 17,
'category_name' => 'Three and one',
'parent_id' => 3,
'children_recursive' =>
array (
),
),
1 =>
array (
'id' => 19,
'category_name' => 'Three and two',
'parent_id' => 3,
'children_recursive' =>
array (
0 =>
array (
'id' => 21,
'category_name' => 'Three and two and one',
'parent_id' => 19,
'children_recursive' =>
array (
),
),
),
),
2 =>
array (
'id' => 20,
'category_name' => 'Three and three',
'parent_id' => 3,
'children_recursive' =>
array (
),
),
),
),
3 =>
array (
'id' => 4,
'category_name' => 'Four',
'parent_id' => NULL,
'children_recursive' =>
array (
0 =>
array (
'id' => 18,
'category_name' => 'Four and one',
'parent_id' => 4,
'children_recursive' =>
array (
),
),
),
),
)
诸如此类:
function getValues($array, $prefix = '')
{
$values = [];
foreach ($array as $value) {
$values[] = $prefix . $value['category_name'];
if (!empty($value['children_recursive'])) {
$values = array_merge($values, getValues($value['children_recursive'], $prefix . $value['category_name'] . ' >> '));
}
}
return $values;
}
implode('<br>', getValues($array));
函数getValues($array,$prefix='')
{
$values=[];
foreach($array作为$value){
$values[]=$prefix.$value['category_name'];
如果(!empty($value['children\u recursive'])){
$values=array\u merge($values,getValues($value['children\u recursive'],$prefix.$value['category\u name'].>>);
}
}
返回$value;
}
内爆(“
”,getValues($array));
类似的内容:
function getValues($array, $prefix = '')
{
$values = [];
foreach ($array as $value) {
$values[] = $prefix . $value['category_name'];
if (!empty($value['children_recursive'])) {
$values = array_merge($values, getValues($value['children_recursive'], $prefix . $value['category_name'] . ' >> '));
}
}
return $values;
}
implode('<br>', getValues($array));
函数getValues($array,$prefix='')
{
$values=[];
foreach($array作为$value){
$values[]=$prefix.$value['category_name'];
如果(!empty($value['children\u recursive'])){
$values=array\u merge($values,getValues($value['children\u recursive'],$prefix.$value['category\u name'].>>);
}
}
返回$value;
}
内爆(“
”,getValues($array));
$category=myarray;
renderNode($类别);
函数renderNode($parent\u category\u name='',$node){
foreach($cat作为节点){
回显$parent_category_name.$cat['category_name']。
;
如果(!empty($cat[children\u recursive'])){
renderNode($parent\u category\u name.$cat['category\u name'].“>>”,$cat[children\u recursive']);
}
}
}
$category=myarray;
renderNode($类别);
函数renderNode($parent\u category\u name='',$node){
foreach($cat作为节点){
回显$parent_category_name.$cat['category_name']。
;
如果(!empty($cat[children\u recursive'])){
renderNode($parent\u category\u name.$cat['category\u name'].“>>”,$cat[children\u recursive']);
}
}
}
您可以使用。您需要像这样扩展它:
class RecursiveChildrenIterator extends RecursiveArrayIterator
{
public function hasChildren()
{
return !empty($this->current()['children_recursive']);
}
public function getChildren()
{
return new static($this->current()['children_recursive']);
}
}
有了这个类,您只需使用一个foreach
循环即可:
$iterator = new RecursiveIteratorIterator(
new RecursiveChildrenIterator($array),
RecursiveIteratorIterator::SELF_FIRST
);
$result = [];
$current = [];
foreach ($iterator as $item) {
$current[$iterator->getDepth()] = $item['category_name'];
if (!$iterator->hasChildren()) {
$result[] = implode(
'>>',
array_slice($current, 0, $iterator->getDepth() + 1)
);
}
}
注意传递给的递归迭代器::SELF_FIRST
标志
这是
添加:
我实际上误读了您想要的数组,所以要获得您想要的数组,请删除该条件:
foreach ($iterator as $item) {
$current[$iterator->getDepth()] = $item['category_name'];
$result[] = implode(
'>>',
array_slice($current, 0, $iterator->getDepth() + 1)
);
}
这是
您可以阅读更多信息。您可以使用。您需要像这样扩展它:
class RecursiveChildrenIterator extends RecursiveArrayIterator
{
public function hasChildren()
{
return !empty($this->current()['children_recursive']);
}
public function getChildren()
{
return new static($this->current()['children_recursive']);
}
}
有了这个类,您只需使用一个foreach
循环即可:
$iterator = new RecursiveIteratorIterator(
new RecursiveChildrenIterator($array),
RecursiveIteratorIterator::SELF_FIRST
);
$result = [];
$current = [];
foreach ($iterator as $item) {
$current[$iterator->getDepth()] = $item['category_name'];
if (!$iterator->hasChildren()) {
$result[] = implode(
'>>',
array_slice($current, 0, $iterator->getDepth() + 1)
);
}
}
注意传递给的递归迭代器::SELF_FIRST
标志
这是
添加:
我实际上误读了您想要的数组,所以要获得您想要的数组,请删除该条件:
foreach ($iterator as $item) {
$current[$iterator->getDepth()] = $item['category_name'];
$result[] = implode(
'>>',
array_slice($current, 0, $iterator->getDepth() + 1)
);
}
这是
您可以阅读更多信息。
var\u dump
对于表示源数据无效,请尝试使用var\u export
,以便可以复制和粘贴数组。@sevavietl我使用var\u export
添加数组。请参阅我更新的问题var_dump
对于表示源数据无效,请尝试使用var_export
,以便可以复制和粘贴数组。@sevavietl我使用var_export
添加数组。请参阅我更新的问题数组#u merge():参数#2不是数组它是我的错误。我在循环中编写返回语句:Parray#u merge():参数#2不是数组。这是我的错误。我在loop:Pin-the-future中编写返回语句,您可能希望添加一些关于您正在共享的代码的上下文(注释)作为答案。将来,您可能需要添加一些关于您正在共享的代码的上下文(注释)作为答案。供参考