wordpress 添加数组相同的新值到另一个数组php

ee7vknir  于 2023-06-21  发布在  WordPress
关注(0)|答案(1)|浏览(104)

我想为elementor创建自定义小部件。在这个小工具中,用户可以创建一个图像滑块,并在每个图像上创建一些标记。所以我为图像创建了一个repeater控件,为标记创建了另一个。在标记重复器中,我添加了一个选择器,用户可以选择图像并将标记添加到选定的图像。
图像中继器输出:

Array
(
    [0] => Array
        (
            [image_title] => Image 1
            [_id] => fe58385
            [image_url] => Array
                (
                    [url] => ../wp-content/uploads/2023/05/big-pic-1.png
                    [id] => 1326
                    [size] => 
                    [alt] => 
                    [source] => library
                )

        )

    [1] => Array
        (
            [image_title] => Image 2
            [_id] => 40a0cc6
            [image_url] => Array
                (
                    [url] => ../wp-content/uploads/2023/05/big-pic-2.png
                    [id] => 1327
                    [size] => 
                    [alt] => 
                    [source] => library
                )

        )

)

和标记输出:

Array
(
    [0] => Array
        (
            [marker_title] => mark1-1
            [marker_top] => 40
            [marker_left] => 100
            [_id] => 6763ae7
            [marker_parent] => 0
            [marker_desc] => Lorem ipsum dolor sit amet, ne est populo dictas, et pri ferri ubique labitur.

Mel ad euismod adipiscing moderatius eos an decore quodsi animal.
        )

    [1] => Array
        (
            [marker_title] => mark2-1
            [marker_top] => 60
            [marker_left] => 100
            [marker_parent] => 0
            [marker_desc] => Lorem ipsum dolor sit amet, ne est populo dictas, et pri ferri ubique labitur.

Mel ad euismod adipiscing moderatius eos an decore quodsi animal.
            [_id] => 9d1b611
        )

    [2] => Array
        (
            [marker_title] => mark1-2
            [marker_top] => 40
            [marker_left] => 100
            [marker_parent] => 1
            [marker_desc] => Lorem ipsum dolor sit amet, ne est populo dictas, et pri ferri ubique labitur.

Mel ad euismod adipiscing moderatius eos an decore quodsi animal.
            [_id] => 6030c28
        )

)

在标记中,您可以看到用户选择图像的[marker_parent]值(用于检查图像索引)。我想添加标记到图像数组作为新的值,如['标记']属于正确的图像。
怎么解决呢?
我写了这个循环来做到这一点:

$settings = $this->get_settings_for_display();
$images = $settings['images'];
$markers = $settings['markers'];
foreach ($images as $key=>$image) :
   if($key == $markers['marker_parent']){
         $image['marker'] = $markers[$key];
   }
endforeach;
w1jd8yoj

w1jd8yoj1#

我想我明白你想要什么了,这段代码将基于marker_parent字段作为images数组的索引,将markers数组Map到images数组。

<?php

/*

Question Author: Vahab Mohammadi
Question Answerer: Jacob Mulquin
Question: Add array same as new value to another array php
URL: https://stackoverflow.com/questions/76435799/add-array-same-as-new-value-to-another-array-php
Tags: php, wordpress, elementor

*/

$settings = $this->get_settings_for_display();
$images = $settings['images'];
$markers = $settings['markers'];

$markers_map = [];
foreach ($markers as $marker) {
    if (!isset($markers_map[$marker['marker_parent']])) {
        $markers_map[$marker['marker_parent']] = [];
    }
    $markers_map[$marker['marker_parent']][] = $marker;
}

foreach ($markers_map as $index => $map) {
    $images[$index]['markers'] = $map;
}

print_r($images);

产量:

Array
(
    [0] => Array
        (
            [image_title] => Image 1
            [_id] => fe58385
            [image_url] => Array
                (
                    [url] => ../wp-content/uploads/2023/05/big-pic-1.png
                    [id] => 1326
                    [size] => 
                    [alt] => 
                    [source] => library
                )

            [markers] => Array
                (
                    [0] => Array
                        (
                            [marker_title] => mark1-1
                            [marker_top] => 40
                            [marker_left] => 100
                            [_id] => 6763ae7
                            [marker_parent] => 0
                            [marker_desc] => Lorem ipsum dolor sit amet, ne est populo dictas, et pri ferri ubique labitur.

Mel ad euismod adipiscing moderatius eos an decore quodsi animal.
                        )

                    [1] => Array
                        (
                            [marker_title] => mark2-1
                            [marker_top] => 60
                            [marker_left] => 100
                            [marker_parent] => 0
                            [marker_desc] => Lorem ipsum dolor sit amet, ne est populo dictas, et pri ferri ubique labitur.

Mel ad euismod adipiscing moderatius eos an decore quodsi animal.
                            [_id] => 9d1b611
                        )

                )

        )

    [1] => Array
        (
            [image_title] => Image 2
            [_id] => 40a0cc6
            [image_url] => Array
                (
                    [url] => ../wp-content/uploads/2023/05/big-pic-2.png
                    [id] => 1327
                    [size] => 
                    [alt] => 
                    [source] => library
                )

            [markers] => Array
                (
                    [0] => Array
                        (
                            [marker_title] => mark1-2
                            [marker_top] => 40
                            [marker_left] => 100
                            [marker_parent] => 1
                            [marker_desc] => Lorem ipsum dolor sit amet, ne est populo dictas, et pri ferri ubique labitur.

Mel ad euismod adipiscing moderatius eos an decore quodsi animal.
                            [_id] => 6030c28
                        )

                )

        )

)

相关问题