私はユーザーの状態に応じてnavメニューを出力するルーチンを作成しようとしています。ユーザーが複数の役割を持つことができること、それは想定され多目的配列要素が配列そのものかどうかを確認してください
array(2) {
["Company 1"]=>
array(2) {
[0]=>
string(3) "dir"
[1]=>
string(5) "manag"
}
["company 2"]=>
string(3) "dir"
}
:それは次のようになり、ネストされた配列変数(会社=>ロール)を供給する必要があります。
今私のルーチン(簡易版、ちょうど作業ロジックイマイチを表示するために):
function get_menu_1 ($status) {
foreach ($status as $company => $position) {
$a = is_array($company); //THIS ALWAYS RETURNS FALSE this is for debugging
echo "<br>this element is array = $a<br>"; //this is for debugging
if (true == is_array($company)) { // THIS ALWAYS RETURNS FALSE this user in this company has multiple roles
foreach ($company as $subcompany => $subposition) {
echo "<br>$subposition<br>";
}
} else { //its not an array, user has one role in the company
echo "<br>$position<br>";
}
}
}
出力は次のとおりです。is_array()
は、変数が配列であるかどうかを確認するために失敗したいくつかの理由
Notice: Array to string conversion in /sata2/home/users/xreact/www/cert.xreact.org/functions.php on line 393
Array
this element is array =
dir
array(2) { ["Company 1"]=> array(2) { [0]=> string(3) "dir" [1]=> string(5) "manag" } ["Company 2"]=> string(3) "dir" }
。
あなたは配列どんなにかのように位置を追加しようとするかもしれません位置は1つだけです。 – xpuc7o