将多维数组转换为字符串

时间:2014-08-23 12:04:11

标签: php mysql arrays multidimensional-array

我'我正在使用MySQL数据库创建一个分层菜单。我'我目前卡在这一点上我在哪里我无法将多维数组转换为字符串。

结果

/media/photo-gallery/Title-1

数组的转储

array(2) {
  [0]=>
  array(2) {
    [0]=>
    array(1) {
      [1]=>
      string(5) "media"
    }
    [1]=>
    string(13) "photo-gallery"
  }
  [1]=>
  string(7) "Title-1"
}

我尝试过使用Implode功能,这就是我得到的

$path = implode("/", $path);
string(19) "Array/photo-gallery"

从数据库获取父slug

function get_path( $page_id ) { 
    global $db;
    $sql = "SELECT * FROM pages WHERE page_id = $page_id";
    $query = $db->SELECT($sql);
    $rows = $db->FETCH_OBJECT();
    $path = array();    
    foreach( $rows as $row ){
        $page_id = $row->page_id;
        $page_parent = $row->page_parent;
        $page_slug = $row->page_slug;

        $path[] = $page_slug;
        $path[] = get_path( $page_parent );
        $path = array_reverse( $path );
        $path = array_filter( $path );
    }
    return $path;
} 

显示HTML菜单

function display_children( $parent = "0", $level = "0" ){
    global $db;

    echo "<ul>";
    // retrieve all children of $parent 
    $sql = "SELECT * FROM pages WHERE page_parent = $parent";
    $query = $db->SELECT($sql);
    $rows = $db->FETCH_OBJECT();        
    foreach( $rows as $row ){
        $page_id = $row->page_id;
        $page_parent = $row->page_parent;
        $page_title = $row->page_title;
        $page_slug = $row->page_slug;

        $path = get_path($page_parent);
        $path = array_unique(array_values($path));          
        var_dump($path);

        echo "<li>";
        echo "<a href=\"\">";
        echo "$page_title | Parent ID: $page_parent";
        echo "</li>";
        echo "</a>";

        // child's children 
        display_children($page_id, $level + 1); 

    }
    echo "</ul>";

}   

3 个答案:

答案 0 :(得分:1)

试试这个

$array = array(
    0 =>array(
        0 => array("media","photo-gallery"),
    ),
    1 => "Title-1"
);

$new_array = array();
foreach($array as $k1=>$v1){
if(is_array($v1)){
    $new_array = parseArray($new_array, $k1, $v1);
}else{
    $new_array = array_merge($new_array, array($k1=>$v1));
}
}

function parseArray($new_array, $key, $val){
if(is_array($val)){
    foreach($val as $k2=>$v2){
        if(is_array($v2)){
            $new_array = parseArray($new_array, $k2, $v2);
        }else{
            $new_array = array_merge($new_array, array($k2=>$v2));
        }
    }
}else{
    $new_array = array_merge($new_array, array($key=>$val));
}
return $new_array;
}
echo implode('/', $new_array);

<强>输出:

media/photo-gallery/Title-1

答案 1 :(得分:1)

使用Standard PHP Library (SPL),您可以获得所需的输出

<?php
 $array = array(
    0 =>array(
        0 => array("media","photo-gallery"),
    ),
    1 => "Title-1"
);


$op = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
foreach($op as $each) {
  $result .= '/'.$each;
}

echo $result;. 

<强>输出:

  

/媒体/照片库/标题-1

答案 2 :(得分:1)

看起来您的输入数组的顺序相反,最上面的项目结束,最后一个叶子是第一个项目。这是一个小功能,您可以尝试。

$path = array(array(array('media'),'photo-gallery'),'Title-1');

var_dump($path);
$path = implode('/',sanitizePath($path));
echo $path;

function sanitizePath($pathArray, $initialPath=array()){
    foreach($pathArray as $a){
        if(is_array($a)){
            $initialPath=sanitizePath($a,$initialPath);
        }else{
            array_push($initialPath,$a);
        }
    }
    return $initialPath;
}