0

私はこのような配列を持っています

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で修正できますか。ありがとう

$titles = array(); を試しました。foreach ($product->custom_options as $key => $row) { $titles[$key] = $row['title']; } var_dump(array_multisort($titles, SORT_DESC, $product->custom_options));

4

4 に答える 4

2

usortstrnatcmpを組み合わせてみてください。このようなもの:

usort($array, function($lhs, $rhs){ 
    return strnatcmp($lhs['title'], $rhs['title']);
});
于 2012-09-05T20:07:18.190 に答える
0

この問題は、現在のように修正するのではなく、アレイの生成にある可能性があります。私が見るところ、最初の要素としてヘッダーがあり、それ以降、適切に「ソート」されていない要素があります。

正確な要件を知らなくても、投稿内容に基づいて、これを「ソート」する方法は次のようになります。

// $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

于 2012-09-05T20:11:57.097 に答える
0

「自然順序」アルゴリズムを使用して配列をソートするPHP 定義のnatsort 関数を直接使用できます。

以下のコードを書くだけで完了です。

natsort($array);
于 2012-09-06T05:14:47.587 に答える
0

配列をソートすることはありません。アイテムをそのように挿入したため、その順序で表示されています。最初に配列を並べ替える必要がありますが、英数字順ではありません。整数値を持つ別のフィールドを使用してみてください。

于 2012-09-05T20:07:56.150 に答える