在二维数组中放置一个数组内的空间

时间:2017-08-22 08:24:51

标签: php arrays

我有两个数组:

$role = isset($_POST['role'])?$_POST['role']:'';
$details = isset($_POST['details'])?$_POST['details']:'';

数组的输出是:

Role: Array ( [0] => leader [1] => follower ) 
Details: Array ( [0] => lead [1] => follow ) 

我想指定领导者和跟随者跟随。然后我把两个数组放在一个:

foreach( $role as $rl => $r ) 
{
  $array_test3[] = array($r, $details[$rl]);
}

print_r($array_test3)的输出是:

Array ( [0] => Array ( [0] => leader [1] => lead ) [1] => Array ( [0] => follower [1] => follow ) ) 

然后我试图内爆一个数组:

$test3 = implode('<|>',array_map('implode',$array_test3));

echo $test3.'<br/>';的输出是:

leaderlead<|>followerfollow

但我想要得到以下内容:

leader<|>lead<|>follower<|>follow

我该怎么做?

2 个答案:

答案 0 :(得分:1)

您需要使用相同的参数来内爆数组:

$test3 = implode('<|>',array_map(function ($innerArray) {
   return implode("<|>",$innerArray);
},$array_test3));

示例:http://sandbox.onlinephpfunctions.com/code/cc6652ada1609ff62379eaac03c6e8cf9ca5a70b

答案 1 :(得分:1)

你可以做一些事情,更多的是玩游戏,看看我怎么能实现这个......

$role = ["leader","follower"];
$details = ["lead", "follow"];
$a1 = array_combine( $role, $details );
echo str_replace('=', '<|>', http_build_query($a1, '', '<|>'));