English 中文(简体)
PHP:获得现有阵列钥匙?
原标题:PHP: get current array key?
$array = ( 
    array( 1231415 =>array( foo => bar ,  test => 1)),
    array( 32434 =>array( foo => bar ,  test =>  0 )),
    array( 123244 =>array( foo => bar ,  test => 0)),
    array( 193928 =>array( foo => bar ,  test => 1))
);

我有一阵列,有(许多)随机钥匙,即身份证号码。 如果测试=1,我需要测试每一阵列,因此我提供了<条码>。

foreach ($array as $sub) {
  if ($sub[ test ] ==  1  ) {
     echo  User:   . $sub .   has test = 1 ;
  }
}

这项工程虽然使用户返回:Array测试=1

我如何从地球上获得哪一个身份识别号码(即随机编号)的检测标准?

我尝试做如下尝试:$array as$sub=>$ Value,但出于某种原因,它只是使foreach不起作用。 谢谢!

问题回答

使用<条码>代谢

foreach ($array as $key => $sub) {
  if ($sub[ test ] ==  1  ) {
    echo  User:   . $key .   has test = 1 ;
  }
}

假设数据为:

$array = array(
   1234  => array( test  => 1),
   5678  => array( test  => 2)
);

如果你需要保留现在的数据,那么你就不得不使用更多的数据:

foreach ($array as $item) {
  list($key, $info) = $item;
  if ($info[ test ] == 1) {
    echo  User:   . $key .   has test = 1 ;
  }
}

你的法典有2个问题。

(1) 你的阵容声明稍微夸张。 为此:

$array = array( 
    1231415 =>array( foo => bar ,  test => 1),
    32434 =>array( foo => bar ,  test => 0),
    123244 =>array( foo => bar ,  test => 0),
    193928 =>array( foo => bar ,  test => 1)
);

2) 在您的<条码>上,你重掉了id钥匙。 为此:

foreach ($array as $id => $sub) {
    if ($sub[ test ] == 1) {
        echo "User: " . $id . " has test = 1
";
    }
}

在我测试上述产出时:

User: 1231415 has test = 1
User: 193928 has test = 1




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签