从范围为数组的数组中选择值?

时间:2018-01-22 03:21:54

标签: php arrays

我在php上有一个数组,它会返回很多key->值,如下所示:

Array 
 [0] => Array 
    ( 
        [value] => 405 
        [information] => some information1
    ) 
 [1] => Array 
    ( 
        [value] => 500
        [information] => some information2
    ) 
 [2] => Array 
    ( 
        [value] => 700
        [information] => some information3
    ) 

值是数字,我需要收集所有值,检查第一个将是> = $ determinedvalue然后返回值"信息"对于这个确切的数组,这甚至可能吗?我知道我可以这样做,如果我在我的数据库上创建临时表,但我不想。

更清楚的是,当我的价值是" 430"它将返回我"一些信息2"。

我在google上搜索了很多但是现在我不知道这是否可能。

感谢任何帮助。

3 个答案:

答案 0 :(得分:0)

这是一个如何做到这一点的示例。代码中的注释可用于解释执行。

// Sample Array
$arr = array( 
 array
    ( 
        "value" => 405,
        "information" => "some information1"
    ),
 array
    ( 
        "value" => 500,
        "information" => "some information2"
    ),
 array
    ( 
        "value" => 700,
        "information" => "some information3"
    ) 
);

// Sample Number
$numberToCheck = 430;

// Sub Array To Assign
$subArray = array();

// Loop Through Outer Array
for ( $i = 0; $i < sizeof($arr); $i++)
{

    // If Value In Array > Number Check
    if ( $arr[$i]['value'] >= $numberToCheck )
    {
        $subArray = $arr[$i]; // Assign Sub Array
        $i = sizeof($arr); // This is Used to Exit For Loop
    }


}
print_r($subArray); // Print

答案 1 :(得分:0)

使用可运行代码:https://tech.io/snippet/QJ93AwV

代码片段与评论:

<?php

/**
 * Helper class for stable sort alogithms
 * @see https://github.com/vanderlee/PHP-stable-sort-functions
 * Class StableSort
 */
class StableSort
{
    static public function usort(array &$array, $value_compare_func)
    {
        $index = 0;
        foreach ($array as &$item) {
            $item = array($index++, $item);
        }

        $result = usort($array, function ($a, $b) use ($value_compare_func) {
            $result = call_user_func($value_compare_func, $a[1], $b[1]);
            return $result == 0 ? $a[0] - $b[0] : $result;
        });

        foreach ($array as &$item) {
            $item = $item[1];
        }

        return $result;
    }

}

$array = [
    // added for test sorting
    [
        'value' => 9999,
        'information' => 'some information-1',
    ],
    [
        'value' => 1200,
        'information' => 'some information0',
    ],
    // \added for test sorting
    [
        'value' => 405,
        'information' => 'some information1',
    ],
    // added for test sorting stability
    [
        'value' => 405,
        'information' => 'some information1.2',
    ],
    [
        'value' => 405,
        'information' => 'some information1.1',
    ],
    // \added for test sorting stability
    [
        'value' => 500,
        'information' => 'some information2',
    ],
    [
        'value' => 700,
        'information' => 'some information3',
    ],
];

// sort array
$determinedvalue = 430;
StableSort::usort($array, function ($item1, $item2) {
    if ($item1['value'] == $item2['value']) return 0;
    return $item1['value'] < $item2['value'] ? -1 : 1;
});

// then select the first element by condition
$res = null;
foreach($array as $v) {
    if($v['value'] >= $determinedvalue) {
        $res = $v['information'];
        break;
    }
}

// for testing
print $res;

答案 2 :(得分:0)

Task task1 = Task.Run(async () =>  // <- marked async
{
    while (true)
    {
        InsertProcess(GameList.RO1);
        await Task.Delay(5000);
    }
});
Task task2 = Task.Run(async () =>  // <- marked async
{
    while (true)
    {
        InsertProcess(GameList.ROS);
        await Task.Delay(5000);
    }
});

Task.WaitAll(new []{task1, task2});