Skip to content
Advertisement

php traverses the array to get the corresponding value

I have two variables, number and user, which are not fixed. When I iterate through the array below, how do I determine if the number and user criteria are met to get the appropriate ID

Example:

If the value of amount is 100 and the value of user is 6, then the obtained ID is 1;

If the value of amount is 50000 and the value of user is 10, the obtained ID is also 1;

If the value of amount is 50000 and the value of user is 12, the obtained ID is also 2;

Why do I always get 1?

$amount = 10000;
$user = 12;
$id = null;
foreach ($array as $item) {
  if ($amount >= $item['value']['amount']['min'] && $user >= $item['value']['user']['min']) {
    $id = $item['id'];
    break;
  }
}
echo $id;

The array as follows

$array = [
  [
    'id'    => 1,
    'value' => [
      'amount' => [
         'min' => 100,
         'max' => 9999 
       ],
      'user'   => [
         'min' => 5,
         'max' => 10 
       ],
      'ratio'  => 10
    ]
  ],
  [
    'id'    => 2,
    'value' => [
      'amount' => [
         'min' => 10000,
         'max' => 49999 
       ],
      'user'   => [
         'min' => 11,
         'max' => 30 
       ],
      'ratio'  => 30
    ]
  ],
  [
    'id'    => 3,
    'value' => [
      'amount' => [
         'min' => 50000,
       ],
      'user'   => [
         'min' => 50,
       ],
      'ratio'  => 35
    ]
  ] 
];

Advertisement

Answer

You need to check the amount is in between min and max value of amount as well as the user is in between min and max value of the user.

eg: ($min <= $value) && ($value <= $max)

<?php
$array = [
    [
        'id'    => 1,
        'value' => [
            'amount' => [
                'min' => 100,
                'max' => 9999
            ],
            'user'   => [
                'min' => 5,
                'max' => 10
            ],
            'ratio'  => 10
        ]
    ],
    [
        'id'    => 2,
        'value' => [
            'amount' => [
                'min' => 10000,
                'max' => 49999
            ],
            'user'   => [
                'min' => 11,
                'max' => 30
            ],
            'ratio'  => 30
        ]
    ],
    [
        'id'    => 3,
        'value' => [
            'amount' => [
                'min' => 50000,
            ],
            'user'   => [
                'min' => 50,
            ],
            'ratio'  => 35
        ]
    ]
];



function in_range($min, $max, $value)
{
    return ($min <= $value) && ($value <= $max);
}

function getId($amount, $user, $array)
{
    $id = 0;
    foreach ($array as $v) {
        // if min value null then initialize as 0
        $min_amount = $v['value']['amount']['min'] ?? 0;
        // if max value null then initialize as the maximum value which PHP holds
        $max_amount = $v['value']['amount']['max'] ?? PHP_INT_MAX;

        $min_user = $v['value']['user']['min'] ?? 0;
        $max_user = $v['value']['user']['max'] ?? PHP_INT_MAX;
        if (in_range($min_amount, $max_amount, $amount) && in_range($min_user, $max_user, $user)) {
            $id = $v['id'];
            break;
        }
    }

    return $id;
}

var_dump( getId(100, 6, $array) );
var_dump( getId(20000, 10, $array) );
var_dump(getId(20000, 12, $array));

Ref:

How to check if an integer is within a range of numbers in PHP?

https://www.php.net/manual/en/reserved.constants.php#constant.php-int-max

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement