Ashwani Kumar
Ashwani Kumar

Reputation: 71

Get a sub array from multidimensional array using php

I want to get a value form a multidimensional array using PHP. I pass the key to the function and if the key contains the value (i.e. without any array value), it will return that. But if that key contains an array value, it will return the whole sub array.

I am adding a sample array for that.

<?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',

);

Now if i pass my array $category and 'Recharge' as a parameter to any PHP function it should return me 2300 as a result. Now if i pass my array $category and 'Show Balance' as a parameter to any PHPfunction it should return me

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

as a result.

Searched a lot on google but couldn't get an answer.

Upvotes: 3

Views: 8510

Answers (3)

Karlo Kokkak
Karlo Kokkak

Reputation: 3714

Try this function, recursive way:

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

This can go through every element of the passed array, using foreach and recursion, until the index is found, and returns the index' value (array or not).

Upvotes: 4

Nisarg Shah
Nisarg Shah

Reputation: 292

To get Show balance try this:

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

To get Recharge try this:

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

I just tested the code to make sure it works, It works perfectly.

--Edit--

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

Upvotes: 1

Bhaskar Jain
Bhaskar Jain

Reputation: 1691

Write a recursive function for this. do foreach and check array key with passed key, if matched return array. if not matched and check array value if it is an array with is_array(), if yes, call function again, else 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

Upvotes: 6

Related Questions