Jump to content

how to merge two arrays in order


Go to solution Solved by jodunno,

Recommended Posts

i have these two arrays:

array(2) { [0]=> array(10) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" [3]=> string(1) "d" [4]=> string(1) "e" [5]=> string(1) "f" [6]=> string(1) "g" [7]=> string(1) "h" [8]=> string(1) "i" [9]=> string(1) "j" } [1]=> array(2) { [0]=> string(1) "a" [1]=> string(0) "" } }

array(11) { [0]=> string(6) "1.1.51" [2]=> string(8) "1.1.51.2" [3]=> string(8) "1.1.51.3" [4]=> string(8) "1.1.51.4" [5]=> string(8) "1.1.51.5" [6]=> string(8) "1.1.51.6" [7]=> string(8) "1.1.51.7" [8]=> string(8) "1.1.51.8" [9]=> string(8) "1.1.51.9" [1]=> string(9) "1.1.51.10" [10]=> string(6) "1.1.52" }

how can  merge the corresponding values of array(11) to those of array(2).keeping in mind that 1.1.51-1.1.51.10 are for  the first sub array in array(2) and 1.1.52 corresponds to the values of key 1 in array(2).And how can i display them?

Link to comment
https://forums.phpfreaks.com/topic/323878-how-to-merge-two-arrays-in-order/
Share on other sites

5 minutes ago, requinix said:

"Merge in order" how? What's the output you want to get?

Spoiler: the answer is probably going to involve some combination of array_combine, array_slice, and/or array_merge.

i would like to map the values of array(2) in key 0 to those of array(11) from 1.1.51 to 1.1.51.10 so that i print out:

1.1.51,a

1.1.51.1,b

.

.

.

1.1.51.10,j

1.1.52,a

You can flatten the first array with array_merge and the ... operator. That gives you an array of size 11 and will keep the same ordering.

Then use array_combine to create a new array using one of them for keys and the other for the values... is what I assume you want.

  • Solution
23 hours ago, shadd said:

how can  merge the corresponding values of array(11) to those of array(2).keeping in mind that 1.1.51-1.1.51.10 are for  the first sub array in array(2) and 1.1.52 corresponds to the values of key 1 in array(2).And how can i display them?

again with these arrays? you'll never learn, eh?

I think that array merge will fail you here with these demands and array combine will throw an error due to the key length mismatches. I can only guess that you are trying to preserve the order of array 11, id est, the key 1 not following 0. I also assume that you want the 10 key moved to array 2 1 key. This is really not how one should code arrays. I can only suggest bad coding practices in an attempt to solve your problem but i may not be understanding you correctly. Very vague.

I have a way to mix the arrays but it is not professional coding methods by any stretch of the imagination. Yes i'm saying that the following code is bad. Meantime, the final key can be printed to the screen but outside of the loop. Inside of the loop would require an array count and a counter to find the end of the loop.

<?php

echo <<<RAYS
<html>
<head>
  <title></title>
  <style>
    div.raybox { display: block; position: static; width: 20em; height: 18em; vertical-align: top; background: #101010; border-radius: 8px; box-sizing: content-box; overflow: hidden; }
    div.ray { display: inline-block; position: relative; margin: 4px; padding: 4px; width: 9em; height: 94%; vertical-align: top; background: #000000; color: rgba(240, 240, 240, 0.6); box-sizing: content-box; }
  </style>
</head>
<body>

RAYS;

$aRay = (array)  [0 => (array) [0 => "a", 1 => "b", 2 => "c", 3 => "d", 4 => "e", 5 => "f", 6 => "g", 7 => "h", 8 => "i", 9=> "j"], 1 => (array) [0 => "a", 1 => ""]];
$bRay = (array) [0 => "1.1.51", 2 => "1.1.51.2", 3 => "1.1.51.3", 4 => "1.1.51.4", 5 => "1.1.51.5", 6 => "1.1.51.6", 7 => "1.1.51.7", 8 => "1.1.51.8", 9 => "1.1.51.9", 1 => "1.1.51.10", 10 => "1.1.52"];

echo '<div class="raybox">';
echo '<div class="ray">aRay:<br>';
foreach ($aRay as $key => $array) {
  foreach ($array as $index => $value) {
    echo '[' . $key . '][' . $index . '] => ' . $value . '<br>';
  }
}
echo '</div>';
echo '<div class="ray">bRay:<br>';
foreach ($bRay as $index => $value) {
    echo '[' . $index . '] => ' . $value . '<br>';
}
echo '</div>';
echo '</div>';

$aRay[1][1] = array_pop($bRay);
$stingRay = array ();
array_push($stingRay, $bRay);
array_push($stingRay, $aRay[0]);
array_push($stingRay, $aRay[1]);

//echo '<br>';
//print_r($stingRay);
//echo '<br>';

//foreach ($stingRay as $key1 => $array) {
//  foreach ($array as $key2 => $value) {
//    echo $value . '<br>';
//  }
//}

echo '<br>';
foreach ($stingRay[0] as $key => $value) {
    echo $value  . ': ';
    echo $stingRay[1][$key]  . '<br>';
} echo $stingRay[2][1] . ': ' . $stingRay[2][0];


echo <<<RAYS
</body>
</html>

RAYS;

?>

the output is what you are seeking, i think. Otherwise, i would say build a new array with array 11 values as keys in the new array. Then add array 2 values (letters) as values for the new array keys. Then add the last key value pair to the new array. Then you could loop over one array printing keys and values to obtain your objective. 

I'm ashamed to post this awful code but that is how twisted this concept is becoming. Really try to recode your project correctly and these problems disappear. 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.