1

I'm new on PHP so my question is how to concat all elements of two arrays, every element from array 1 whith element of array 2??

$array1 = array("zero", "one", "two", "three");

$array2 = array("0", "1", "2", "3");

The output that i want wil be :

array3=array("zero 0", "one 1", "two 2", "three 3")

So i will add a space with first element of array 2 to the first element of array 1 ,a space with second element of array 2 to the second element of array 1 ....

0

5 Answers 5

1

First step be sure theta the tow tables have the same number of elements then

 $table_A = array('A_1', 'A_2', 'A_3'); 
 $table_B = array('B_1', 'B_2', 'B_3'); 
 /* To merge the two tables */
 $table_C = array_merge((array)$table_A, (array)$table_B);
echo '<pre>'; 
    print_r($table_C);
echo '</pre>';

/* Output */

Array ( [0] => A_1 [1] => A_2 [2] => A_3 [3] => B_1 [4] => B_2 [5] => B_3 )

/* To merge the two tables according to your example */
$table_D = array(); 
foreach($table_A as $key=>$value) {
    array_push($table_D, $value.' '.$table_B[$key]);
}
echo '<pre>'; 
    print_r($table_D);
echo '</pre>';
/* Output */

Array ( [0] => A_1 B_1 [1] => A_2 B_2 [2] => A_3 B_3 ) ?>

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

2 Comments

@BassemRabia - Looking at your answer I feel i must point out that your first step is not correct. Surely you merge the two arrays, but it doesn't tell you anything. If you want to count the arrays, use count($array);
@Daniel, thanks for your feedback, the main idea is just to write a simple example how to merge two tables, but according to the question details, the answers is the second part of the code.
1

You have to make sure that you have the same quantity of element in bove arrays, so you can make a for on it:

$result_array = array();
for($i=0;$i<count($array1);$i++) {
    $result_array[] = "{$array1[$i} {$array2[$i]}";
}

print_r($result_array);

Comments

1

If I may conclude that the keys are one-to-one, you could do a foreach loop:

foreach ($array1 as $key => $value) {

    $new_array[] = $array1[$key] .  " " . $array2[$key];

}

Comments

1
$arr1 = array("zero", "one", "two", "three");
$arr2 = array("0", "1", "2", "3");
$arr3 = array();

for ($i=0; $i<count($arr1); $i++) {
    $arr3[] = $arr1[$i] . ' ' . $arr2[$i]; 
}

2 Comments

Please explain your answers
What do you mean? Isn't it self explanatory? What it is you don't understand?
1

This is very simple.

for(i=0;i<count($array2);i++)
{
$temp = "";
$temp = $array1[i].' '.$array2[i];
array_push($array3,$temp)
}

In above code, in for loop you should pass the minimum size array from array1 and array2 to count function. In your code, you should pass array2 as its size is less than array1.

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.