将值分配给数组

时间:2014-05-06 06:18:01

标签: php arrays

我有以下数组结构。使用变量 $ sortie

Array
    (
        [0] => 480
        [1] => 483
        [2] => 497
        [3] => 481
        [4] => 478
        [5] => 475
        [6] => 476
        [7] => 477
        [8] => 498
        [9] => 496
        [10] => 502
        [11] => 499
        [12] => 494
        [13] => 503
        [14] => 493
        [15] => 500
        [16] => 484
        [17] => 501
        [18] => 495
        [19] => 485
        [20] => 489
        [21] => 490
        [22] => 488
        [23] => 487
        [24] => 486
    )

我正在尝试通过使用以下代码在新阵列上分配它们来实现某些目标。

        $release = array();
        foreach ($sortie as $key_true => $value_true) {
            $release[$key_true] = $value_true;
            echo $key_true.'---'.$value_true.'<br>';
        }

到目前为止,根据 $ sortie

的正确顺序,回声结果正如预期的那样

$ release 数组未遵循相同的顺序,未将 $ key_true 分配给 $ release 数组。非常感谢为什么要这样做。

修改 对不起,差点忘了, $ release 的结果是值是原始键和原始值

        Array
    (
        [0] => 0---480
        [1] => 1---483
        [2] => 10---502
        [3] => 11---499
        [4] => 12---494
        [5] => 13---503
        [6] => 14---493
        [7] => 15---500
        [8] => 16---484
        [9] => 17---501
        [10] => 18---495
        [11] => 19---485
        [12] => 2---497
        [13] => 20---489
        [14] => 21---490
        [15] => 22---488
        [16] => 23---487
        [17] => 24---486
        [18] => 3---481
        [19] => 4---478
        [20] => 5---475
        [21] => 6---476
        [22] => 7---477
        [23] => 8---498
        [24] => 9---496
    )

2 个答案:

答案 0 :(得分:0)

最佳做法是将数组更改为此结构:

$sortie = [480, 483, ...]; 
$release = [];
foreach($sortie as $val) {
  $release[$val] = true;
}

然后你可以使用php array sorting functions

答案 1 :(得分:0)

尝试输入$ release == array();,就分配而言,它可能会为你工作