2

i have a problem here.

    //this post data containt two array.    
    $titleArray = $_POST['data1']; // Array Project Manager, System Analist, ...
    $nameArray = $_POST['data2']; // Array Gabriel, Anna, Rey, ...

and i whant the result like this:

    array (
           ["Project Manager"] => Gabriel
           ["System Analist"] => Anna
           ["Programmer"] => Jhon
           ["Designer"] => Rey
)

and/or i whant to print like this:

echo $key . ":" . $value;

4 Answers 4

3

array_combineCreates an array by using one array for keys and another for its values

$result = array_combine($titleArray , $nameArray);

foreach ($result as $key => $value) {
   echo $key . ":" . $value;
}
Sign up to request clarification or add additional context in comments.

Comments

1
$titleArray = array('Project Manager','System Analist','Programmer','Designer');
$nameArray  = array('Gabriel','Anna','Jhon','Rey');
$output = array_combine($titleArray,$nameArray);


foreach($output as $key => $value) {
    echo $key.": ".$value.'<br>';
}

ouput

Project Manager: Gabriel
System Analist: Anna
Programmer: Jhon
Designer: Rey

Comments

1

Try array_merge() to merge two array

$result = array_merge($titleArray, $nameArray);
print_r($result);

Or to combine first array for key and second for values

$result = array_combine($titleArray , $nameArray);

or for print use foreach

foreach($result as $key=>$value) {
  echo $key. ":" .$value;
}

Comments

0

From the PHP documentation you can use array_merge

<?php
$array1 = $_POST['data1'];
$array2 = $_POST['data2'];
$result = array_merge($array1, $array2);
print_r($result);
?>

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.