英文:
Add array same as new value to another array php
问题
我想要为Elementor创建自定义小部件。在这个小部件中,用户可以创建一个图像滑块,并在每个图像上创建一些标记。因此,我为图像创建了一个重复控件,并为标记创建了另一个重复控件。在标记的重复控件中,我添加了一个选择器,用户可以选择图像并将标记添加到所选图像上。
图像重复控件输出:
Array
(
[0] => Array
(
[image_title] => 图像 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] => 图像 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] => 标记1-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] => 标记2-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] => 标记1-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] 值,该值由用户选择的图像(用于检查图像索引)。
我想将标记添加到图像数组中,作为新值,类似于 ['markers'],以确保它们属于正确的图像。您如何解决这个问题?
我编写了以下循环来实现这一点:
$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;
希望这可以帮助您解决问题。
英文:
I want to create custom widget for elementor . In this widget user can create a image slider and create some markers on every image. so i created a repeater control for images and another one for markers . In markers repeater i added a selector that user can select image and add marker to selected image.
Images repeater output :
Array
(
[0] => Array
(
[image_title] => Image 1
[_id] => fe58385
[image_url] => Array
(
=> ../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
(
=> ../wp-content/uploads/2023/05/big-pic-2.png
[id] => 1327
[size] =>
[alt] =>
[source] => library
)
)
)
And Markers output:
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
)
)
in markers you can see [marker_parent] value that selected image by user(use for check image index).
i want to add markers to Images array as new value like ['markers'] that belong to correct image.
how can i resolved it?
I wrote this loop to do that :
$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;
答案1
得分: 0
这段代码将markers
数组映射到images
数组,基于marker_parent
字段作为images
数组的索引。
英文:
I think I understand what you want, this code will map the markers
array to the images
array based on the marker_parent
field being the index of the images array.
<?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);
Yields:
Array
(
[0] => Array
(
[image_title] => Image 1
[_id] => fe58385
[image_url] => Array
(
=> ../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
(
=> ../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
)
)
)
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论