无法获取特定的数组结构
我花了几个小时来获取一个特定的数组,但无法弄清楚。
我用 foreach 循环得到这个起始数组:
Array
(
[0] => Title 1
[1] => Image 1.jpg
[2] => Title 2
[3] => Image 2.png
[4] => Text 1
)
我生成了一个数组来排序第一个数组(当用户拖放第一个数组的元素时)
Array
(
[0] => 1
[1] => 4
[2] => 0
[3] => 2
[4] => 3
)
我需要的是链接第二个数组 (1, 4, 0, 2, 3) 与第一个数组的键 ([0],1 ...) 来完全得到:
Array
(
[1] => Text 1
[4] => Image 2.png
[0] => Image 1.jpg
[2] => Title 1
[3] => Title 2
)
我尝试过 array_combine 但它没有给我上面的结果。
我制定了一个架构来理解问题:
I'm hanging around since hours to get a specific Array and can't figure it out.
I get this start Array with a foreach loop :
Array
(
[0] => Title 1
[1] => Image 1.jpg
[2] => Title 2
[3] => Image 2.png
[4] => Text 1
)
And I have an Array generated to order the 1st Array (when the user drag and drop elements of the 1st array)
Array
(
[0] => 1
[1] => 4
[2] => 0
[3] => 2
[4] => 3
)
What I need is to link the VALUES of the 2nd array (1, 4, 0, 2, 3) with the keys of the first Array ([0],1 ...) to get exactly that :
Array
(
[1] => Text 1
[4] => Image 2.png
[0] => Image 1.jpg
[2] => Title 1
[3] => Title 2
)
I've tried array_combine but it don't gives me the result above.
I've made a schema to understand the problem:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一些伪代码(
我复制粘贴,但懒得将其更正为有效的 PHP我更正了它):当然,这是假设您的
$order
数组有按键按顺序排列。如果没有,您可能需要在执行foreach
之前对其执行ksort
操作。http://codepad.org/Lq8iw29w
Here is some pseudo-code (
I copy pasted and was too lazy to correct it to be valid PHPI corrected it):Of course this is assuming your
$order
array has the keys in order. If it doesn't you may want to do aksort
on it too before you do theforeach
.http://codepad.org/Lq8iw29w
您将获取第二个数组的每个值,并将其用作第二个数组的键,以获取第一个数组的键,同时使用第二个数组的顺序并键入输出:
输出:
演示
You are taking each value of the second array and use it as key for the second array to obtain the key for the first array while using the second's array order and key the output:
Output:
Demo