【问题标题】:fixing the array to numeric order将数组固定为数字顺序
【发布时间】:2012-09-05 20:03:02
【问题描述】:

我有一个这样的数组

array
  0 => 
    array
      'title' => string 'Last Name' (length=9)
      'price' => string '0.0000' (length=6)
      'price_type' => string 'fixed' (length=5)
      'is_required' => int 1
  1 => 
    array
      'title' => string 'Title 1' (length=6)
      'price' => string '0.0000' (length=6)
      'price_type' => string 'fixed' (length=5)
      'is_required' => int 0
  2 => 
    array
      'title' => string 'Title 10' (length=7)
      'price' => string '0.0000' (length=6)
      'price_type' => string 'fixed' (length=5)
      'is_required' => int 0
  3 => 
    array
      'title' => string 'Title 11' (length=7)
      'price' => string '0.0000' (length=6)
      'price_type' => string 'fixed' (length=5)
      'is_required' => int 0
  4 => 
    array
       'title' => string 'Title 12' (length=7)
      'price' => string '0.0000' (length=6)
      'price_type' => string 'fixed' (length=5)
      'is_required' => int 0
  5 => 
    array
       'title' => string 'Title 2' (length=6)
      'price' => string '0.0000' (length=6)
      'price_type' => string 'fixed' (length=5)
      'is_required' => int 0
  6 => 
    array
        'title' => string 'Title 3' (length=6)
      'price' => string '0.0000' (length=6)
      'price_type' => string 'fixed' (length=5)
      'is_required' => int 0
  7 => 
    array
       'title' => string 'Title 4' (length=6)
      'price' => string '0.0000' (length=6)
      'price_type' => string 'fixed' (length=5)
      'is_required' => int 0
  8 => 
    array
      'title' => string 'Title 5' (length=6)
      'price' => string '0.0000' (length=6)
      'price_type' => string 'fixed' (length=5)
      'is_required' => int 0
  9 => 
    array
      'title' => string 'Title 6' (length=6)
      'price' => string '0.0000' (length=6)
      'price_type' => string 'fixed' (length=5)
      'is_required' => int 0
  10 => 
    array
      'title' => string 'Title 7' (length=6)
      'price' => string '0.0000' (length=6)
      'price_type' => string 'fixed' (length=5)
      'is_required' => int 0
  11 => 
    array
       'title' => string 'Title 8' (length=6)
      'price' => string '0.0000' (length=6)
      'price_type' => string 'fixed' (length=5)
      'is_required' => int 0
  12 => 
    array
        'title' => string 'Title 9' (length=6)
      'price' => string '0.0000' (length=6)
      'price_type' => string 'fixed' (length=5)
      'is_required' => int 0

所以问题是在数组中我有标题。当我执行 foreach 循环时,标题显示为

LAST NAME:
Title 1:
Title 10:
Title 11:
Title 12:
Title 2:
Title 3:
Title 4:
Title 5:
Title 6:
Title 7:
Title 8:
Title 9:

您可以注意到,标题 10 出现在 1 之后,并且它的数字顺序不正确。我怎样才能在 php.ini 中解决这个问题。 谢谢

尝试过 $titles = 数组(); foreach ($product->custom_options as $key => $row) { $titles[$key] = $row['title']; } var_dump(array_multisort($titles, SORT_DESC, $product->custom_options));

【问题讨论】:

标签: php cakephp


【解决方案1】:

尝试将usortstrnatcmp 结合使用。像这样的:

usort($array, function($lhs, $rhs){ 
    return strnatcmp($lhs['title'], $rhs['title']);
});

【讨论】:

    【解决方案2】:

    您永远不会对数组进行排序,它是按该顺序显示的,因为您以这种方式插入项目,您首先需要对数组进行排序,但不是以字母数字方式。尝试使用另一个具有整数值的字段。

    【讨论】:

      【解决方案3】:

      问题很可能与数组的生成有关,而不是像现在这样修复它。从我看到你有一个标题作为第一个元素,从那时起你有没有正确“排序”的元素。

      在不知道确切要求的情况下,根据您发布的内容,一种“排序”的方法是:

      // $data array is the existing array
      $new_array = array();
      foreach ($data as $item) {
          if ($item['title'] == 'Last Name') {
              $new_array[0] = $item;
          } 
          else
          {
              $key = intval(trim(str_replace("Title ", "", $item['title'])));
              $new_array[$key] = $item;
          }
      }
      
      ksort($new_array);
      

      生成的数组以键 0 作为标题的元素,其他一切都取决于“标题 XX”,其中 XX 是一个数字(取自标题元素

      HTH

      【讨论】:

        【解决方案4】:

        您可以直接使用 php 定义的natsort function 使用“自然顺序”算法对数组进行排序。

        只需编写以下代码即可。

        natsort($array);
        

        【讨论】:

          猜你喜欢
          • 2017-08-08
          • 1970-01-01
          • 2013-04-03
          • 2011-09-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-03-09
          相关资源
          最近更新 更多