0

I have two arrays:

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

Output for arrays is:

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

I want to assign leader to lead and follower to follow. Then I put two arrays into one:

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

Output for print_r($array_test3) is:

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

Then I tried to implode an array:

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

Output for echo $test3.'<br/>'; is:

leaderlead<|>followerfollow

But I want is to get the following:

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

How can I do this?

2 Answers 2

1

You need to implode the inner array using the same parameter:

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

Example at: http://sandbox.onlinephpfunctions.com/code/cc6652ada1609ff62379eaac03c6e8cf9ca5a70b

Sign up to request clarification or add additional context in comments.

Comments

1

You could do something which was more a case of having a play and seeing how I could achieve this...

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.