Jump to content

[SOLVED] How would I do this with an array?


Brandon Jaeger

Recommended Posts

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

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;
}

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?>

$_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... ???

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?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.