I have this following array
我有以下数组
array (
1 =>
array (
't' =>
array (
0 => 't1',
1 => 't2',
2 => 't3',
),
'c' =>
array (
0 => 'c1',
1 => 'c2',
2 => 'c3',
),
'd' =>
array (
0 => 'd1',
1 => 'd2',
2 => 'd3',
),
),
2 =>
array (
'j' =>
array (
0 => 'j1',
1 => 'j2',
2 => 'j3',
),
'm' =>
array (
0 => 'm1',
1 => 'm2',
2 => 'm3',
),
'n' =>
array (
0 => 'n1',
1 => 'n2',
2 => 'n3',
),
),
)
And I need the output to be
我需要输出
array(
1=>array(
't-j'=>array('t1-j1','t2-j2','t3-j3'),
'c-m'=>array('c1-m1','c2-m2','c3-m3'),
'd-n'=>array('d1-n1','d2-n2','d3-n3')
)
);
And What I've done is as
而我所做的就是
$i = 0;
$res = [];
foreach($arr[1] as $key => $value){
foreach($arr[2] as $k => $v){
$res[$key.'-'.$k][] = $value[$i].'-'.$v[$i];
}
$i++;
}
But that gives me something like this?
但这给了我这样的东西?
Array
(
[t-j] => Array
(
[0] => t1-j1
)
[t-m] => Array
(
[0] => t1-m1
)
[t-n] => Array
(
[0] => t1-n1
)
[c-j] => Array
(
[0] => c2-j2
)
[c-m] => Array
(
[0] => c2-m2
)
[c-n] => Array
(
[0] => c2-n2
)
[d-j] => Array
(
[0] => d3-j3
)
[d-m] => Array
(
[0] => d3-m3
)
[d-n] => Array
(
[0] => d3-n3
)
)
So I've searched many of links over here and tried at least most of things that can be possible by me. But I couldn't get the most of it
所以我在这里搜索了很多链接,并尝试了至少大部分可以由我做的事情。但我无法充分利用它
6 个解决方案
#1
5
This did the trick for me:
这对我有用:
$array1 = $arr[1];
$array2 = $arr[2];
$result = combineArray($array1, $array2);
var_dump($result);
function combineArray($array1, $array2)
{
$res = [];
foreach ($array1 as $key => $value)
{
$otherArray = array_splice($array2, 0, 1 );
$otherKey = array_keys($otherArray)[0];
$smallerResult = array();
foreach ($value as $smallerKey => $smallerValue)
{
$smallerResult[] = $smallerValue . '-' . $otherArray[$otherKey][$smallerKey];
}
$res[$key . '-' . $otherKey] = $smallerResult;
}
return $res;
}
The output I get is the following:
我得到的输出如下:
array(3) {
't-j' =>
array(3) {
[0] =>
string(5) "t1-j1"
[1] =>
string(5) "t2-j2"
[2] =>
string(5) "t3-j3"
}
'c-m' =>
array(3) {
[0] =>
string(5) "c1-m1"
[1] =>
string(5) "c2-m2"
[2] =>
string(5) "c3-m3"
}
'd-n' =>
array(3) {
[0] =>
string(5) "d1-n1"
[1] =>
string(5) "d2-n2"
[2] =>
string(5) "d3-n3"
}
}
#2
4
All you should have to do is walk one of the child arrays and get the corresponding keys from both then use array_map with a callback to map the arrays containing the leaves.
您所要做的就是遍历其中一个子数组并从两个数组中获取相应的键,然后使用带有回调的array_map来映射包含叶子的数组。
<?php
// define a callback for use with array_map
$callback = function($v1, $v2){ return $v1."-".$v2; };
// Reset the array pointers
reset($arr[0]);
reset($arr[1]);
$res = [];
// while I'm not at the end of the first array
while (current($arr[0]) !== false){
// combine the keys from each of the subarrays
$key = key($arr[0])."-".key($arr[1]);
//use array_map to mash up the child arrays from each sub array
$value = array_map($callback, current($arr[0]), current($arr[1]));
$res[$key] = $value;
// move the pointers to the next element
next($arr[0]);
next($arr[1]);
}
print_r($res);
To see a simple test case in action check it out here
要查看一个简单的测试用例,请在此处查看
#3
2
Might not be the answer you are looking for but it might help. I have simplified the two arrays.
可能不是你正在寻找的答案,但它可能会有所帮助。我简化了两个数组。
Fiddle: http://ideone.com/M5RQbv
小提琴:http://ideone.com/M5RQbv
<?php
$arr1 = array (
't' => array ('t1','t2','t3'),
'c' => array ('c1','c2','c3'),
'd' => array ('d1','d2','d3'),
);
$arr2 = array (
'j' => array ('j1','j2','j3'),
'm' => array ('m1','m2','m3'),
'n' => array ('n1','n2','n3'),
);
$res = [];
foreach($arr1 as $key => $value){
foreach($arr2 as $k => $v){
for($i = 0 ; $i<count($v); $i++){
$res[$key.'-'.$k][] = $value[$i].'-'.$v[$i];
}
}
}
print_r($res);
Output:
输出:
Array
(
[t-j] => Array
(
[0] => t1-j1
[1] => t2-j2
[2] => t3-j3
)
[t-m] => Array
(
[0] => t1-m1
[1] => t2-m2
[2] => t3-m3
)
[t-n] => Array
(
[0] => t1-n1
[1] => t2-n2
[2] => t3-n3
)
[c-j] => Array
(
[0] => c1-j1
[1] => c2-j2
[2] => c3-j3
)
[c-m] => Array
(
[0] => c1-m1
[1] => c2-m2
[2] => c3-m3
)
[c-n] => Array
(
[0] => c1-n1
[1] => c2-n2
[2] => c3-n3
)
[d-j] => Array
(
[0] => d1-j1
[1] => d2-j2
[2] => d3-j3
)
[d-m] => Array
(
[0] => d1-m1
[1] => d2-m2
[2] => d3-m3
)
[d-n] => Array
(
[0] => d1-n1
[1] => d2-n2
[2] => d3-n3
)
)
#4
2
Here's another approach using for-statement..
这是使用for-statement的另一种方法..
function array_config($arr1, $arr2)
{
$temp_arr = array();
$arr_k1 = array_keys($arr1);
$arr_k2 = array_keys($arr2);
for ($i = 0; $i < count($arr1); $i++)
{
$k1 = $arr_k1[$i];
$k2 = $arr_k2[$i];
$key = $k1."-".$k2;
for ($j = 0; $j < count($arr1[$k1]);$j++)
{
$temp_arr[$key][] = $arr1[$k1][$j]."-".$arr2[$k2][$j];
}
}
return $temp_arr;
}
var_dump(array_config($arr[1], $arr[2]));
Output:
输出:
Cheers! :)
干杯! :)
#5
1
Little late but work for many elements regardless of 2
很晚才为许多元素工作而不管2
<?php
//merge keys
$aKeysMerged = array();
$aValuesMerged = array(); //merge values
foreach ($aArray as $iKey=>$aValue){
$sKeys = array_keys($aValue);
$aValues = array_values($aValue);
if(empty($aKeysMerged)){
$aKeysMerged = $sKeys; //initialize
$aValuesMerged = $aValues; //initialize
}
else{
//merge keys
foreach($aKeysMerged as $iKey1=>$sKey){
$aKeysMerged[$iKey1] = $sKey.'-';
if(isset($sKeys[$iKey1]))
$aKeysMerged[$iKey1] .= $sKeys[$iKey1];
}
//merge values
for($i=0; $i<count($aValuesMerged); $i++){
$a11 = $aValuesMerged[$i];
$a12 = $aValues[$i];
$aMerge = array();
foreach ($a11 as $ikey2=>$sVal){
$aMerge[$ikey2] = $sVal.'-'.$a12[$ikey2];
}
$aValuesMerged[$i] = $aMerge;
}
}
}
//combine the keys and values here
$aResult = array_combine($aKeysMerged, $aValuesMerged);
print_r($aResult);
#6
0
This would help you,
这会对你有所帮助,
$i = 0;
$res = [];
for($i=0;$i<$arr[1].length;$i++){
for($j=0;$j<$arr[1][$i].length;$j++){
$res[$arr[1][$i].'-'.$arr[2][$i] = $arr[1][$i][$j].'-'.$arr[2][$i][$j];
$i++;
}
}
#1
5
This did the trick for me:
这对我有用:
$array1 = $arr[1];
$array2 = $arr[2];
$result = combineArray($array1, $array2);
var_dump($result);
function combineArray($array1, $array2)
{
$res = [];
foreach ($array1 as $key => $value)
{
$otherArray = array_splice($array2, 0, 1 );
$otherKey = array_keys($otherArray)[0];
$smallerResult = array();
foreach ($value as $smallerKey => $smallerValue)
{
$smallerResult[] = $smallerValue . '-' . $otherArray[$otherKey][$smallerKey];
}
$res[$key . '-' . $otherKey] = $smallerResult;
}
return $res;
}
The output I get is the following:
我得到的输出如下:
array(3) {
't-j' =>
array(3) {
[0] =>
string(5) "t1-j1"
[1] =>
string(5) "t2-j2"
[2] =>
string(5) "t3-j3"
}
'c-m' =>
array(3) {
[0] =>
string(5) "c1-m1"
[1] =>
string(5) "c2-m2"
[2] =>
string(5) "c3-m3"
}
'd-n' =>
array(3) {
[0] =>
string(5) "d1-n1"
[1] =>
string(5) "d2-n2"
[2] =>
string(5) "d3-n3"
}
}
#2
4
All you should have to do is walk one of the child arrays and get the corresponding keys from both then use array_map with a callback to map the arrays containing the leaves.
您所要做的就是遍历其中一个子数组并从两个数组中获取相应的键,然后使用带有回调的array_map来映射包含叶子的数组。
<?php
// define a callback for use with array_map
$callback = function($v1, $v2){ return $v1."-".$v2; };
// Reset the array pointers
reset($arr[0]);
reset($arr[1]);
$res = [];
// while I'm not at the end of the first array
while (current($arr[0]) !== false){
// combine the keys from each of the subarrays
$key = key($arr[0])."-".key($arr[1]);
//use array_map to mash up the child arrays from each sub array
$value = array_map($callback, current($arr[0]), current($arr[1]));
$res[$key] = $value;
// move the pointers to the next element
next($arr[0]);
next($arr[1]);
}
print_r($res);
To see a simple test case in action check it out here
要查看一个简单的测试用例,请在此处查看
#3
2
Might not be the answer you are looking for but it might help. I have simplified the two arrays.
可能不是你正在寻找的答案,但它可能会有所帮助。我简化了两个数组。
Fiddle: http://ideone.com/M5RQbv
小提琴:http://ideone.com/M5RQbv
<?php
$arr1 = array (
't' => array ('t1','t2','t3'),
'c' => array ('c1','c2','c3'),
'd' => array ('d1','d2','d3'),
);
$arr2 = array (
'j' => array ('j1','j2','j3'),
'm' => array ('m1','m2','m3'),
'n' => array ('n1','n2','n3'),
);
$res = [];
foreach($arr1 as $key => $value){
foreach($arr2 as $k => $v){
for($i = 0 ; $i<count($v); $i++){
$res[$key.'-'.$k][] = $value[$i].'-'.$v[$i];
}
}
}
print_r($res);
Output:
输出:
Array
(
[t-j] => Array
(
[0] => t1-j1
[1] => t2-j2
[2] => t3-j3
)
[t-m] => Array
(
[0] => t1-m1
[1] => t2-m2
[2] => t3-m3
)
[t-n] => Array
(
[0] => t1-n1
[1] => t2-n2
[2] => t3-n3
)
[c-j] => Array
(
[0] => c1-j1
[1] => c2-j2
[2] => c3-j3
)
[c-m] => Array
(
[0] => c1-m1
[1] => c2-m2
[2] => c3-m3
)
[c-n] => Array
(
[0] => c1-n1
[1] => c2-n2
[2] => c3-n3
)
[d-j] => Array
(
[0] => d1-j1
[1] => d2-j2
[2] => d3-j3
)
[d-m] => Array
(
[0] => d1-m1
[1] => d2-m2
[2] => d3-m3
)
[d-n] => Array
(
[0] => d1-n1
[1] => d2-n2
[2] => d3-n3
)
)
#4
2
Here's another approach using for-statement..
这是使用for-statement的另一种方法..
function array_config($arr1, $arr2)
{
$temp_arr = array();
$arr_k1 = array_keys($arr1);
$arr_k2 = array_keys($arr2);
for ($i = 0; $i < count($arr1); $i++)
{
$k1 = $arr_k1[$i];
$k2 = $arr_k2[$i];
$key = $k1."-".$k2;
for ($j = 0; $j < count($arr1[$k1]);$j++)
{
$temp_arr[$key][] = $arr1[$k1][$j]."-".$arr2[$k2][$j];
}
}
return $temp_arr;
}
var_dump(array_config($arr[1], $arr[2]));
Output:
输出:
Cheers! :)
干杯! :)
#5
1
Little late but work for many elements regardless of 2
很晚才为许多元素工作而不管2
<?php
//merge keys
$aKeysMerged = array();
$aValuesMerged = array(); //merge values
foreach ($aArray as $iKey=>$aValue){
$sKeys = array_keys($aValue);
$aValues = array_values($aValue);
if(empty($aKeysMerged)){
$aKeysMerged = $sKeys; //initialize
$aValuesMerged = $aValues; //initialize
}
else{
//merge keys
foreach($aKeysMerged as $iKey1=>$sKey){
$aKeysMerged[$iKey1] = $sKey.'-';
if(isset($sKeys[$iKey1]))
$aKeysMerged[$iKey1] .= $sKeys[$iKey1];
}
//merge values
for($i=0; $i<count($aValuesMerged); $i++){
$a11 = $aValuesMerged[$i];
$a12 = $aValues[$i];
$aMerge = array();
foreach ($a11 as $ikey2=>$sVal){
$aMerge[$ikey2] = $sVal.'-'.$a12[$ikey2];
}
$aValuesMerged[$i] = $aMerge;
}
}
}
//combine the keys and values here
$aResult = array_combine($aKeysMerged, $aValuesMerged);
print_r($aResult);
#6
0
This would help you,
这会对你有所帮助,
$i = 0;
$res = [];
for($i=0;$i<$arr[1].length;$i++){
for($j=0;$j<$arr[1][$i].length;$j++){
$res[$arr[1][$i].'-'.$arr[2][$i] = $arr[1][$i][$j].'-'.$arr[2][$i][$j];
$i++;
}
}