wrenbjor Posted November 4, 2008 Share Posted November 4, 2008 ok I have an array where the length changes but it is always divisible by 5. I.E <?php $rep_array = Array ( [0] => 3 [1] => 48 [2] => 206 [3] => 1224460800 [4] => 0.0 [5] => 3 [6] => 48 [7] => 206 [8] => 1224461100 [9] => 0.0 [10] => 3 [11] => 48 [12] => 206 [13] => 1224461400 [14] => 0.0 [15] => 3 [16] => 48 [17] => 206 [18] => 1224462300 [19] => 0.0 [20] => 3 [21] => 48 [22] => 206 [23] => 1224462600 [24] => 0.0); ?> so this array has 25 values but there could be up to 1000 or more. I want to take this array and combine it with an array with keys for the values that you see here. I.E <?php $rep_keys = array("Server", "Dest", "Test", "Time", "Packets"); ?> So that I have <?php Array ( [server] => 3 [Dest] => 48 [Test] => 206 [Time] => 1224460800 [Packets] => 0.0 [server] => 3 [Dest] => 48 [Test] => 206 [Time] => 1224461100 [Packets] => 0.0 [server] => 3 [Dest] => 48 [Test] => 206 [Time] => 1224461400 [Packets] => 0.0 [server] => 3 [Dest] => 48 [Test] => 206 [Time] => 1224462300 [Packets] => 0.0 [server] => 3 [Dest] => 48 [Test] => 206 [Time] => 1224462600 [Packets] => 0.0); ?> So $rep_keys is always the same but $rep_array always changes in length but always has the 5 fields over and over. I just can't see how to loop through this to add the keys to each section. Please help (edited by kenrbnsn to add tags) Link to comment https://forums.phpfreaks.com/topic/131379-solved-combine-different-size-arrays/ Share on other sites More sharing options...
bobbinsbro Posted November 4, 2008 Share Posted November 4, 2008 i imagine something like this would work: $loopCount = count($rep_array); for($i = 0; $i < $loopCount; $i+5){ $combinedArray[] = array($rep_keys[0] => $rep_array[$i], $rep_keys[1] => $rep_array[$i+1], $rep_keys[2] => $rep_array[$i+2], $rep_keys[3] => $rep_array[$i+3], $rep_keys[4] => $rep_array[$i+4]); } Link to comment https://forums.phpfreaks.com/topic/131379-solved-combine-different-size-arrays/#findComment-682299 Share on other sites More sharing options...
kenrbnsn Posted November 4, 2008 Share Posted November 4, 2008 You can't have the same key more than once in an array. Ken Link to comment https://forums.phpfreaks.com/topic/131379-solved-combine-different-size-arrays/#findComment-682301 Share on other sites More sharing options...
F1Fan Posted November 4, 2008 Share Posted November 4, 2008 Something like this: $rep_array2 = array(); for ($i=0;$i<(count($rep_array)/5);$i++){ $rep_array2['Server'][] = $rep_array[$i]; $rep_array2['Dest'][] = $rep_array[$i+1]; $rep_array2['Test'][] = $rep_array[$i+2]; $rep_array2['Time'][] = $rep_array[$i+3]; $rep_array2['Packets'][] = $rep_array[$i+4]; } Link to comment https://forums.phpfreaks.com/topic/131379-solved-combine-different-size-arrays/#findComment-682303 Share on other sites More sharing options...
Barand Posted November 4, 2008 Share Posted November 4, 2008 try <?php $rep_array = Array ( 0 => 3, 1 => 48, 2 => 206, 3 => 1224460800, 4 => 0.0, 5 => 3, 6 => 48, 7 => 206, 8 => 1224461100, 9 => 0.0, 10 => 3, 11 => 48, 12 => 206, 13 => 1224461400, 14 => 0.0, 15 => 3, 16 => 48, 17 => 206, 18 => 1224462300, 19 => 0.0, 20 => 3, 21 => 48, 22 => 206, 23 => 1224462600, 24 => 0.0); $rep_keys = array("Server", "Dest", "Test", "Time", "Packets"); function combine5 ($keys, $array) { $tmp = array_chunk($array, 5); $result = array(); foreach ($tmp as $data) { $result[] = array_combine ($keys, $data); } return $result; } $new = combine5($rep_keys, $rep_array); echo '<pre>', print_r($new, true), '</pre>'; ?> Link to comment https://forums.phpfreaks.com/topic/131379-solved-combine-different-size-arrays/#findComment-682304 Share on other sites More sharing options...
kenrbnsn Posted November 4, 2008 Share Posted November 4, 2008 To take this to the next level: <?php $rep_array = Array ( 3, 48, 206, 1224460800, 0.0, 3, 48, 206, 1224461100, 0.0, 3, 48, 206, 1224461400, 0.0, 3, 48, 206, 1224462300, 0.0, 3, 48, 206, 1224462600, 0.0); $rep_keys = array("Server", "Dest", "Test", "Time", "Packets"); $rep_array2 = array(); $max = count($rep_array); for ($i=0;$i<$max;$i){ foreach ($rep_keys as $k) { if (!is_array($rep_array2[$k])) $rep_array2[$k] = array(); $rep_array2[$k][] = $rep_array[$i++]; } } echo '<pre>' . print_r($rep_array2,true) . '</pre>'; ?> Ken Link to comment https://forums.phpfreaks.com/topic/131379-solved-combine-different-size-arrays/#findComment-682319 Share on other sites More sharing options...
wrenbjor Posted November 4, 2008 Author Share Posted November 4, 2008 Barand's code is what I am looking for. Thanks a lot!!! Link to comment https://forums.phpfreaks.com/topic/131379-solved-combine-different-size-arrays/#findComment-682326 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.