使用php从多维数组中获取子数组

时间:2018-03-19 07:05:54

标签: php multidimensional-array extract sub-array array-key

我希望使用PHP从多维数组中获取值。我将密钥传递给函数,如果密钥包含值(即没有任何数组值),它将返回该值。但是如果该键包含一个数组值,它将返回整个子数组。

我正在为此添加一个示例数组。

<?php

// prepare a list of array for category, subcategory etc...

$category = array(
'Account' => array(
    'Show Balance' => array(
        'Recharge' => 2300,
        'Success' => 12000,
        'Failure' => 25000,
    ),
    'Balance History' => 'your balance is very low for last 2 years',
    'Mini Statement' => 'This is your mini statement. You can have a look of your transaction details',
    'Last Transaction' => 25000
), 
'Deposit' => array(
    'Deposit Limit' => 40000,
    'Make Deposit' => 'Please go to the nearest branch ans deposit the money.',
    'Last Deposit' => 12000
), 
'FAQ' => array(
    'How To Open An Account' => 'Go to your nearest branch fill up a form, submit it to the branch manger with required supporting documents.',
    'How To Withdraw Money' => 'Go to any ATM center swipe the card enter pin and get your money.',
    'How To Add Money' => 'You need to go to your nearest branch and deposit money over there.'
), 
'Loan' => array(
    'Home Loan' => 'This is home loan related answer',
    'Personal Loan' => 'This is personal loan related answer',
    'Car Loan' => 'This is car loan related answer',
    'Bike Loan' => 'This is bike loan related answer'
) ,
'Test',

);

现在如果我将我的数组$ category和'Recharge'作为参数传递给任何PHP函数,它应该返回2300作为结果。 现在,如果我将我的数组$ category和'Show Balance'作为参数传递给任何PHP函数,它应该返回给我

array(
        'Recharge' => 2300,
        'Success' => 12000,
        'Failure' => 25000,
    ) 

结果。

在谷歌搜索了很多但无法得到答案。

3 个答案:

答案 0 :(得分:3)

尝试这个函数,递归方式:

function get_key_val($arrs, $k) {
    foreach( $arrs as $key=>$val ) {
        if( $key === $k ) {
            return $val;
        }
        else {
            if( is_array( $val ) ) {
                $ret = get_key_val( $val, $k );

                if( $ret !== NULL) {
                    return $ret;
                }
            }
        }
    }

    return NULL;
}

echo get_key_val( $category, "Recharge" ); // outputs 2300

这可以使用 foreach 递归遍历传递数组的每个元素,直到找到索引,并返回索引'value(数组与否)

答案 1 :(得分:3)

为此编写递归函数。执行foreach并使用传递的密钥检查数组键,如果匹配的返回数组。如果不匹配则检查数组值是否为is_array()数组,如果是,则再次调用函数,否则返回值

function getData($categoryArray, $key){
    foreach($categoryArray as $k => $value){ 
        if($k==$key) return $value; 
        if(is_array($value)){ 
            $find = getData($value, $key);
            if($find){
                return $find;
            } 
        }
    }
    return null;
}

$result1 = getData($category, 'Show Balance');
var_dump($result1);
$result = getData($category, 'Recharge');
var_dump($result);

Demo

答案 2 :(得分:0)

要获得显示余额,请尝试以下操作:

print_r($category['Account']['Show Balance']);

要获得充值,请尝试以下方法:

print_r($category['Account']['Show Balance']['Recharge']);

我刚刚对代码进行了测试,以确保其有效,但效果非常好。

- 编辑 -

foreach ($category as $cat) {
  print_r($cat['Account']['Recharge']);
  print_r($category['Account']['Show Balance']);
}