dsvs50005 2017-01-04 21:06
浏览 14

如何使用PHP从多个值等于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
        )

)
  • 写回答

1条回答 默认 最新

  • douxie0824 2017-01-04 21:28
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?