我在Laravel上开发应用程序时遇到了一种情况。我按以下顺序有三个数组。我需要合并它们并按以下顺序显示它们的值。这就是它。
stone_name['ruby','diamond','catseye'];
stone_weight[112,223,445];
stone_rate[1000,2500,670];
我需要合并这三个数组并按此顺序显示输出。
stone_info = array(stone_name[0] , stone_weight[0] , stone_rate[0]);
这样最终的结果就像:
stone_info = array("ruby",112,1000);
答案 0 :(得分:1)
我处于相同的情况,我就是这样做的。可能这可以帮到你。
<?php
$stone_name = ['ruby','diamond','catseye'];
$stone_weight = [112,223,445];
$stone_rate = [1000,2500,670];
$result = mergeArrays($stone_name, $stone_weight, $stone_rate);
function mergeArrays($stone_name, $stone_weight, $stone_rate) {
$result = array();
foreach ($stone_name as $key => $name ) {
$result[] = array( 'stone_name' => $name, 'stone_weight' => $stone_weight[$key], 'stone_rate' => $stone_rate[ $key ] );
}
return $result;
}
print_r($result);
输出:
Array
(
[0] => Array
(
[stone_name] => ruby
[stone_weight] => 112
[stone_rate] => 1000
)
[1] => Array
(
[stone_name] => diamond
[stone_weight] => 223
[stone_rate] => 2500
)
[2] => Array
(
[stone_name] => catseye
[stone_weight] => 445
[stone_rate] => 670
)
)