简体   繁体   English

如何获取具有特定值的数组的关联索引

[英]how to get associative indexes of an array which has a specific value

    {
    [2012-05-23] => 1
    [2012-05-24] => 1
    [2012-05-25] => 1
    [2012-05-26] => 1
    [2012-05-27] => 1
    [2012-05-28] => 11
    [2012-05-29] => 11
    [2012-05-30] => 12
    }

for example this is my array and i just want to get the indexes of array which has a value greater then 10 例如,这是我的数组,我只想获得数值大于10的数组索引

so in this case the answer must be sounded like that 所以在这种情况下,答案必须听起来像那样

    {
    [0] => 2012-05-28
    [1] => 2012-05-29
    [2] => 2012-05-30
     }

someone like this! 像这样的人!

$new_array = array();
foreach ($array as $r => $a)
{
    if ($a>10)
    $new_array[] = $r;
}
$array=//Your array in original post.
$newarray=array();
foreach ($array as $key=>$val) {
    if ($key>10) {
        $newarray[]=$key;
    }
}    

If $array is your first array : 如果$array是你的第一个数组:

$new_array = array();
foreach($array as $key => $value) {
    if( $value > 10 ) {
        $new_array[] = $key;
    }
}

Try this (assuming that the array in <pre> is called $dates ): 试试这个(假设<pre>中的数组被称为$dates ):

$result = array();
foreach($dates as $date => $value) {
    if($value > 10) $result[] = $date;
}

print_r($result);
$testData = array ('2012-05-23' => 1,
                   '2012-05-24' => 1, 
                   '2012-05-25' => 1,
                   '2012-05-26' => 1, 
                   '2012-05-27' => 1, 
                   '2012-05-28' => 11, 
                   '2012-05-29' => 11, 
                   '2012-05-30' => 12,
                   );
$testNeedle = 10;

$result = array_filter($testData, 
                       function($arrayEntry) use ($testNeedle) {
                           return $arrayEntry > $testNeedle; 
                       }
);
$result = array_keys($result);

var_dump($result);

This code will do the job. 这段代码可以完成这项工作。

$array1 = array(
'2012-05-23' => 1,
'2012-05-24' => 1,
'2012-05-25' => 1,
'2012-05-26' => 1,
'2012-05-27' => 1,
'2012-05-28' => 11,
'2012-05-29' => 11,
'2012-05-30' => 12
);
$result = (array_keys(array_filter($array1, "select")));
function select($var)
{    return($var > 10);     }

You can also use a for loop with array_push in an empty array to skip the callback function. 您还可以在空数组中使用带有array_push的for循环来跳过回调函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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