重新排列php数组;将索引设置为数组值

时间:2013-06-26 08:55:51

标签: php arrays sorting multidimensional-array arrangeoverride

我有一个数组,我添加到这篇文章的末尾。我需要将数组的'parent'值设置为父数组索引号.Arrays与父数组相关的父值idnumber value。

例如;

[131] => Array
    (
        [idnumber] => 171
        [name] => 9f146bfd1b1aa02612b95a0ae7b52f13
        [parent] => 21
    )

Parrent数组

[15] => Array
    (
        [idnumber] => 21
        [name] => 175d60205b36459d75d1cdda406f1ea4
        [parent] => 0
    )

所以新数组;

[131] => Array
    (
        [idnumber] => 171
        [name] => 9f146bfd1b1aa02612b95a0ae7b52f13
        [parent] => 15
    )

但对我来说这个问题很难; idnumbers必须是唯一的,但前22个数组(父值为0)idnumbers可以找到其余的数组idnumber(不多)。这使得我很难解决这个问题。我尝试了不同的方法,但每种方式效率都不高我想要的。

Array
(
    [0] => Array
        (
            [idnumber] => 433
            [name] => a2c8925220b6b1b0da3973396c0cd108
            [parent] => 0
        )

    [1] => Array
        (
            [idnumber] => 325
            [name] => e88e9c4a57d198c09c73e80b3ea3c6b4
            [parent] => 0
        )

    [2] => Array
        (
            [idnumber] => 327
            [name] => 844d3ef5110b9e64ea1f4cd4b90ca132
            [parent] => 0
        )

    [3] => Array
        (
            [idnumber] => 7758
            [name] => d1dfc50dedfe4ea1b7a149099a80e10c
            [parent] => 0
        )

    [4] => Array
        (
            [idnumber] => 328
            [name] => 8017101ae9cb183f0c2c574d439ecda8
            [parent] => 0
        )

    [5] => Array
        (
            [idnumber] => 331
            [name] => 49891f391525284e73a1675716d8e1e4
            [parent] => 0
        )

    [6] => Array
        (
            [idnumber] => 414
            [name] => 3e89cf3b3c688deab39cb60e5d1521af
            [parent] => 0
        )

    [7] => Array
        (
            [idnumber] => 7946
            [name] => c0833a1d36ae2be7f52a4f50466295ed
            [parent] => 0
        )

    [8] => Array
        (
            [idnumber] => 329
            [name] => 02186c71a6253223965a9ee70d2ed17f
            [parent] => 0
        )

    [9] => Array
        (
            [idnumber] => 19
            [name] => ecdf84f60437910c099f200be25cedf1
            [parent] => 0
        )

    [10] => Array
        (
            [idnumber] => 18
            [name] => f75e2a70527c6719cd2f21539036b4e2
            [parent] => 0
        )

    [11] => Array
        (
            [idnumber] => 415
            [name] => 21d7865f2427d65a664c10d38d24ea13
            [parent] => 0
        )

    [12] => Array
        (
            [idnumber] => 330
            [name] => bf2ac8c506387b8a3ad974d3a1a295ce
            [parent] => 0
        )

    [13] => Array
        (
            [idnumber] => 20
            [name] => b978fb4cb0d58479345d900ad3ca34e7
            [parent] => 0
        )

    [14] => Array
        (
            [idnumber] => 7757
            [name] => 0f66b5b894361329c9bdc60dd5ea7c8b
            [parent] => 0
        )

    [15] => Array
        (
            [idnumber] => 21
            [name] => 175d60205b36459d75d1cdda406f1ea4
            [parent] => 0
        )

    [16] => Array
        (
            [idnumber] => 416
            [name] => eacf58667eeafd9490d473b774ebb462
            [parent] => 0
        )

    [17] => Array
        (
            [idnumber] => 7756
            [name] => 9b84269ace836d2a320f8f3be3c5ea6a
            [parent] => 0
        )

    [18] => Array
        (
            [idnumber] => 7771
            [name] => 4f0cd7825aab84d0377f9e307bba2d71
            [parent] => 0
        )

    [19] => Array
        (
            [idnumber] => 23
            [name] => 9bfe6f10ea30411732eed8541639563f
            [parent] => 0
        )

    [20] => Array
        (
            [idnumber] => 429
            [name] => df3ef7626f9b0455d67443435c4e6dba
            [parent] => 0
        )

    [21] => Array
        (
            [idnumber] => 7890
            [name] => 15395b205bad7f3e36fc8b3b72746ab5
            [parent] => 0
        )

    [22] => Array
        (
            [idnumber] => 425
            [name] => 137220b2d62975d283b924a0ddd248c1
            [parent] => 0
        )

    [23] => Array
        (
            [idnumber] => 7
            [name] => 0e45a0ab15c868d9bc5d725968304076
            [parent] => 19
        )

    [24] => Array
        (
            [idnumber] => 8
            [name] => a8c99d65bb9395c6f744f12eb94fb119
            [parent] => 19
        )

    [25] => Array
        (
            [idnumber] => 9
            [name] => 3b84f212132ea63b3df46860a7b62af6
            [parent] => 19
        )

    [26] => Array
        (
            [idnumber] => 11
            [name] => 499a5ea53bb8f922740d6283b32e7c67
            [parent] => 19
        )

    [27] => Array
        (
            [idnumber] => 13
            [name] => 10d25ee7bc5439423a2cf1295c4a18c9
            [parent] => 19
        )

    [28] => Array
        (
            [idnumber] => 15
            [name] => 264b7390eabe5d84067da472c90b704d
            [parent] => 19
        )

    [29] => Array
        (
            [idnumber] => 17
            [name] => cf6ddee29dbbfa041bc1277abbee5fca
            [parent] => 19
        )

    [30] => Array
        (
            [idnumber] => 18
            [name] => 475e6246dbf5aa4774cfb6aed1f223a3
            [parent] => 19
        )

    [31] => Array
        (
            [idnumber] => 22
            [name] => d0c8be2ee968af2bd217946dc8d79cf5
            [parent] => 19
        )

    [32] => Array
        (
            [idnumber] => 23
            [name] => fbebe6735e37bf5afd82a178f694d823
            [parent] => 19
        )

    [33] => Array
        (
            [idnumber] => 24
            [name] => e93361e02cd866344592d85afa5eb0af
            [parent] => 19
        )

    [34] => Array
        (
            [idnumber] => 26
            [name] => e4368046a74c7366c3d7a34920950bd1
            [parent] => 18
        )

    [35] => Array
        (
            [idnumber] => 28
            [name] => e75118e2e9d35179ecde9d8d280af51a
            [parent] => 18
        )

    [36] => Array
        (
            [idnumber] => 29
            [name] => 7847fd5077032fc77651e4cea53b099e
            [parent] => 18
        )

    [37] => Array
        (
            [idnumber] => 30
            [name] => 11778423bba90c851a250b1e9f71386b
            [parent] => 18
        )

    [38] => Array
        (
            [idnumber] => 31
            [name] => 8558d19657e99ef6a71aaf6134a57355
            [parent] => 18
        )

    [39] => Array
        (
            [idnumber] => 32
            [name] => 58654b22b577ac46bce4c61ef40d10ca
            [parent] => 18
        )

    [40] => Array
        (
            [idnumber] => 33
            [name] => e7270a938e053eaff3c5fe9984174999
            [parent] => 18
        )

    [41] => Array
        (
            [idnumber] => 34
            [name] => 271da6055ffe18f43dbab615f2422692
            [parent] => 18
        )

    [42] => Array
        (
            [idnumber] => 35
            [name] => eedf03bea676eac48d8cd4e13645e0de
            [parent] => 18
        )

    [43] => Array
        (
            [idnumber] => 36
            [name] => f657ceca7ab3f19f170ac179eb2e7e92
            [parent] => 18
        )

    [44] => Array
        (
            [idnumber] => 38
            [name] => e5230a6a7239418050bd4cc47c3614ec
            [parent] => 18
        )

    [45] => Array
        (
            [idnumber] => 39
            [name] => 2531e2c5bd76ac0461740fbde55c3634
            [parent] => 18
        )

    [46] => Array
        (
            [idnumber] => 40
            [name] => 780b7625a5ec2e03730450a4f5e24b39
            [parent] => 18
        )

    [47] => Array
        (
            [idnumber] => 41
            [name] => 55d0234fcff0040ae944b4d3b7045737
            [parent] => 18
        )

    [48] => Array
        (
            [idnumber] => 42
            [name] => d627049101522353b8e611d2bbcd47e6
            [parent] => 18
        )

    [49] => Array
        (
            [idnumber] => 43
            [name] => 872facaf721796e97c5e0df6e4d5b93a
            [parent] => 18
        )

    [50] => Array
        (
            [idnumber] => 44
            [name] => bcd8ed29ca9fc66b12ffd8dab96bed1d
            [parent] => 18
        )

    [51] => Array
        (
            [idnumber] => 45
            [name] => 72d76372f293df54d9456434fea25149
            [parent] => 18
        )

    [52] => Array
        (
            [idnumber] => 46
            [name] => 692a547fb6fd4a143f8e3c9408f9f6e2
            [parent] => 20
        )

    [53] => Array
        (
            [idnumber] => 47
            [name] => 9ec462c2ad644aebe49d158f78358fd1
            [parent] => 20
        )

    [54] => Array
        (
            [idnumber] => 48
            [name] => 342875df8a48b77010f5781d89b1af27
            [parent] => 20
        )

    [55] => Array
        (
            [idnumber] => 49
            [name] => 33aba60927eea35fab311b23fc0a1bf4
            [parent] => 20
        )

    [56] => Array
        (
            [idnumber] => 50
            [name] => c4f64881b971c47195130f4c7d697b3e
            [parent] => 20
        )

    [57] => Array
        (
            [idnumber] => 51
            [name] => 50df2fe48c2e53f4f216e4f266439cf3
            [parent] => 20
        )

    [58] => Array
        (
            [idnumber] => 52
            [name] => b597f5537c334713fb944907eeb2d05a
            [parent] => 20
        )

    [59] => Array
        (
            [idnumber] => 53
            [name] => d2067bc65317ed97c201ce2795af04ea
            [parent] => 20
        )

    [60] => Array
        (
            [idnumber] => 54
            [name] => f15f125c54390c968b1821a5afee7605
            [parent] => 20
        )

    [61] => Array
        (
            [idnumber] => 67
            [name] => 39bb4be6f7e30a7631b2fd025bc91f16
            [parent] => 21
        )

    [62] => Array
        (
            [idnumber] => 69
            [name] => 76793afd574e5d76829dd86b05e73e1a
            [parent] => 416
        )

    [63] => Array
        (
            [idnumber] => 70
            [name] => b2eefd8e37aa3306790651eeb1ae265a
            [parent] => 416
        )

    [64] => Array
        (
            [idnumber] => 71
            [name] => dbfd24078ce6c3b41d907e1e9af44009
            [parent] => 416
        )

    [65] => Array
        (
            [idnumber] => 74
            [name] => 9fc683d522580cd8a9f7589eee17aee8
            [parent] => 415
        )

    [66] => Array
        (
            [idnumber] => 77
            [name] => 0c8a52ec9b69c16a278142f5f1778b23
            [parent] => 414
        )

    [67] => Array
        (
            [idnumber] => 84
            [name] => ed13d27194a7c8ec10ae78f7edde7e78
            [parent] => 328
        )

    [68] => Array
        (
            [idnumber] => 85
            [name] => 296acae6743ae09f3056be5be2d7853c
            [parent] => 328
        )

    [69] => Array
        (
            [idnumber] => 86
            [name] => a8051e9837a4425f036b84f836f34149
            [parent] => 329
        )

    [70] => Array
        (
            [idnumber] => 87
            [name] => 29b7e255580b7a4be94145f5ef42923e
            [parent] => 329
        )

    [71] => Array
        (
            [idnumber] => 88
            [name] => 5278e04859e872d7bcd9e65cda9c0f56
            [parent] => 329
        )

    [72] => Array
        (
            [idnumber] => 89
            [name] => 140e465803de655a5b05cee052ca85e1
            [parent] => 329
        )

    [73] => Array
        (
            [idnumber] => 92
            [name] => 99b48a46e92397167254a6247e0b4953
            [parent] => 329
        )

    [74] => Array
        (
            [idnumber] => 93
            [name] => aac30240cf45fbfb6fb85c98516ba655
            [parent] => 329
        )

    [75] => Array
        (
            [idnumber] => 94
            [name] => ae65dc3cd4877ba2f45e4f1957d8ac59
            [parent] => 7946
        )

    [76] => Array
        (
            [idnumber] => 95
            [name] => 0ba7c97a63925f1cca6be8ed9be62090
            [parent] => 7946
        )

    [77] => Array
        (
            [idnumber] => 96
            [name] => 3bae849e0aa84084651e7d89a64b9566
            [parent] => 329
        )

    [78] => Array
        (
            [idnumber] => 97
            [name] => 4cef5692d9b52c5708cd65f500f7e57f
            [parent] => 329
        )

    [79] => Array
        (
            [idnumber] => 98
            [name] => c092a289457c01860e5d86fa740b249f
            [parent] => 329
        )

    [80] => Array
        (
            [idnumber] => 99
            [name] => 5ad5af7f29a16e5c6dadc4ee4433efbb
            [parent] => 329
        )

    [81] => Array
        (
            [idnumber] => 100
            [name] => 2fef02f5b42328de1a531c116f1f44c8
            [parent] => 329
        )

    [82] => Array
        (
            [idnumber] => 101
            [name] => 99fcb7cb1958db55278d75bd9ffce256
            [parent] => 329
        )

    [83] => Array
        (
            [idnumber] => 102
            [name] => cc398a543d74d03c70449d0b6106428c
            [parent] => 331
        )

    [84] => Array
        (
            [idnumber] => 103
            [name] => 66820a6642317a83cf2d4a4a4e6e521c
            [parent] => 331
        )

    [85] => Array
        (
            [idnumber] => 104
            [name] => 3537971acafb2a96cfb914f951815605
            [parent] => 331
        )

    [86] => Array
        (
            [idnumber] => 105
            [name] => 3b1bac0d48851c080f8d75c02576d4f3
            [parent] => 331
        )

    [87] => Array
        (
            [idnumber] => 107
            [name] => 12d3c2a669e461680c8ea85ef7903dbe
            [parent] => 331
        )

    [88] => Array
        (
            [idnumber] => 110
            [name] => 86498621642b02f0849a86f80b48856f
            [parent] => 325
        )

    [89] => Array
        (
            [idnumber] => 111
            [name] => 85dcefe856011148d8609b933b9f2213
            [parent] => 325
        )

    [90] => Array
        (
            [idnumber] => 112
            [name] => 6aefbde721e70e7ee83fe49495812f95
            [parent] => 325
        )

    [91] => Array
        (
            [idnumber] => 113
            [name] => c097b3cd89cf195a4a59922da977473f
            [parent] => 325
        )

    [92] => Array
        (
            [idnumber] => 114
            [name] => ed13d27194a7c8ec10ae78f7edde7e78
            [parent] => 325
        )

    [93] => Array
        (
            [idnumber] => 115
            [name] => 296acae6743ae09f3056be5be2d7853c
            [parent] => 325
        )

    [94] => Array
        (
            [idnumber] => 116
            [name] => d3c160bd103b2f6a8169fe59f765c7ee
            [parent] => 325
        )

    [95] => Array
        (
            [idnumber] => 118
            [name] => 86498621642b02f0849a86f80b48856f
            [parent] => 330
        )

    [96] => Array
        (
            [idnumber] => 119
            [name] => 85dcefe856011148d8609b933b9f2213
            [parent] => 330
        )

    [97] => Array
        (
            [idnumber] => 120
            [name] => ed13d27194a7c8ec10ae78f7edde7e78
            [parent] => 330
        )

    [98] => Array
        (
            [idnumber] => 121
            [name] => 296acae6743ae09f3056be5be2d7853c
            [parent] => 330
        )

    [99] => Array
        (
            [idnumber] => 122
            [name] => 37ef0a2d87231b6fef606daa8e704e9b
            [parent] => 330
        )

    [100] => Array
        (
            [idnumber] => 123
            [name] => b01543c73aafea980d3ede43596225be
            [parent] => 330
        )

    [101] => Array
        (
            [idnumber] => 124
            [name] => d3c160bd103b2f6a8169fe59f765c7ee
            [parent] => 330
        )

    [102] => Array
        (
            [idnumber] => 126
            [name] => 5c8dfb4a4aed6062d1d41baab8ad53f0
            [parent] => 330
        )

    [103] => Array
        (
            [idnumber] => 127
            [name] => 9ad1bbe4e196b578c66ead152c7b02d7
            [parent] => 330
        )

    [104] => Array
        (
            [idnumber] => 128
            [name] => 6aefbde721e70e7ee83fe49495812f95
            [parent] => 330
        )

    [105] => Array
        (
            [idnumber] => 129
            [name] => c097b3cd89cf195a4a59922da977473f
            [parent] => 330
        )

    [106] => Array
        (
            [idnumber] => 130
            [name] => 17ba0c06ff5227f94af93783c1da7345
            [parent] => 330
        )

    [107] => Array
        (
            [idnumber] => 131
            [name] => 6fbc54f26ecbe5306b08921cc16c3807
            [parent] => 330
        )

    [108] => Array
        (
            [idnumber] => 132
            [name] => 28dff0ecadf84d94530811d32ff347b8
            [parent] => 330
        )

    [109] => Array
        (
            [idnumber] => 133
            [name] => ceee8277ac38013590607a7f61df7ad1
            [parent] => 330
        )

    [110] => Array
        (
            [idnumber] => 137
            [name] => 6aefbde721e70e7ee83fe49495812f95
            [parent] => 327
        )

    [111] => Array
        (
            [idnumber] => 138
            [name] => ed13d27194a7c8ec10ae78f7edde7e78
            [parent] => 327
        )

    [112] => Array
        (
            [idnumber] => 139
            [name] => 296acae6743ae09f3056be5be2d7853c
            [parent] => 327
        )

    [113] => Array
        (
            [idnumber] => 140
            [name] => 86498621642b02f0849a86f80b48856f
            [parent] => 327
        )

    [114] => Array
        (
            [idnumber] => 141
            [name] => 85dcefe856011148d8609b933b9f2213
            [parent] => 327
        )

    [115] => Array
        (
            [idnumber] => 142
            [name] => d3c160bd103b2f6a8169fe59f765c7ee
            [parent] => 327
        )

    [116] => Array
        (
            [idnumber] => 144
            [name] => 014b9bb8de7e518262c5a0883a074ea9
            [parent] => 327
        )

    [117] => Array
        (
            [idnumber] => 151
            [name] => 0b34972fe3b45566ceb9770a11a90a9d
            [parent] => 7946
        )

    [118] => Array
        (
            [idnumber] => 152
            [name] => 0b34972fe3b45566ceb9770a11a90a9d
            [parent] => 7946
        )

    [119] => Array
        (
            [idnumber] => 153
            [name] => 0b34972fe3b45566ceb9770a11a90a9d
            [parent] => 7946
        )

    [120] => Array
        (
            [idnumber] => 154
            [name] => 14af2aec1b4799e34dc499ce56825a8a
            [parent] => 329
        )

    [121] => Array
        (
            [idnumber] => 155
            [name] => 29b7e255580b7a4be94145f5ef42923e
            [parent] => 329
        )

    [122] => Array
        (
            [idnumber] => 158
            [name] => 140e465803de655a5b05cee052ca85e1
            [parent] => 329
        )

    [123] => Array
        (
            [idnumber] => 159
            [name] => bd09b24563702316e57224e2bf467150
            [parent] => 329
        )

    [124] => Array
        (
            [idnumber] => 160
            [name] => c88dc73076720593f1267b1e46b79046
            [parent] => 329
        )

    [125] => Array
        (
            [idnumber] => 161
            [name] => c88dc73076720593f1267b1e46b79046
            [parent] => 329
        )

    [126] => Array
        (
            [idnumber] => 162
            [name] => 7e716ea311ab94d7e488086807e07b04
            [parent] => 329
        )

    [127] => Array
        (
            [idnumber] => 163
            [name] => 140e465803de655a5b05cee052ca85e1
            [parent] => 329
        )

    [128] => Array
        (
            [idnumber] => 165
            [name] => d016d35b57067c62fb7ad3968d42ba13
            [parent] => 21
        )

    [129] => Array
        (
            [idnumber] => 167
            [name] => bee11a7b01fa7d7e9aaef9ca6c6eecca
            [parent] => 21
        )

    [130] => Array
        (
            [idnumber] => 169
            [name] => d6d0d7cf5c7ac37d5880b1fa6727380e
            [parent] => 21
        )

    [131] => Array
        (
            [idnumber] => 171
            [name] => 9f146bfd1b1aa02612b95a0ae7b52f13
            [parent] => 21
        )

    [132] => Array
        (
            [idnumber] => 173
            [name] => 7aa1ba9aa94752ee4714e369addaa0d4
            [parent] => 21
        )

    [133] => Array
        (
            [idnumber] => 175
            [name] => a5657e83f1d0908ce2e21f7a99abc500
            [parent] => 21
        )

    [134] => Array
        (
            [idnumber] => 177
            [name] => 5f14f3f1c00baa93ebec797562347ba2
            [parent] => 21
        )

    [135] => Array
        (
            [idnumber] => 179
            [name] => 23a00461ee7a67ee34a48b181113f110
            [parent] => 21
        )

    [136] => Array
        (
            [idnumber] => 183
            [name] => 46196ca9e1d3b55d89497904bbb3a56a
            [parent] => 21
        )

    [137] => Array
        (
            [idnumber] => 184
            [name] => 4853ee507844bdec64071b762c35695f
            [parent] => 21
        )

    [138] => Array
        (
            [idnumber] => 185
            [name] => b034836887359d8fc4ab57c35858ab01
            [parent] => 21
        )

    [139] => Array
        (
            [idnumber] => 186
            [name] => f86d74c5c3aca97ebf974d88df5891bc
            [parent] => 21
        )

    [140] => Array
        (
            [idnumber] => 192
            [name] => ea222f107efb10bb13ac23632d4a798a
            [parent] => 19
        )

    [141] => Array
        (
            [idnumber] => 484
            [name] => 1e1a02d2e8ebe6ea21962e8e3444f4b8
            [parent] => 425
        )

    [142] => Array
        (
            [idnumber] => 518
            [name] => e2a827a10dc2a105c66bc863f06262b4
            [parent] => 19
        )

    [143] => Array
        (
            [idnumber] => 521
            [name] => fe99d661f1c4416e8e6891f1b54dd1e0
            [parent] => 18
        )

    [144] => Array
        (
            [idnumber] => 522
            [name] => 59896b120ec9a236c97a9e36042d8be9
            [parent] => 330
        )

    [145] => Array
        (
            [idnumber] => 523
            [name] => e712a22d1019191ce13901b0aa863e1d
            [parent] => 20
        )

    [146] => Array
        (
            [idnumber] => 525
            [name] => 431317a1408785f11b535406f0efa3f6
            [parent] => 20
        )

    [147] => Array
        (
            [idnumber] => 526
            [name] => c19f8c114520d8de56af061ade571db3
            [parent] => 20
        )

    [148] => Array
        (
            [idnumber] => 527
            [name] => bc5105bc977e9be039bc200565b71c7f
            [parent] => 20
        )

    [149] => Array
        (
            [idnumber] => 529
            [name] => bd4a333b60555df7f4428ca2b5742279
            [parent] => 20
        )
)

1 个答案:

答案 0 :(得分:0)

这是一个解决方案,假设如果idnumber出现多次,那么父母将仅引用第一次出现。

// create a list of the indexes's for each idnumber
$convert_list=array(); // initialise array
reset($data); // pointer to start
while(list($index,$datum)=each($data)) // loop through data
{
    $idnumber=$datum['idnumber'];
    // if idnumber already exists, then dont log it again
    if(!isset($convert_list[$idnumber])) $convert_list[$idnumber]=$index;
}

// create a NEW parent with the index of the entry with that idnumber
reset($data); // pointer to start
while(list($index,$datum)=each($data)) // loop through data
{
    $parent=$datum['parent'];
    if($parent==0)
    {
        // no parent
        $data[$index]['new_parent']=0;
    }
    else
    {
        // check if idnumber actually exists, otherwise say its not found
        if(isset($convert_list[$parent]))
        {
            $data[$index]['new_parent']=$convert_list[$parent];
        }
        else
        {
            $data[$index]['new_parent']='not found';
        }
    }
}

我希望这会有所帮助