简体   繁体   English

数组最高值还是创建下一个值?

[英]Array highest value or create next value?

I have an array of names and id's. 我有一组名称和ID。

Array(
    0=>array(
        name=>New York, 
        id=>45763448349, 
        can=0
    ),
    1=>array(
        name=>New York2, 
        id=>45763464566, 
        can=0
    ),
    3=>array(
        name=>New York3, 
        id=>45763464854, 
        can=0
    ),
    4=>array(
        name=>New York4, 
        id=>45763464955, 
        can=0
    ),
    5=>array(
        name=>New York5, 
        id=>45763464745, 
        can=0
    ),
    6=>array(
        name=>New York6, 
        id=>45763464235, 
        can=1
    )
)

For example (there will be different names (not all New York)) and I have an x variable with a name (for example x=New York) what I need to do is find the highest name and get the id and then make sure can is 1 otherwise specific the next highest name. 例如(会有不同的名称(并非全是纽约)),并且我有一个带有名称的x变量(例如x = New York),我需要做的就是找到最高名称并获取ID,然后确保可以是1,否则指定第二个大名。

For example let's assume that I have x=New York so I look through the array and find that the highest array name is New York6. 例如,假设我有x = New York,那么我遍历该数组并发现最高的数组名称是New York6。 I see that can=1 so I just get the id. 我看到can = 1,所以我得到了ID。 Now if can != 1 then I could specific New York7. 现在,如果可以= 1,那么我可以指定纽约。

NOTE: This array will contain many different values (not all New York, I just used those for an example) 注意:此数组将包含许多不同的值(不是所有的纽约,我只是使用这些作为示例)

Now how on earth would I even get started? 现在我到底该如何开始?

In code: 在代码中:

function city_cmp($record_a, $record_b) {  
  return strcmp($record_a["name"], $record_b["name"];
}

usort($cities, 'city_cmp');
end($cities);
$city = current($cities);
do {
    if ($city["can"] === 1) { 
         $can_city = $city;
         break;
    }

} while ($city = prev($cities));

Note: optionally, you could skip the break and make $can_city part of your while condition. 注意:(可选)您可以跳过该break ,并将$can_city为while条件的一部分。 Some prefer that as it can make the loop exit condition clearer. 有些人更喜欢这样做,因为它可以使循环退出条件更清晰。

In quick steps: 快速步骤:

  1. usort() the array usort()数组

  2. use end() to point at the end of the array and prev() until you find a can=1. 使用end()指向数组和prev()的末尾,直到找到can = 1。

Since it's a multi-dimensional array (I'll call it $array) you probably have to loop through it once to find relevant data. 由于它是一个多维数组(我将其称为$ array),因此您可能必须循环遍历一次才能找到相关数据。

$highest_id = array('id' => 0, 'count' => 0, 'can' => 0);
for each ( $array AS $key => $data )
{
  if ( substr($data['name'],0,strlen($x)) == $x )
  {
    $current_count = (int) substr($data['name'],strlen($x));
    if ( $current_count > $highest_id['count'] )
    {
      $highest_id = array('id' => $data['id'], 'count' => $current_count, 'can' => $data['can']);
    }
  }
}
if ( $highest_id['can'] == 1 )
{
  $id = $highest_id['id'];
}
else
{
 // can is not 1 => specify new $x
}

Something like this: 像这样:

//$my_array is your array of values
//$string = 'New York'; your search term
$id = false;
for($i = count($my_array) - 1; $i >= 0; $i++) {
    if($id === false) {
        if(strpos($my_array[$i]['name'], $string) !== false && $my_array[$i]['can'] == 1) {
            $id = $my_array[$i]['id'];
        }
    }
}
//Insert it if no id was found
if($id === false) {
    $id = 'something';
    $can = 1;
    array_push($my_array, array('name' => $string, 'id' => $id, 'can' => $can));
}

Does that work? 那样有用吗?

edit (In case it's not obvious, $string is whatever you are searching for) 编辑 (以防万一, $string是您要搜索的内容)

Start by dividing your task into smaller ones. 首先将任务划分为较小的任务。 The first filter criterion seems to be can=1 . 第一个过滤条件似乎是can=1 Let's check that with array_filter . 让我们用array_filter进行检查。

$ar = array(array('name'=>'New York', 'id'=>45763448349, 'can'=>1), array('name'=>'New York2', 'id'=>45763464566, 'can'=>0), array('name'=>'New York3', 'id'=>45763464854, 'can'=>0), array('name'=>'New York4', 'id'=>45763464955, 'can'=>0), array('name'=>'New York5', 'id'=>45763464745, 'can'=>0), array('name'=>'New York6', 'id'=>45763464235, 'can'=>1), array('name'=>'New Yorkistan', 'id'=>0, 'can'=1));
$ar = array_filter($ar, function($el) {return $el['can'];});
// Now $ar contains the entries 'New York', 'New York2', and 'New Yorkistan'

Now, filter out the entries which do not match New York (and a number). 现在,筛选出与New York (和数字)不匹配的条目。

$ar = array_filter($ar, function($el) {
    $search = 'New York';
    $name = $ar['name'];
    $potentialNumber = substr($name, strlen($search));
    return ((substr($name, 0, strlen($search)) == $search) && // starts with NY
            ($potentialNumber == '' || is_numeric($potentialNumber)); // [0-9]*
});
// $ar contains only 'New York' and 'New York6' now

Now, you just need some sorting with usort and pick the first element: 现在,您只需要使用usort进行排序并选择第一个元素:

usort($ar, function($el1, $el2) {
    $id1 = $el1['id']; $id2 = $el2['id'];
    if ($id1 == $id2) return 0;
    return $id1 < $id2 ? 1 : -1;
});
if (count($ar) > 0) {
    $result = $ar[0];
} else {
    // Nothing matched your criteria
}

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

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