2016-04-24 15 views
1

私は(MySQLからの)配列を持って、分割多次元配列:PHP

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

私は、別の配列に[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; 
} 

期待される結果は、各メニューセグメントが最初の配列からの要素のリストを持つ配列です:

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

MySql Data base

そのナビゲーションメニュー&サブメニュー表示のため、

enter image description here

答えて

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

解決済み...あなたの答えが働いています –

+0

あなたは正解をマークできますか? –

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

?> 

出力:

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