简体   繁体   中英

How can I use PHP to select data from an array where multiple value equals x?

How can I use PHP, to select all membership plans where 'type_id' = x and level_id = x or term_id = x? For example, I want to return all Single (type_id = 1) and Basic (level_id = 1) membership plans. I'm trying to avoid additional SQL queries, as the data is already stored in an array, it's just not easy for me to lookup the data I want.

I'm also trying to avoid a ton of foreach loops. For example, I recently tried the following which sort of works... but what if I have more types than I do levels. There has to be a better way, right?

<?php
$available_types = array();
$available_levels = array();
foreach ($membership_plans as $plan) {
    $available_levels[$plan->level_id] = $plan->level_name;
    $available_types[$plan->type_id] = $plan->type_name;
}
$available_types = array_unique($available_types);// Unique Types
$available_levels = array_unique($available_levels);// Unique Levels
foreach ($available_levels as $level_id => $level_name) {// foreach level
    foreach ($available_types as $type_id => $type_name) {// list each type with level
        echo $level_name.' - '.$type_name;
        // find plans tht match type and level
        foreach ($plans as $plan) {
            // More code goes here, but it didn't work out
        }
    }
}
?>

I have a membership plan array that looks like the following:

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [type_id] => 1
            [type_name] => Single
            [level_id] => 1
            [level_name] => Basic
            [term_id] => 1
            [term_name] => MTM
            [name] => SB_1
            [description] =>  
            [rate] => 58
            [hidden] => 1
            [sort] => 1000
            [active] => 1
        )

    [1] => stdClass Object
        (
            [id] => 2
            [type_id]`enter code here` => 1
            [type_name] => Single
            [level_id] => 1
            [level_name] => Basic
            [term_id] => 2
            [term_name] => 12 Months
            [name] => SB_12
            [description] =>  
            [rate] => 56
            [hidden] => 0
            [sort] => 1110
            [active] => 1
        )

    [2] => stdClass Object
        (
            [id] => 14
            [type_id] => 1
            [type_name] => Single
            [level_id] => 3
            [level_name] => Premier
            [term_id] => 2
            [term_name] => 12 Months
            [name] => SP_12
            [description] =>  
            [rate] => 76
            [hidden] => 0
            [sort] => 1310
            [active] => 1
        )
    [3] => stdClass Object
        (
            [id] => 16
            [type_id] => 1
            [type_name] => Single
            [level_id] => 3
            [level_name] => Premier
            [term_id] => 4
            [term_name] => 28 Months
            [name] => SP_28
            [description] =>  
            [rate] => 65.43
            [hidden] => 1
            [sort] => 1330
            [active] => 1
        )
    [4] => stdClass Object
        (
            [id] => 19
            [type_id] => 2
            [type_name] => Dual
            [level_id] => 1
            [level_name] => Basic
            [term_id] => 1
            [term_name] => MTM
            [name] => DB_1
            [description] =>  
            [rate] => 81
            [hidden] => 1
            [sort] => 2100
            [active] => 1
        )

)

You probably want to use array_filter

array_filter(
  $arr, 
  function($v) {
    return (
      $v->type_id == 'x' 
      && ($v->level_id == 'x' || $v->term_id == 'x')
    )
  }
 )

if the x needs to be variable you have the use function($v) use ($var) { And maybe its better to check if the type_id exists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM