在PHP中合并3个数组(Merging 3 arrays in PHP)
我有3个阵列如下。
$array1 = Array
(
[0] => 05/01
[1] => 05/02
)
$array2 =Array
(
[0] => ED
[1] => P
)
$array3 =Array
(
[0] => Mon
[1] => Tue
)
我想将这3 $result_array为$result_array 。 我写了如下代码。 但它给了一个空阵列。
$result_array =Array
(
[0] => Array
(
[0] => 05/01
[1] => ED
[2] => Mon
)
[1] => Array
(
[0] => 05/02
[1] => P
[2] => Tue
)
)
码:
for($z=0; $z
$all_array[$z][] = array_merge($array1[$z],$array2[$z] );
$all_array2[$z] = array_merge($all_array[$z],$array3[$z] );
}
请帮助我做到这一点。
I have 3 arrays as below.
$array1 = Array
(
[0] => 05/01
[1] => 05/02
)
$array2 =Array
(
[0] => ED
[1] => P
)
$array3 =Array
(
[0] => Mon
[1] => Tue
)
I want to merge these 3 arrays as below $result_array. I have written a code as below. But It gave a empty array.
$result_array =Array
(
[0] => Array
(
[0] => 05/01
[1] => ED
[2] => Mon
)
[1] => Array
(
[0] => 05/02
[1] => P
[2] => Tue
)
)
Code:
for($z=0; $z
$all_array[$z][] = array_merge($array1[$z],$array2[$z] );
$all_array2[$z] = array_merge($all_array[$z],$array3[$z] );
}
Please help me to do this.
原文:https://stackoverflow.com/questions/45164928
更新时间:2019-11-03 17:39
最满意答案
简单地遍历第一个数组,并使用索引作为其他数组的关键字。
foreach ( $array1 as $idx => $val ) {
$all_array[] = [ $val, $array2[$idx], $array3[$idx] ];
}
请记住,如果所有3个数组的长度都相同,那么这个操作将会起作用,您可能想先检查一下
Simply foreach over the first array and use the index as the key to the other arrays.
foreach ( $array1 as $idx => $val ) {
$all_array[] = [ $val, $array2[$idx], $array3[$idx] ];
}
Remember this will only work if all 3 arrays are the same length, you might like to check that first
2017-07-18
相关问答
简单地遍历第一个数组,并使用索引作为其他数组的关键字。 foreach ( $array1 as $idx => $val ) {
$all_array[] = [ $val, $array2[$idx], $array3[$idx] ];
}
请记住,如果所有3个数组的长度都相同,那么这个操作将会起作用,您可能想先检查一下 Simply foreach over the first array and use the index as the key to the other arra
...
一个简单的自定义脚本可以使这个技巧工作 http://www.tehplayground.com/#7Uudu9PwY $arr1 = array(
array('search_terms' => 'facebook', 'search_count' => 1),
array('search_terms' => 'whatever', 'search_count' => 4)
);
$arr2 = array(
array('search_terms' => 'face
...
我一直是可以在将来使用的一般用途功能的粉丝。 此解决方案有三个参数: 1)需要组合的所有阵列的数组 2)将用作比较基础的密钥(具有密钥的两个节点将被组合) 3)数据键,如果节点“相等”,它将是组合的字段 最后,php非常擅长制作词典/哈希映射。 这将有助于我们组合数组,因为我们不需要做太多的搜索。 /**
* $arrays - an array of arrays to be combined
* $key - a key in the array that represents the k
...
以正确的方式使用array_merge_recursive。 $arr1 = array(
'0' => array(
'u_data' => array(
'Mode' => 'Rail',
'TotalCost' => 150,),
'0' => array(
'CostSavingRou
...
你应该在从数据库中提取时改变你构建数组的方式,这样你就可以获得一个有意义的键而不是无意义的数字索引。 在上面的示例中,您可能希望使用DealerName作为键。 这样,当您检索项目时,只需将它们添加到适当的位置即可。 例: while($rows = fetch_from_db($res)) {
$output[$rows['DealerName']]['dealersContacted'] = $rows['dealersContacted'];
$output[$rows['
...
$array=Array
(
0 => Array
(
0 => '2017-11-03',
1 => '2017-11-05',
2 => '1',
),
1 => Array
(
0=> '2017-11-23',
1 => '2017-11-25',
2 => '1'
),
);
$arraySm
...
做这样的事情而不是在循环时将它们添加到数组中: $file = fopen('/home/user/log.txt', 'r');
$array = [];
if ($file) {
while (($line = fgets($file)) !== false)
{
if (preg_match('~^/[^:]+~m', $line, $files))
{ //match everything until the first ':' to get file n
...
array_fill_keys将构建一个可以在array_merge使用的好数组: array_merge(array_fill_keys($array1, ''), $array2); 或者,您可以使用执行联合的+ op代替array_merge: $array2 + array_fill_keys($array1, ''); 这个用数字键或混合数字/字符串:) array_fill_keys will build you a nice array you can use in array_me
...
既然你想获得用户名,可以只: 直接指出它(这是索引2) 弹出第一个数组(看起来像标题),搜索可用作标记的username 。 在确定它的位置后,您可以在一个简单的循环中使用它。 考虑这个例子: #1 $values = array( array('First Name', 'Last Name', 'Username'), array('Bob', 'Dill', 'DBob'), array('Amy', 'Simpson', 'Asimp'), array('Doug', 'James', '
...
$final = array($array1, $array2); $final = array($array1, $array2);