将多维数组拆分为单个数组:PHP

时间:2022-06-17 22:04:54

I have an array (From mysql),

我有一个数组(来自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

我需要将它拆分为单独的数组,基于[menu],检查这个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' => [
        [...], 
        [...],
    ],
]

将多维数组拆分为单个数组:PHP

its for display an Navigation Menu & Submenu,

它用于显示导航菜单和子菜单,

将多维数组拆分为单个数组:PHP

2 个解决方案

#1


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

#2


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

#1


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

#2


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