多维数组

时间:2017-03-19 20:19:16

标签: php mysql arrays multidimensional-array

我的数组需要帮助。几个小时试图找到解决方案,但我无法应对。我尝试以这种方式做到这一点,但是,代码结果证明是完全没用的。

private function findKey($array, $keySearch,$app,$klucz='')
{
    foreach ($array as $key => $item) {
        if ($key == $keySearch) {
            $c2 = [];
            $cache = [
                'nazwa'=>$app->nazwa,
                'link'=>$app->link,
                'child'=>[]
            ];

            // echo $klucz; echo '<br />';

            if($klucz && $klucz != '/'){
                // echo $klucz;
                // $k = explode('/',$klucz);
                // $url   = "/2/22/";
                // debug($klucz);
                $parts = explode('/',$klucz);
                debug($klucz);
                debug($parts);

                $x = 0;
                $last = $parts[count($parts)-1];
                $arr = array();
                while ($bottom = array_pop($parts)) {  
                    if($bottom != $last){

                        $arr = array($bottom => $arr);
                        $x++;
                    }else{
                        array_push($arr, $cache);
                    }


                }

                // $arr['child'] = $cache;
                debug($arr);
                // exit;

                // print_r($output);
                // exit;

                // self::buildKey($k);

                // print_r($c2);

                echo "<br />";
            }


        }
        else {
            $klucz = $klucz.'/'.$key;
            if (is_array($item) && self::findKey($item, $keySearch,$app,$klucz)) {
               // return true;
            }
        }
    }

    // return false;
}

我需要从循环创建数组。从mysql我得到数据作为对象,这个数据视图如下:

  

| ID |由... |链路|父|

     

| 1 | ABC | ABCC | 0 |

     

| 2 | AAA | BBB | 1 |

     

| 3 | AAS | BBC | 2 |

     

| 4 | asdasd | adsasd | 2 |

     

| 5 | asdasd | serae | 4 |

     

| 6 | rywer | twet | 0 |

现在我需要从mysql使用数据的数组,并在数组中显示它:

array(
    [1]=>array(
        id=>1,
        nazwa=>abc
        link=>abcc
        child=>array(

            [2]=>array(
                id=>2,
                nazwa=>aaa
                link=>bbb
                child=>array(

                    [3]=>array(
                        id=>3,
                        nazwa=>aas
                        link=>bbc
                        child=>array(

                        )
                    ),

                    [4]=>array(
                        id=>4,
                        nazwa=>asdasd
                        link=>adsasd
                        child=>array(

                            [5]=>array(
                                id=>5,
                                nazwa=>asdasd
                                link=>serae
                                child=>array(

                                )
                            ),
                        )
                    ),
                )
            )
        )
    ),

    [6]=>array(
        id=>6,
        nazwa=>rywer
        link=>twet
        child=>array(

        )
    ),
)

我认为只是一个简单,良好的循环或功能,但我无法处理它。

2 个答案:

答案 0 :(得分:1)

试试我的代码link to online demo

<?php
$data = array(
    array(
        'id' => 1,
        'name' => 'abc',
        'link' => 'abcc',
        'parent' => 0
    ),
    array(
        'id' => 2,
        'name' => 'aaa',
        'link' => 'bbb',
        'parent' => 1
    ),
    array(
        'id' => 3,
        'name' => 'aas',
        'link' => 'bbc',
        'parent' => 2
    ),
    array(
        'id' => 4,
        'name' => 'asdasd',
        'link' => 'adsasd',
        'parent' => 2
    ),
    array(
        'id' => 5,
        'name' => 'asdasd',
        'link' => 'serae',
        'parent' => 4
    ),
    array(
        'id' => 6,
        'name' => 'rywer',
        'link' => 'twet',
        'parent' => 0
    )
);

function buildMenu($data) {
    usort($data, function($a, $b) {
        if ($a['parent'] == $b['parent']) {
            if ($a['id'] == $b['id']) {
                return 0;
            }
            return ($a['id'] < $b['id']) ? -1 : 1;
        }
        return ($a['parent'] < $b['parent']) ? -1 : 1;
    });

    $shortcuts = array();
    $menu = array();

    foreach($data as &$row) {
        if ($row['parent'] <= 0) {
            $menu[] = &$row;
            $shortcuts[$row['id']] = &$row;
            continue;
        } else {
            $parent = $row['parent'];
            if (!isset($shortcuts[$parent])) {
                throw new \Exception("Menu cannot be build");
            }
            $parentItem = &$shortcuts[$parent];
        }
        if (!isset($parentItem['child'])) {
            $parentItem['child'] = array();
        }
        $parentItem['child'][] = &$row;
        $shortcuts[$row['id']] = &$row;
    }
    return $menu;
}

print_r(buildMenu($data));

它使用引用来保持它的清洁。在buildMenu函数的初始化中,我还对你的xceu数组进行了排序,使数据按父ID进行排序,越来越多。

请同时考虑使用英文变量名。

如果您想从此数组生成<ul>菜单,请使用以下代码:

$menu = '<ul>';
function buildUl($data) {
    $menuHtml = '';
    foreach ($data as $menuItem) {
        $menuHtml .= '<li>';
        $menuHtml .= '<a href="'.$menuItem['link'].'">'.$menuItem['name'].'</a>';
        if (!empty($menuItem['child'])) {
            $menuHtml .= '<ul>';
            $menuHtml .= buildUl($menuItem['child']);
            $menuHtml .= '</ul>';
        }
        $menuHtml .= '</li>';
    }

    return $menuHtml;
}
$menu .= buildUl(buildMenu($data));
$menu .= '</ul>';

echo $menu;

更新示例:http://sandbox.onlinephpfunctions.com/code/27cfa95c066be9b1526b71566e2ec2f2093bdc34

答案 1 :(得分:1)

使用对象时,事情变得更容易:

$data = [
    ['id' => 1, 'nazwa' => 'abc',    'link' => 'abcc',   'parent' => 0],
    ['id' => 2, 'nazwa' => 'aaa',    'link' => 'bbb',    'parent' => 1],
    ['id' => 3, 'nazwa' => 'aas',    'link' => 'bbc',    'parent' => 2],
    ['id' => 4, 'nazwa' => 'asdasd', 'link' => 'adsasd', 'parent' => 2],
    ['id' => 5, 'nazwa' => 'asdasd', 'link' => 'serae',  'parent' => 4],
    ['id' => 6, 'nazwa' => 'rywer',  'link' => 'twet',   'parent' => 0],

];

$objectTree = [];
$objects = [];

foreach ($data as $row) {
    $obj = (object)$row;
    $obj->childs = [];
    $objects[$obj->id] = $obj;
}

foreach ($objects as $obj) {
    if ($obj->parent == 0) {
        $objectTree[] = $obj;
    } else {
        $objects[$obj->parent]->childs[] = $obj;
    }
}

var_export($objectTree);

演示:http://rextester.com/RZRQLX19028