jaymc Posted July 21, 2009 Share Posted July 21, 2009 An array comes in as this from a $_POST Array ( [fieldOne] => Array ( [row1] => data1 [row2] => data2 [row3] => data3 [row4] => data4 [row5] => data5 ) [fieldTwo] => Array ( [row1] => data1 [row2] => data2 [row3] => data3 [row4] => data4 [row5] => data5 ) [fieldThree] => Array ( [row1] => data1 [row2] => data2 [row3] => data3 [row4] => data4 [row5] => data5 ) [fieldFour] => Array ( [row1] => data1 [row2] => data2 [row3] => data3 [row4] => data4 [row5] => data5 ) ) I need to take row1, row2, row3, row4, row5 of each child array and push it into a new array using the keyname of the parent array as the keyname for each element in the new array The example below will show you the exact output I need [0] => Array ( [fieldOne] => data1 [fieldTwo] => data1 [fieldThree] => data1 [fieldFour] => data1 ) [1] => Array ( [fieldOne] => data2 [fieldTwo] => data2 [fieldThree] => data2 [fieldFour] => data2 ) [2] => Array ( [fieldOne] => data3 [fieldTwo] => data3 [fieldThree] => data3 [fieldFour] => data3 ) [3] => Array ( [fieldOne] => data4 [fieldTwo] => data4 [fieldThree] => data4 [fieldFour] => data4 ) [4] => Array ( [fieldOne] => data5 [fieldTwo] => data5 [fieldThree] => data5 [fieldFour] => data5 ) I dont think there is a function to do this, may requre some foreach stuff but Im getting no where Can anyone help? Quote Link to comment https://forums.phpfreaks.com/topic/166721-solved-array/ Share on other sites More sharing options...
dzelenika Posted July 21, 2009 Share Posted July 21, 2009 $newArr = array(); foreach($array as $key => $val) { foreach($val as $vKey => $vVal) { $found = false; foreach($newArr as $nKey => &$nVal) { if(in_array($vVal, $nVal)) { $nVal[$key] = $vVal; $found = true; } } if(!$found) { $newArr[] = array($key => $vVal); } } } print_r($newArr); Quote Link to comment https://forums.phpfreaks.com/topic/166721-solved-array/#findComment-879417 Share on other sites More sharing options...
jaymc Posted July 21, 2009 Author Share Posted July 21, 2009 Excellent! Worked first try Am I lacking in ability for spending 2 hours trying to do that? It really confused me, maybe I was looking at it the wrong way Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/166721-solved-array/#findComment-879454 Share on other sites More sharing options...
jaymc Posted July 22, 2009 Author Share Posted July 22, 2009 Actually, sorry. That does not work Where I have used this dummy data for the example data1 data2 data3 data4 Those values can be anything, but you have based your code on them being hard set as data* Here is what the $_POST could come in as Array ( [fieldOne] => Array ( [row1] => cheese [row2] => red [row3] => rain ) [fieldTwo] => Array ( [row1] => beans [row2] => blue [row3] => snow ) ) Which I need converted to [0] => Array ( [fieldOne] => cheese [fieldTwo] => beans ) [1] => Array ( [fieldOne] => red [fieldTwo] => blue ) [2] => Array ( [fieldOne] => rain [fieldTwo] => snow ) So basically the [row1]'s must all be merged together in a new array, all the [row2]'s in another and so on... whilst using the original keyname of the array as each elements key name. The example above shows exactly what is needed Quote Link to comment https://forums.phpfreaks.com/topic/166721-solved-array/#findComment-879964 Share on other sites More sharing options...
Philip Posted July 22, 2009 Share Posted July 22, 2009 <pre><?php // Test array2: $array = array( 'fieldOne' => array( 'row1' => 'cheese', 'row2' => 'red', 'row3' => 'rain', ), 'fieldTwo' => array( 'row1' => 'beans', 'row2' => 'blue', 'row3' => 'snow', ), ); // actual script: foreach($array as $key=>$sub) { $i = 0; foreach($sub as $value) { $final[$i][$key] = $value; $i++; } } // end // print results: print_r($array); print_r($final); ?></pre> Outputs: Array ( [fieldOne] => Array ( [row1] => cheese [row2] => red [row3] => rain ) [fieldTwo] => Array ( [row1] => beans [row2] => blue [row3] => snow ) ) Array ( [0] => Array ( [fieldOne] => cheese [fieldTwo] => beans ) [1] => Array ( [fieldOne] => red [fieldTwo] => blue ) [2] => Array ( [fieldOne] => rain [fieldTwo] => snow ) ) Quote Link to comment https://forums.phpfreaks.com/topic/166721-solved-array/#findComment-879977 Share on other sites More sharing options...
jaymc Posted July 22, 2009 Author Share Posted July 22, 2009 That appears to work. Very small script too. Good work. Quote Link to comment https://forums.phpfreaks.com/topic/166721-solved-array/#findComment-880321 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.