如何从多维数组中提取数据

时间:2017-11-04 08:58:03

标签: php

如果[real]

,我有以下代码如何提取[name] => Y Downtime Switch
[inputs] => Array
        (
            [1] => Array
                (
                    [name] => X Downtime Switch
                    [raw] => 10
                    [real] => 0.01
                    [unit] => V
                    [count] => 0
                    [maxval] => 10
                    [minval] => 0
                    [decimals] => 2
                    [alarm] => A---
                )

            [2] => Array
                (
                    [name] => Y Downtime Switch
                    [raw] => 9197
                    [real] => 9.2
                    [unit] => V
                    [count] => 0
                    [maxval] => 10
                    [minval] => 0
                    [decimals] => 2
                    [alarm] => ----
                )

            [3] => Array
                (
                    [name] => Z Run PR
                    [raw] => 9187
                    [real] => 9187
                    [unit] => mV
                    [count] => 0
                    [maxval] => 10000
                    [minval] => 0
                    [decimals] => 0
                    [alarm] => ----
                    )
)

1 个答案:

答案 0 :(得分:1)

为了帮助自己:

<?php
$data = [
    [
        'name' => "X Downtime Switch",
        'real' => 0.01,
    ],
    [
        'name' => "Y Downtime Switch",
        'real' => 9.2
    ],
    [
        'name' => "Z Run PR",
        'real' => 9187
    ]
];
$output = null;
array_walk($data, function($entry) use (&$output) {
    if ($entry['name'] == "Y Downtime Switch") {
        $output = $entry['real'];
    }
} );
var_dump($output);

输出显然是:

float(9.2)

但是我真的建议你先阅读一下这个地方的工作原理:

我们不是来为你做你的工作。我们随时为您解决在尝试自行解决任务时遇到的具体问题。这意味着您需要编写自己的代码。