猿问

验证php数组中内部kery的值?

是否可以检查并获取数组的内部键的值?这很容易解释,但不可能实现!


所以用几句话,


我有多级数组,如: ARRAY ( 1KEY ARRAY ( 2KEY => 2VAL, 3KEY = 3VAL ) )


我需要检查是否2KEY存在以及value它有什么!


这是我正在处理的代码:


<?php


$array = array

  (

    "first.php" => array

                    (

                      "edit" => "0",

                      "add" => "1",

                      "delete" => "0",

                    ),

    "second.php" => array

                    (

                      "edit" => "1",

                      "add" => "1",

                      "delete" => "1",

                    )

  );

//This checks the current page that we are if it exists in array,

if (isset($array[$_SERVER['PHP_SELF']])) {


    //Pass permissions into variables

    $allowEdit = isset($array['edit'])?"1":"0";

    $allowAdd = isset($array['add'])?"1":"0";

    $allowDelete = isset($array['delete'])?"1":"0";


} else {

    echo "This page don't exist!";

}


?>


慕无忌1623718
浏览 192回答 2
2回答

泛舟湖上清波郎朗

如果你有 PHP7+,你可以使用空合并运算符??。请注意,您需要使用basenameof$_SERVER['PHP_SELF']以确保查找有效:$page = basename($_SERVER['PHP_SELF']);$allowEdit = $array[$page]['edit'] ?? 0;$allowAdd = $array[$page]['add'] ?? 0;$allowDelete = $array[$page]['delete'] ?? 0;

猛跑小猪

你可以&nbsp;empty(!empty($array['1KEY']['2KEY']))&nbsp;?&nbsp;($array['1KEY']['2KEY'])&nbsp;:&nbsp;'';
随时随地看视频慕课网APP
我要回答