Brandon Jaeger Posted August 29, 2008 Share Posted August 29, 2008 Let's say I have 4 sets of 3 numbers and I wanted to get the first number out of every set and store it in a 2d array along with the other 3 as part of the array. How would I do that? Example: The sets of numbers: .[0]1, 2, 3 .[1]4, 5, 6 .[2]7, 8, 9 .[3]10, 11, 12 $array[0][0] = 1; $array[1][0] = 4; $array[2][0] = 7; $array[3][0] = 10; $array[0][1] = 2; $array[1][1] = 5; $array[2][1] = 8; $array[3][1] = 11; $array[0][2] = 3; $array[1][2] = 6; $array[2][2] = 9; $array[3][2] = 12; Hopefully I'm making sense to you guys! Thanks in advance. Brandon Link to comment https://forums.phpfreaks.com/topic/121816-solved-how-would-i-do-this-with-an-array/ Share on other sites More sharing options...
Ken2k7 Posted August 29, 2008 Share Posted August 29, 2008 Are the arrays named like that? $len = count($array); $ct = -1; $new_array = array(); while (++$ct < $len) { $clen = count($array); $cn = -1; $tmp = array(); while (++$clen < $clen) { $tmp[] = array($array[$ct][$clen]; } $new_array[] = $tmp; } Link to comment https://forums.phpfreaks.com/topic/121816-solved-how-would-i-do-this-with-an-array/#findComment-628481 Share on other sites More sharing options...
Brandon Jaeger Posted August 29, 2008 Author Share Posted August 29, 2008 The string starts out like this: "1234 5678 91011" and is then split. I forgot to mention that. Sorry. Link to comment https://forums.phpfreaks.com/topic/121816-solved-how-would-i-do-this-with-an-array/#findComment-628482 Share on other sites More sharing options...
Ken2k7 Posted August 29, 2008 Share Posted August 29, 2008 The string starts out like this: "1234 5678 91011" and is then split. I forgot to mention that. Sorry. Uh wait, I'm confused. I think I misunderstood. Link to comment https://forums.phpfreaks.com/topic/121816-solved-how-would-i-do-this-with-an-array/#findComment-628483 Share on other sites More sharing options...
Third_Degree Posted August 29, 2008 Share Posted August 29, 2008 Are the sets of numbers already in an array? Link to comment https://forums.phpfreaks.com/topic/121816-solved-how-would-i-do-this-with-an-array/#findComment-628485 Share on other sites More sharing options...
Brandon Jaeger Posted August 29, 2008 Author Share Posted August 29, 2008 Okay. Scenario: I have 4 sets of 3 numbers like so: 1 2 3 4 5 6 7 8 9 10 11 12 I want to store each number in each set so I can get the mins and max's of all sets combined later. I'm not sure how to explain it Here. My code might help: <?php $Origin_1 = $_POST['Origin1']; $Origin_2 = $_POST['Origin2']; $Origin_3 = $_POST['Origin3']; $Origin_4 = $_POST['Origin4']; $Origins_Post = array(0 => $Origin_1 , $Origin_2 , $Origin_3 , $Origin_4); $Replace_Array = array(0 => ' x, ' , ' y, ' , ' z'); if( isset( $_POST['Convert'] ) ) { for( $i = 0; $i < 4; $i++ ) { for( $n = 0; $n < 3; $n++ ) { if( $n != 2 ) $Origins_Post[$i] = str_replace( $Replace_Array[$n] , " " , $Origins_Post[$i] ); else $Origins_Post[$i] = str_replace( $Replace_Array[$n] , "" , $Origins_Post[$i] ); } } } else if( isset( $_POST['Submit'] ) ) { $Origins[] = array(); for( $i = 0; $i < 4; $i++ ) { $Origins[$i] = explode( $Origins_Post[$i] , " " ); } // Here's where I need it // Compare mins and max's of each 1st number of each set. Same with 2nd and 3rd numbers of each set } php?> Link to comment https://forums.phpfreaks.com/topic/121816-solved-how-would-i-do-this-with-an-array/#findComment-628488 Share on other sites More sharing options...
Ken2k7 Posted August 29, 2008 Share Posted August 29, 2008 I'm not sure I understand. How do you want those numbers stored? It's in 4 sets of 3 numbers each right now. So what should it be? Link to comment https://forums.phpfreaks.com/topic/121816-solved-how-would-i-do-this-with-an-array/#findComment-628490 Share on other sites More sharing options...
Third_Degree Posted August 29, 2008 Share Posted August 29, 2008 So $_POST['Origin1'] would, for example, contain the string '123'? I'm just trying to find out what you are dealing with so I can help you effectively. Link to comment https://forums.phpfreaks.com/topic/121816-solved-how-would-i-do-this-with-an-array/#findComment-628492 Share on other sites More sharing options...
Brandon Jaeger Posted August 29, 2008 Author Share Posted August 29, 2008 $_POST['Origin1'] would contain the string '123 456 789'. It is then split (using explode(); with the a space as the delimiter) into an array like so: Array { [0] => '123', [1] => '456', [2] => '789' } I want to store the other 3 numbers in the same array like that. Then I want them sorted into a 2D array. I work with Small and not PHP as much, so I'm not that good with PHP arrays yet. Basically what I'm trying to do is take the 4 sets of numbers, store each 1st, 2nd, and 3rd number in it's own array for each of the 4 sets. The array should look like it does in my first post. I then want to get the max values of the 1st numbers in each set like so: $max = max( $array[0][0] , $array[1][0] , $array[2][0] , $array[3][0] ); Sorry for being so hard to understand... ??? Link to comment https://forums.phpfreaks.com/topic/121816-solved-how-would-i-do-this-with-an-array/#findComment-628497 Share on other sites More sharing options...
Brandon Jaeger Posted August 29, 2008 Author Share Posted August 29, 2008 I got it working by using more variables instead of storing them in a 2D array. <?php $Origin_1 = $_POST['Origin1']; $Origin_2 = $_POST['Origin2']; $Origin_3 = $_POST['Origin3']; $Origin_4 = $_POST['Origin4']; $Origins_Post = array(0 => $Origin_1 , $Origin_2 , $Origin_3 , $Origin_4); $Replace_Array = array(0 => ' x, ' , ' y, ' , ' z'); if( isset( $_POST['Convert'] ) ) { for( $i = 0; $i < 4; $i++ ) { for( $n = 0; $n < 3; $n++ ) { if( $n != 2 ) $Origins_Post[$i] = str_replace( $Replace_Array[$n] , " " , $Origins_Post[$i] ); else $Origins_Post[$i] = str_replace( $Replace_Array[$n] , "" , $Origins_Post[$i] ); } } } else if( isset( $_POST['Submit'] ) ) { $Origins_1st_Array = explode( " " , $Origins_Post[0]); $Origins_2nd_Array = explode( " " , $Origins_Post[1]); $Origins_3rd_Array = explode( " " , $Origins_Post[2]); $Origins_4th_Array = explode( " " , $Origins_Post[3]); $MaxVals = array(); $MinVals = array(); for( $i = 0; $i < 3; $i++ ) { $MaxVals[$i] = max( $Origins_1st_Array[$i] , $Origins_2nd_Array[$i] , $Origins_3rd_Array[$i] , $Origins_4th_Array[$i] ); $MinVals[$i] = min( $Origins_1st_Array[$i] , $Origins_2nd_Array[$i] , $Origins_3rd_Array[$i] , $Origins_4th_Array[$i] ); } echo "MAXES: " . $MaxVals[0] . " " . $MaxVals[1] . " " . $MaxVals[2] . "<br \>\n"; echo "MINS: " . $MinVals[0] . " " . $MinVals[1] . " " . $MinVals[2]; } php?> Link to comment https://forums.phpfreaks.com/topic/121816-solved-how-would-i-do-this-with-an-array/#findComment-628524 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.