1

How can I loop into the plans datas of this array:

$currencies = array(
    array(
        'currency' => 'mxn', 
        'sign'     => '$',
        'plans'    => array(
            'tiny'   => 429,
            'small'  => 1319,
            'medium' => 3399,
            'big'    => 6669
        )
    ),
    array(
        'currency' => 'usd', 
        'sign'     => '$',
        'plans'    => array(
            'tiny'   => 29,
            'small'  => 319,
            'medium' => 399,
            'big'    => 669
        )
    )
);

If I have only the currency ?

$currency = 'mxn';

I tried:

foreach($currencies as $currency => $info) {
    if($info['currency'] = 'mxn') {
        ....
    }
}

Thanks.

  • Your if statement is using an assignment operator (single =) and should be equal (double ==) or identical (triple ===). – DaveStSomeWhere Jan 13 '18 at 02:35
  • This looks close: https://stackoverflow.com/questions/46690169/how-can-i-check-index-in-array-2-dimensional-without-loop-in-php I'll see if I can find a better one (there are hundreds of related pages to sift through). – mickmackusa Jan 13 '18 at 04:08
  • Also https://stackoverflow.com/questions/13933454/get-the-sub-array-having-a-particular-value – mickmackusa Jan 13 '18 at 05:42

2 Answers2

1

If I understood correctly you want to loop the array "plans" when the "currency" is equal to 'mxn'. Here is:

<?php
foreach($currencies as $key => $data) {
  if($data['currency'] == 'mxn')
  {
    echo 'List of plans: <br />';
    foreach($data['plans'] as $item){
        echo $item.'<br />';
    }
  }
}
?>

First, the code checks which array is the mxn currency and do a loop on the "plans" array.

Just to complement the post with a simplified alternative:

<?php
$key = array_search('mxn', array_column($currencies, 'currency'));
foreach($currencies[$key]['plans'] as $item){
    echo $item.'<br />';
}
?>
mad4n7
  • 171
  • 7
  • This is not a refined answer because it will continue to iterate after the targeted subarray is found. There is a combination of php functions that offers a superior method. – mickmackusa Jan 13 '18 at 03:20
  • I know that. I always try to explain with more code to people understand what's exactly happening but I posted a simplified or "superior" method. Thanks for your concern @mickmackusa – mad4n7 Jan 13 '18 at 03:48
0

This question is an excellent opportunity to demonstrate a better way.

Code: (Demo: https://3v4l.org/dU09B )

$currencies = [
    'mxn' => [
        'sign'  => '$',
        'plans' => [
            'tiny'   =>  429,
            'small'  => 1319,
            'medium' => 3399,
            'big'    => 6669
        ]
    ],
    'usd' => [
        'sign'  => '$',
        'plans' => [
            'tiny'   =>  29,
            'small'  => 319,
            'medium' => 399,
            'big'    => 669
        ]
    ]
];

$currency='mxn';

if(!isset($currencies[$currency])){
    echo "$currency was not found in the currencies array.";
}else{
    echo "In $currency:\n";
    foreach($currencies[$currency]['plans'] as $size=>$price){
        echo "\t$size costs {$currencies[$currency]['sign']}$price\n";
    }
}

Output:

In mxn:
    tiny costs $429
    small costs $1319
    medium costs $3399
    big costs $6669

Explanation:

Because the currency names will logically be unique, you can reduce the overall size of your lookup array by declaring the currency names as subarray keys.

If you were to store this same data in a database, you would assign the currency name as the Primary Key.

As a matter of improved efficiency and direct coding, the new array structure will allow you to swiftly determine if your desired currency exists in the multi-dimensional array using isset().

Your question title asks: Loop into an array if I have a key in PHP I take this to mean that it is possible that the needle may not be in the haystack. Maybe this is not what you mean, but this consideration is important to build into your code.

Verifying that the key exists in the multidimensional array is essential (to avoid a Notice ftom php) before attempting to use the key.


If you are not able or interested in modifying your data structure, then be sure that you employ two pieces of best practice:

  1. break your searching foreach loop after your currency is found; or call array_search() which does this for you. Iterating the remaining items in your array after you've received the desired data is wasteful/inefficient.

  2. Write a condition that handles the possibility of the currency not being found. Because trying to access: $currencies[false] isn't going to work out well for you.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136