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

Get a sub array from multidimensional array using php

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

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

<?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 函数,结果应该 return 我 2300。 现在,如果我将我的数组 $category 和 'Show Balance' 作为参数传递给任何 PHP 函数,它应该 return 我

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

结果。

在 google 上搜索了很多,但找不到答案。

要获得显示余额试试这个:

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']);
}

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

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递归,直到找到索引,并且returns 索引值(数组或非数组)。

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

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