11

PHPでは、要素を配列内の特定の位置に移動することにより、連想配列を並べ替える機能が必要です。並べ替える必要はありません。私が選んだ並べ替えだけです。

例として、次のような連想配列があるとします。

array(
 'a' => 'Element A',
 'b' => 'Element B',
 'c' => 'Element C',
);

あるケースでは、C を B の前に移動して、次の結果を得たい場合があります。

array(
 'a' => 'Element A',
 'c' => 'Element C',
 'b' => 'Element B',
);

または別のケースでは、C を A の前に移動して、次の結果を得たい場合があります。

array(
 'c' => 'Element C',
 'a' => 'Element A',
 'b' => 'Element B',
);

私が示そうとしているのは、単に「この配列要素をこの他の配列要素の前に移動したい」または「この配列要素を移動して、この他の配列の後に来るようにしたい」という単純な方法ですエレメント'

うまくいけば、これは理にかなっています!

これを手伝ってくれる人に前もって感謝します

4

7 に答える 7

11

カスタムの並べ替えの場合、たとえば、キーの目的の順序である配列を作成し、値をそれらに関連付けることができます。例:

$input = array("a"=>"Element A","b"=>"Element B","c"=>"Element C");
$order = array("c","a","b");
$out = array();
foreach($order as $k) {
    $out[$k] = $input[$k];
}

の要素は$out、指定された順序になります。

于 2012-07-01T14:11:44.330 に答える
2

2 つの値を交換する場合は、次のような関数を作成できます。

function array_swap($key1, $key2, $array) {
        $newArray = array ();
        foreach ($array as $key => $value) {
            if ($key == $key1) {
                $newArray[$key2] = $array[$key2];
            } elseif ($key == $key2) {
                $newArray[$key1] = $array[$key1];
            } else {
                $newArray[$key] = $value;
            }
        }
        return $newArray;
    }
于 2012-07-01T14:12:05.860 に答える
1

ここには難しい方法がたくさんあります :) 実際、 のキーの保存機能を活用できますarray_slice()

$new_element = array('new_key' => 'value');

// if needed, find the insertion index by key
$index = array_search('key to search', array_keys($old_array));

// add element at index (note the last array_slice argument)
$new_array = array_slice($old_array, 0, $index+1, true) + $new_element + array_slice($old_array, $index+1, null, true);
于 2016-07-29T10:01:35.593 に答える
0

array_splice残念ながら、連想配列では機能しないため、次のように少し面倒です。

$keys = array_keys($arr);
$values = array_values($arr);

$keyIndex = array_search($someKey, $keys);
array_splice($keys, $keyIndex, 1);
array_splice($values, $keyIndex, 1);

$insertIndex = 1;
array_splice($keys, $insertIndex, 0, array($someKey));
array_splice($values, $insertIndex, 0, array($arr[$someKey]));

$arr = array_combine($keys, $values);
于 2012-07-01T14:14:02.670 に答える
0

luky の回答が一番気に入っていますが、すべてのキーを指定する必要があります。ほとんどの場合、配列の先頭にキーのサブセットを並べたいだけです。この機能は次の場合に役立ちます。

function reorder_assoc_array(
  $cur,   // current assoc array 
  $order  // array conaining ordered (subset of) keys in $cur
) {
  $result = [];
  // first copy ordered key/values to result array
  foreach($order as $key) {
    $result[$key] = $cur[$key];
    // unset key in original array
    unset($cur[$key]);
  }
  // ... then copy all remaining keys that were not given in $order
  foreach($cur as $key => $value) {
    $result[$key] = $value;
  }
  return $result;
}

例:

$assoc_arr = [
  'b' => 'bbb',
  'a' => 'aaa',
  'c' => 'ccc',
  'd' => 'ddd'
];

// suppose we want to swap the first two keys and leave the remaining keys as is
$assoc_arr = reorder_assoc_array($assoc_arr, ['a', 'b']);

// ... the order of keys is now a, b, c, d
于 2020-09-23T07:44:11.697 に答える