Php 组合数组和concat值?
解释起来有点复杂,所以这里有一个简单的具体例子: 阵列1:Php 组合数组和concat值?,php,foreach,Php,Foreach,解释起来有点复杂,所以这里有一个简单的具体例子: 阵列1: Array ( [4] => bim [5] => pow [6] => foo ) 阵列2: Array ( [n] => Array ( [0] => 1 ) [m] => Array ( [0
Array
(
[4] => bim
[5] => pow
[6] => foo
)
阵列2:
Array
(
[n] => Array
(
[0] => 1
)
[m] => Array
(
[0] => 1
[1] => 2
)
[l] => Array
(
[0] => 1
[1] => 4
[2] => 64
)
我需要输出一个数组3
预期的阵列数:
Array
(
[bim] => n-1
[pow] => Array
(
[0] => m-1
[1] => m-2
)
[foo] => Array
(
[0] => l-1
[1] => l-4
[2] => l-64
)
预期最终回声输出:
bim n-1、pow m-1 m-2、foo l-1 l-4 l-64、
我试过了,但似乎很遗憾:
foreach($array2 as $k1 =>$v1){
foreach($array2[$k1] as $k => $v){
$k[] = $k1.'_'.$v);
}
foreach($array1 as $res =>$val){
$val = $array2;
}
谢谢你的帮助,
Jess接受了挑战
<?php
$a = array(
4 => 'bim',
5 => 'pow',
6 => 'foo',
);
$b = array(
'n' => array(1),
'm' => array(1, 2),
'l' => array(1, 4, 64),
);
$len = count($a);
$result = array();
$aVals = array_values($a);
$bKeys = array_keys($b);
$bVals = array_values($b);
for ($i = 0; $i < $len; $i++) {
$combined = array();
$key = $aVals[$i];
$prefix = $bKeys[$i];
$items = $bVals[$i];
foreach ($items as $item) {
$combined[] = sprintf('%s-%d', $prefix, $item);
};
if (count($combined) === 1) {
$combined = $combined[0];
}
$result[$key] = $combined;
}
var_dump($result);
?>
您的代码可能非常简单。例如,假设数组:
$one = Array
(
4 => 'bim',
5 => 'pow',
6 => 'foo'
);
$two = Array
(
'n' => Array
(
0 => 1
),
'm' => Array
(
0 => 1,
1 => 2
),
'l' => Array
(
0 => 1,
1 => 4,
2 => 64
)
);
您可以通过以下方式获得结果:
$result = [];
while((list($oneKey, $oneValue) = each($one)) &&
(list($twoKey, $twoValue) = each($two)))
{
$result[$oneValue] = array_map(function($item) use ($twoKey)
{
return $twoKey.'-'.$item;
}, $twoValue);
};
-检查此注释,上面的代码不会将单个元素数组作为单个元素。如果需要,只需添加:
$result = array_map(function($item)
{
return count($item)>1?$item:array_shift($item);
}, $result);
此解决方案的版本适用于PHP4>=4.3
,PHP5>=5.0
,您可以找到
更新:如果只需要字符串,则使用(交叉版本):
作为问题的解决方案,请尝试执行以下代码段
<?php
$a=array(4=>'bim',5=>'pow',6=>'foo');
$b=array('n'=>array(1),'m'=>array(1,2),'l'=>array(1,4,64));
$keys=array_values($a);
$values=array();
foreach($b as $key=>$value)
{
if(is_array($value) && !empty($value))
{
foreach($value as $k=>$val)
{
if($key=='n')
{
$values[$key]=$key.'-'.$val;
}
else
{
$values[$key][]=$key.'-'.$val;
}
}
}
}
$result=array_combine($keys,$values);
echo '<pre>';
print_r($result);
?>
通过阅读代码注释,应该清楚背后的逻辑。
这是一个例子 关于您的最终输出,您可以执行以下操作
这将回显
bim n-1、pow m-1 m-2、foo l-1 l-4 l-64
,因此格式应始终相同,这意味着如果第一个数组有3个元素,那么第二个数组将正好有3个,并且第一个数组中的第一个元素将始终与第二个数组中的第一个元素关联,以生成最终数组?您尝试的代码甚至不会编译。每一秒都有一个)
太多,而你缺少了一个代码>在每个的最后一个。@AbhikChakraborty如果我理解的很好,答案是yes@BastiM我编辑了,但不幸的是,它不是pb:(但它需要PHP5.3,Miraages不需要:答案是Pthx,但我不知道如何对它进行调整,你有我的数组的具体示例吗?!那又怎样?我的解决方案至少需要5.4(因为[]
array declaration)。再次强调:不要拘泥于旧的PHP版本。没有理由使用它们。如果您不使用共享主机,您可能会完全忘记旧的PHP版本。否。“[bim]=>n-1”-不是数组,你的解决方案是数组。我的意思是,正如我在问题中所写的,我的最终结果应该是这样的:bim n-1,pow m-1 m-2,foo l-1 l-4 l-64是的,但对于你的解决方案,你如何使最终的回声输出达到预期效果:bim n-1,pow m-1 m-2,foo l-1 l-4 l-64我使用了数组_组合函数来生成最终结果,它创建了以键和值作为参数的新数组
<?php
$a=array(4=>'bim',5=>'pow',6=>'foo');
$b=array('n'=>array(1),'m'=>array(1,2),'l'=>array(1,4,64));
$keys=array_values($a);
$values=array();
foreach($b as $key=>$value)
{
if(is_array($value) && !empty($value))
{
foreach($value as $k=>$val)
{
if($key=='n')
{
$values[$key]=$key.'-'.$val;
}
else
{
$values[$key][]=$key.'-'.$val;
}
}
}
}
$result=array_combine($keys,$values);
echo '<pre>';
print_r($result);
?>
//omitted array declarations
$output = array();
//variables to shorten things in the loop
$val1 = array_values($array1);
$keys2 = array_keys($array2);
$vals2 = array_values($array2);
//iterating over each element of the first array
for($i = 0; $i < count($array1); $i++) {
//if the second array has multiple values at the same index
//as the first array things will be handled differently
if(count($vals2[$i]) > 1) {
$tempArr = array();
//iterating over each element of the second array
//at the specified index
foreach($vals2[$i] as $val) {
//we push each element into the temporary array
//(in the form of "keyOfArray2-value"
array_push($tempArr, $keys2[$i] . "-" . $val);
}
//finally assign it to our output array
$output[$val1[$i]] = $tempArr;
} else {
//when there is only one sub-element in array2
//we can assign the output directly, as you don't want an array in this case
$output[$val1[$i]] = $keys2[$i] . "-" . $vals2[$i][0];
}
}
var_dump($output);
Array (
["bim"]=> "n-1"
["pow"]=> Array (
[0]=> "m-1"
[1]=> "m-2"
)
["foo"]=> Array (
[0]=> "l-1"
[1]=> "l-4"
[2]=> "l-64"
)
)
$final = "";
//$output can be obtained by any method of the other answers,
//not just with the method i posted above
foreach($output as $key=>$value) {
$final .= $key . " ";
if(count($value) > 1) {
$final .= implode($value, " ") .", ";
} else {
$final .= $value . ", ";
}
}
$final = rtrim($final, ", ");