doujiongqin0687 2012-05-01 15:11
浏览 64
已采纳

从php多维数组中选择数据

I have a multidimensional array as follows:

$a = array (
   #id           num  name      full name
    0 => array ( 65, 'name-1', 'Name One' ),
    1 => array ( 76, 'name-2', 'Name Two' ),
    2 => array ( 82, 'name-3', 'Name Three' )
);

Now, if I want to select 'name-1' (name) I use $a[0][1] and to select 'Name Three' (full name) I use $a[2][2].

So, I can select the number, the name or the full name if I have the id in the array.

But what if I have the number or the name and I want to select the full name? how could I get it?

Thanks.

  • 写回答

3条回答 默认 最新

  • dqtm8504 2012-05-01 15:14
    关注

    If with id you mean the num:

    $num = // num you want to find
    foreach ($a as $b) {
        if ($b[0] == $num) {
            $name = $b[1];
            $full = $b[2];
            break;
        }
    }
    

    If with id you mean #ID then:

    $id = // id you want to find
    $num = $a[$id][0];
    $name = $a[$id][1];
    $full = $a[$id][2];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图