dpwqicw157673 2015-08-20 03:40
浏览 21

具有多个“主键”列的数组

I create an array like this:

$table = Array();
array_push($table, Array('item' => 1, 'storage' = 1, 'qtd' = 0) );
array_push($table, Array('item' => 1, 'storage' = 2, 'qtd' = 4) );
array_push($table, Array('item' => 2, 'storage' = 1, 'qtd' = 78) );
array_push($table, Array('item' => 3, 'storage' = 2, 'qtd' = 10) );

I need to search if i have some item in some storage.. For example in a sql query i do like "... where item = 1 and storage = 2"

How can i search that way in the array, to get the "qtd" value?

Thanks!

  • 写回答

2条回答 默认 最新

  • doulou0882 2015-08-20 03:54
    关注
    $table = Array();
    
    //populate $table array this way
    $table[1]=array('item' => 1, 'storage' = 1, 'qtd' = 0);
    $table[1]=array('item' => 1, 'storage' = 2, 'qtd' = 4);
    $table[2]=array('item' => 2, 'storage' = 1, 'qtd' = 78);
    $table[3]=array('item' => 3, 'storage' = 2, 'qtd' = 10);
    
    //to prevent over-writting use in_array() function
    if(!in_array(1 /*item*/ ,array_keys($table))){
        $table[1]=array('item' => 1, 'storage' = 1, 'qtd' = 0);
    }
    
    //the following will not be inserted as we have already a value at index=1
    if(!in_array(1,array_keys($table)),){
        $table[1]=array('item' => 1, 'storage' = 2, 'qtd' = 4);
    }     
    

    EDITED**** to get highest max index in $table array

    $current_primary_key =   max(array_keys($table));
    $next_primary_key = $current_primary_key + 1;
    
    评论

报告相同问题?

悬赏问题

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