简体   繁体   English

如何从多维数组中获取特定值?

[英]How to get specific values from a multi-dimensional array?

Im trying to understand how i can write a PHP syntax to get the value from a given a PHP array with definition. 我试图了解如何编写PHP语法以从具有定义的给定PHP数组中获取值。

//given definition
$php_array=array(“index1”=>array(“value1”,”value2”,“value3”),
         “index2”=>“value4”,
         “index3”=>array([0]=>“value5”,[1]=>“value6”,
             [2]=>“value7”),
         “index4”=>array([“index5”]=>“value8”,
             [“index6”]=>array(“value9”,”value10”)))

I'm trying to get "value 3", "value 6" and "value "9". 我正在尝试获取“值3”,“值6”和“值” 9。

How do I do that? 我怎么做?

Your values are in these variables: 您的值在以下变量中:

$php_array['index1'][2];
$php_array['index3'][1];
$php_array['index4']['index6'][0];

I think we can format your array like this: 我认为我们可以像这样格式化您的数组:

$php_array = array(
    "index1"=>array("value1","value2","value3"),
    "index2"=>"value4",
    "index3"=>array(
        "value5", "value6","value7"
    ),
    "index4"=>array(
        "index5"=>"value8",
        "index6"=>array(
            "value9","value10"
        )
    )
);

$value3 = $php_array['index1'][2];
$value6 = $php_array['index3'][1];
$value9 = $php_array['index4']['index6'][0];

I'm guessing that we wish to loop through and extract the desired values, and do other things, which we can do so. 我猜想我们希望遍历并提取所需的值,并执行其他操作(我们可以这样做)。


Here, we have a main array with some other arrays inside the array. 在这里,我们有一个主数组数组中还有其他一些数组。 They have indices (keys) and values. 它们具有索引(键)和值。

Our desired outputs can be simply found in these three places: 我们期望的输出可以在以下三个地方找到:

$php_array["index1"][2];
$php_array["index3"][1];
$php_array["index4"]["index6"][0];

where 2, 1 and 0 are the keys of nested or inside arrays in the main array: 其中2、1和0是主数组中嵌套数组或内部数组的键:

Main Array 主阵列

$php_array = array(
    "index1" => ["value1", "value2", "value3"],
    "index2" => "value4",
    "index3" => ["value5", "value6", "value7"],
    "index4" => [
        "index5" => "value8",
        "index6" => ["value9", "value10"],
    ],
);

Example

Here, we loop in our main array, which has four indices. 在这里,我们循环进入具有四个索引的主数组。 Whenever the index is === to our desired value, the program echo or print our desired value, otherwise does nothing: 每当index ===达到我们的期望值时,程序就会echo或打印我们的期望值,否则不执行任何操作:

$php_array = array(
    "index1" => ["value1", "value2", "value3"],
    "index2" => "value4",
    "index3" => ["0" => "value5", "1" => "value6", "2" => "value7"],
    "index4" => ["index5" => "value8", "index6" => ["value9", "value10"]],
);

foreach ($php_array as $key => $value) {
    if ($key === "index1") {
        echo $value["2"];
    }

    if ($key === "index3") {
        echo $value["1"];

    }

    if ($key === "index4") {
        echo $value["index6"]["0"];
    }

}

Output 输出量

value3
value6
value9

References 参考文献

在此处输入图片说明

在此处输入图片说明

Video 视频

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM