【问题标题】:Get a sub array from multidimensional array using php使用php从多维数组中获取子数组
【发布时间】:2018-08-27 16:29:07
【问题描述】:

我想使用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 和“显示余额”作为参数传递给任何 PHPfunction,它应该返回我

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

结果。

在谷歌上搜索了很多,但没有得到答案。

【问题讨论】:

  • 中的"任何函数"是什么意思"如果我将数组$category和'Recharge'作为参数传递给任何php函数"我>?这没有任何意义。
  • 添加那个PHP函数代码

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


【解决方案1】:

要获得显示平衡试试这个:

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

【讨论】:

  • 实际上我不能以这种方式编写代码来获取它的值。因为我不知道关键级别。这意味着密钥可能位于第一级或第二级或第 n 级。所以我只需要传递密钥,然后只有我必须得到答案。
【解决方案2】:

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

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

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

【讨论】:

  • 一个好的答案包括对代码的解释。它有什么作用?为什么 OP 应该尝试一下?
【解决方案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

【讨论】:

    猜你喜欢
    • 2019-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-29
    • 1970-01-01
    相关资源
    最近更新 更多