1

I' am creating a hierarchical menu using MySQL Database. I' am currently stuck at this point where I' am not able to convert the multidimensional array into a string.

The Outcome

/media/photo-gallery/Title-1

Var Dump of the array

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

I have tried using Implode function, this is what I get

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

Get the parent slug from database

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;
} 

Displays the HTML Menu

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
  • How is Title-1 coming under photo-gallery? do you mean to merge the [0] part recursive and then append [1]? Commented Aug 23, 2014 at 12:12
  • @bansi Yes that's what I mean. Commented Aug 23, 2014 at 12:15
  • Firstly Refine your SQL if it is coming from database. Commented Aug 23, 2014 at 12:29

3 Answers 3

1

looks like your input array is in reverse order, the topmost item goes to the end and the last leaf is the first item. Here is a small function which you can try.

$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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try this

$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);

Output:

media/photo-gallery/Title-1

Comments

1

With Standard PHP Library (SPL) you can get the desired output

<?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;. 

Output:

/media/photo-gallery/Title-1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.