无法获取特定的数组结构

发布于 2024-12-10 01:11:00 字数 806 浏览 1 评论 0原文

我花了几个小时来获取一个特定的数组,但无法弄清楚。

我用 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:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凹づ凸ル 2024-12-17 01:11:00

这是一些伪代码(我复制粘贴,但懒得将其更正为有效的 PHP 我更正了它):

<?php
$items = Array
(
   0 => 'Title 1',
   1 => 'Image 1.jpg',
   2 => 'Title 2',
   3 => 'Image 2.png',
   4 => 'Text 1',
);
$order = Array
(
   0 => 1,
   1 => 4,
   2 => 0,
   3 => 2,
   4 => 3,
);

foreach ($order as $itemPosition) {
    $sorted[] = $items[ $itemPosition ];
}

// optionally 
ksort($sorted);
var_dump($sorted);

当然,这是假设您的 $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 PHP I corrected it):

<?php
$items = Array
(
   0 => 'Title 1',
   1 => 'Image 1.jpg',
   2 => 'Title 2',
   3 => 'Image 2.png',
   4 => 'Text 1',
);
$order = Array
(
   0 => 1,
   1 => 4,
   2 => 0,
   3 => 2,
   4 => 3,
);

foreach ($order as $itemPosition) {
    $sorted[] = $items[ $itemPosition ];
}

// optionally 
ksort($sorted);
var_dump($sorted);

Of course this is assuming your $order array has the keys in order. If it doesn't you may want to do a ksort on it too before you do the foreach.

http://codepad.org/Lq8iw29w

橪书 2024-12-17 01:11:00

您将获取第二个数组的每个值,并将其用作第二个数组的键,以获取第一个数组的键,同时使用第二个数组的顺序并键入输出:

$mapped = array();

foreach($arr2 as $value)
{
    $mapped[$value] = $arr1[$arr2[$value]];
}

输出:

Array
(
    [1] => Text 1
    [4] => Image 2.png
    [0] => Image 1.jpg
    [2] => Title 1
    [3] => Title 2
)

演示

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:

$mapped = array();

foreach($arr2 as $value)
{
    $mapped[$value] = $arr1[$arr2[$value]];
}

Output:

Array
(
    [1] => Text 1
    [4] => Image 2.png
    [0] => Image 1.jpg
    [2] => Title 1
    [3] => Title 2
)

Demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文