我需要将2个数组合并为1个数组。
$ languages Array应该为每个[language]数组保存一个[line],并且只有它自己匹配的[language]值。
第一个阵列:
$languages = Array (
[english] => Array ()
[germany] => Array ()
[russia] => Array ()
);
第二阵列:
$data = Array (
[line0] => Some text
[english0] => English text
[germany0] => Dutch text
[russia0] => Russian text
[line1] => Some more text
[english1] => English some more text
[germany1] => Dutch some more text
[russia1] => Russian some more text
.......
(key's increment by 1)
);
组合数组应该是什么样的:
$languages = Array (
[english] = Array (
[line0] => Some text
[english0] => English text
[line1] => Some more text
[english1] => English some more text
........
(key's increment by 1)
)
[germany] = Array (
[line0] => Some text
[germany0] => Dutch text
[line1] => Some more text
[germany1] => Dutch some more text
......
(key's increment by 1)
)
[russia] = Array (
[line0] => Some text
[russia0] => Russian text
[line1] => Some more text
[russia1] => Russian some more text
........
(key's increment by 1)
)
);
答案 0 :(得分:4)
在下面的解决方案中引用您想要的结果:
$languages = Array(
'english' => Array(),
'germany' => Array(),
'russia' => Array()
);
$data = Array(
'line0' => 'Some test',
'english0' => 'English test',
'germany0' => 'Dutch test',
'russia0' => 'Russian test',
'line1' => 'Some more test',
'english1' => 'English some more test',
'germany1' => 'Dutch some more test',
'russia1' => 'Russian some more test',
);
$new_array = array();
foreach ($languages as $k => $v) {
foreach ($data as $key => $value) {
if (strpos($key, 'line') !== false || strpos($key, $k) !== false)
$new_array[$k][$key] = $value;
}
}
print_r($new_array);
<强>输出:强>
Array
(
[english] => Array
(
[line0] => Some test
[english0] => English test
[line1] => Some more test
[english1] => English some more test
)
[germany] => Array
(
[line0] => Some test
[germany0] => Dutch test
[line1] => Some more test
[germany1] => Dutch some more test
)
[russia] => Array
(
[line0] => Some test
[russia0] => Russian test
[line1] => Some more test
[russia1] => Russian some more test
)
)
但正如评论中所建议的,您应该创建更好的格式。例如,请参考以下示例:
$new_array = array();
foreach ($languages as $k => $v) {
foreach ($data as $key => $value) {
$textarr = str_split($key, strlen($key) - 1);
// print_r($textarr);
if (strpos($key, 'line') !== false || strpos($key, $k) !== false)
$new_array[$k][$textarr[0]][$textarr[1]] = $value;
}
}
print_r($new_array);
<强>输出:强>
Array
(
[english] => Array
(
[line] => Array
(
[0] => Some test
[1] => Some more test
)
[english] => Array
(
[0] => English test
[1] => English some more test
)
)
[germany] => Array
(
[line] => Array
(
[0] => Some test
[1] => Some more test
)
[germany] => Array
(
[0] => Dutch test
[1] => Dutch some more test
)
)
[russia] => Array
(
[line] => Array
(
[0] => Some test
[1] => Some more test
)
[russia] => Array
(
[0] => Russian test
[1] => Russian some more test
)
)
)
希望这会对你有所帮助。
答案 1 :(得分:1)
只是为了试图。 以正确的方式查看Chetan的回答
$newArray = array();
$finalArray = array();
$thirdArray = array('line','english','germany','russia'); // for formatting 2nd array
//$data is secondArray
$countData = count($data);
for($i=0;$i<$countData/4;$i++)
{
foreach ($thirdArray as $key => $value) {
$newArray[$i][$value.$i] =$data[$value.$i]; //formatting array
}
}
foreach($newArray as $key=>$val)
{
foreach($languages as $k=>$v){
$finalArray[$k][line.$key] = $newArray[$key][line.$key];
$finalArray[$k][$k.$key] = $newArray[$key][$k.$key];
}
}
print_r($finalArray); // outputs desired results
答案 2 :(得分:0)
假设:(密钥的增量为1)来自
$data
数组。如果不是这个代码将给出未定义索引的错误。
$languageReference
:用作$languages
参考,因为我不想将结果保存在同一个数组中,但如果您愿意,可以。
$reference
:将$data
数组的引用保存为end()
函数会将内部指针移动到数组的末尾,因此不希望处理原始数组。
$languageReference = $languages;
$reference = $data;
end($reference);
$lastKey = key($reference);
$count = (int) $lastKey[strlen($lastKey) - 1];
foreach ($languageReference as $lang => $langData) {
for ($index = 0; $index <= $count; $index++) {
$languageReference[$lang]['line' . $index] = $data['line' . $count];
$languageReference[$lang][$lang . $index] = $data[$lang . $count];
}
}
echo "<pre>";
var_dump($languageReference);
输出:
array(3) {
["english"]=>
array(4) {
["line0"]=>
string(14) "Some more text"
["english0"]=>
string(22) "English some more text"
["line1"]=>
string(14) "Some more text"
["english1"]=>
string(22) "English some more text"
}
["germany"]=>
array(4) {
["line0"]=>
string(14) "Some more text"
["germany0"]=>
string(20) "Dutch some more text"
["line1"]=>
string(14) "Some more text"
["germany1"]=>
string(20) "Dutch some more text"
}
["russia"]=>
array(4) {
["line0"]=>
string(14) "Some more text"
["russia0"]=>
string(22) "Russian some more text"
["line1"]=>
string(14) "Some more text"
["russia1"]=>
string(22) "Russian some more text"
}
}