I have 3 arrays, just 7 elements in them each. Arrays are:
我有3个数组,每个数组只有7个元素。数组是:
filename[]
文件名[]
title[]
标题[]
description[]
描述[]
I want to express and iterate through a single associative array for each of the data in the arrays above. filename
can be the key value for the assoc array, but each filename has it's own corresponding title and description.
我想表达并迭代上面数组中每个数据的单个关联数组。 filename可以是assoc数组的键值,但每个文件名都有自己对应的标题和描述。
Below is a sample of:
以下是一个示例:
var_dump($filename)
string(10) "IMG_1676_3" [1]=>
string(10) "IMG_0539_3" [2]=>
string(8) "IMG_1942" [3]=>
string(8) "IMG_1782" [4]=>
string(8) "IMG_2114" [5]=>
string(8) "IMG_9759" [6]=>
string(8) "IMG_2210" }
var_dump($title)
string(31) "Lighthouse at Ericeira Portugal" [1]=>
string(23) "Gaudi park in Barcelona" [2]=>
string(32) "Driving around outside of Lisbon" [3]=>
string(16) "Madeira Portugal" [4]=>
string(15) "Barcelona Spain" [5]=>
string(15) "Lisbon Portugal" [6]=>
string(14) "Sailing Lisbon" }
4 个解决方案
#1
6
function mergeArrays($filenames, $titles, $descriptions) {
$result = array();
foreach ( $filenames as $key=>$name ) {
$result[] = array( 'filename' => $name, 'title' => $titles[$key], 'descriptions' => $descriptions[ $key ] );
}
return $result;
}
Just make sure you pass valid input to the function, or add some extra checking. Is that what you're looking for?
只需确保将有效输入传递给函数,或添加一些额外的检查。这就是你要找的东西吗?
#2
4
If you array key are the same for all the 3 arrays, the better way to do what you are asking, is a foreach creating a new array with all the key(filename,title,description) in the same key:
如果所有3个数组的数组键都相同,那么更好的方法就是使用同一个键创建一个包含所有键(文件名,标题,描述)的新数组:
<?php
foreach($filename as $key => $file)
{
$files[$key]['filename'] = $file;
$files[$key]['title'] = $title[$key];
$files[$key]['description'] = $description[$key];
}
?>
#3
0
Try this
尝试这个
$result = array_combine($filename, array_map(null, $title, $description));
var_dump($result);
Or if you need the inner array to be associative.
或者,如果您需要内部数组是关联的。
$result = array_combine($filename,
array_map(function($t, $d) {
return array('title'=>$t, 'description'=>$d);
}, $title, $description
)
);
If you just want to combine the three arrays
如果你只想组合三个数组
$result = array_map(function($f, $t, $d) {
return array('filename'=>$f, 'title'=>$t, 'description'=>$d);
}, $filename, $title, $description
);
#4
-1
$array1 = array("orange", "apple", "grape");
$array2 = array("peach", 88, "plumb");
$array3 = array("lemon", 342);
$newArray = array_merge($array1, $array2, $array3);
foreach ($newArray as $key => $value) {
echo "$key - <strong>$value</strong> <br />";
}
#1
6
function mergeArrays($filenames, $titles, $descriptions) {
$result = array();
foreach ( $filenames as $key=>$name ) {
$result[] = array( 'filename' => $name, 'title' => $titles[$key], 'descriptions' => $descriptions[ $key ] );
}
return $result;
}
Just make sure you pass valid input to the function, or add some extra checking. Is that what you're looking for?
只需确保将有效输入传递给函数,或添加一些额外的检查。这就是你要找的东西吗?
#2
4
If you array key are the same for all the 3 arrays, the better way to do what you are asking, is a foreach creating a new array with all the key(filename,title,description) in the same key:
如果所有3个数组的数组键都相同,那么更好的方法就是使用同一个键创建一个包含所有键(文件名,标题,描述)的新数组:
<?php
foreach($filename as $key => $file)
{
$files[$key]['filename'] = $file;
$files[$key]['title'] = $title[$key];
$files[$key]['description'] = $description[$key];
}
?>
#3
0
Try this
尝试这个
$result = array_combine($filename, array_map(null, $title, $description));
var_dump($result);
Or if you need the inner array to be associative.
或者,如果您需要内部数组是关联的。
$result = array_combine($filename,
array_map(function($t, $d) {
return array('title'=>$t, 'description'=>$d);
}, $title, $description
)
);
If you just want to combine the three arrays
如果你只想组合三个数组
$result = array_map(function($f, $t, $d) {
return array('filename'=>$f, 'title'=>$t, 'description'=>$d);
}, $filename, $title, $description
);
#4
-1
$array1 = array("orange", "apple", "grape");
$array2 = array("peach", 88, "plumb");
$array3 = array("lemon", 342);
$newArray = array_merge($array1, $array2, $array3);
foreach ($newArray as $key => $value) {
echo "$key - <strong>$value</strong> <br />";
}