1

I have an array (From mysql),

Array ( 
    [0] => Array ( 
        [heading] => Page Name change 
        [name] => Page_Name_change 
        [menu] => online 
    ) 
    [1] => Array ( 
        [heading] => Lorem ipsum dolor 
        [name] => Lorem_ipsum_dolor_ 
        [menu] => akshaya 
    ) 
    [2] => Array ( 
        [heading] => fgdfgfdgdfgdf 
        [name] => fgdfgfdgdfgdf 
        [menu] => akshaya 
    ) 
)

I need to split it into separate arrays, basis on [menu], check this php

function getpage() {
    $query = "SELECT heading,name,menu FROM pages";
    $res = $this->_conn->query($query);
    while ($row = mysqli_fetch_assoc($res)) {
        $result[] = $row;
    }
    for ($i=0; $i < count($result); $i++) {
        if (strcmp($result[$i]['menu'],'akshaya') == 0) {
            for ($j=0; $j < count($result[$i]) ; $j++)
                $menu = $result[$i][$j];
        }
    }
    //if(strcmp($row['menu'],'akshaya')==0) { }
    return $menu;
}

The expected result is the array where each menu segment has the list of the elements from the initial array, like that:

[
    'online' => [
        [
            'heading' => '...',
            'name' => '...',
        ],
    ],
    'akshaya' => [
        [...], 
        [...],
    ],
]

MySql Data base

its for display an Navigation Menu & Submenu,

enter image description here

2 Answers 2

1
function getpage() {
    $query = "SELECT heading,name,menu FROM pages";
    $res = $this->_conn->query($query);
    while ($row = mysqli_fetch_assoc($res)) {
        $result[] = $row;
    }

    $menu = [];
    foreach ($result as $menuItem) {
        $menu[$menuItem['menu']][] = $menuItem;
    }

    return $menu;
}
Sign up to request clarification or add additional context in comments.

Comments

0
function getpage() {
    $query = "SELECT heading,name,menu FROM pages";
    $res = $this->_conn->query($query);
    while ($row = mysqli_fetch_assoc($res)) {
        $result[] = $row;
    }

    $menu = [];
    foreach ($result as $menuItem) {
        $menu[$menuItem['menu']][] = $menuItem;
    }

    return $menu;
}



<?php
$nav=new Pages();
$res=$nav->getpage();
//print_r($res);
echo "<br>";
foreach ($res as $r){
   // print_r($r);
    echo "<br>";
    foreach($r as $rc){

         if (strcmp($rc['menu'],'akshaya') == 0) {
             $akshaya[]=$rc;
    }
    echo "<br>";
}
}
   print_r($akshaya); 
echo count($res);

?>

OutPut:

Array ( [0] => Array ( [heading] => Lorem ipsum dolor [name] => Lorem_ipsum_dolor_ [menu] => akshaya ) [1] => Array ( [heading] => fgdfgfdgdfgdf [name] => fgdfgfdgdfgdf [menu] => akshaya ) ) 

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.