Page 1 of 1

problem swaping the two element of array

Posted: Wed Jan 08, 2003 2:16 pm
by pratik678
Hi everyone,

i am having problem swaping two element of array, can anyone help me....
I am getting following warrings, although it work correctly....I am idecating the line for error by ****
Warning: Undefined offset: 1 in D:\Inetpub\wwwroot\set\pratiktest\next.php on line 163

Warning: Undefined offset: 2 in D:\Inetpub\wwwroot\set\pratiktest\next.php on line 154

Warning: Undefined offset: 3 in D:\Inetpub\wwwroot\set\pratiktest\next.php on line 163

Warning: Undefined offset: 4 in D:\Inetpub\wwwroot\set\pratiktest\next.php on line 163

Warning: Undefined offset: 5 in D:\Inetpub\wwwroot\set\pratiktest\next.php on line 163

Warning: Undefined offset: 6 in D:\Inetpub\wwwroot\set\pratiktest\next.php on line 158

Code: Select all

function swap_twoelement_2Darray(&$array,$first_element, $second_element)
{
		//make sure the first_element is smaller then the second_element
		//or do a swap
		if($first_element > $second_element)
		{
			$tempelement = $first_element;
			$second_element = $first_element;
			$first_element = $second_element;
		}
		
		//intialize conter to zero
		$columncount=0;
		
		//count number of colums in a array
		while (list ($key,$value) = each($arrayї0]))
		{
				$columncount++;
		}
		//count number of rows in a array		
		$rowcount = sizeof ($array);

		//check the dimensions to be swaped
			for($rowCounter = 0;$rowCounter < $rowcount;$rowCounter++)
			&#123;
				for($colCounter = 0; $colCounter < $columncount; $colCounter++)
				&#123;
					if($first_element == $colCounter)
					&#123;
						 ****$temp_array&#1111;$rowCounter]&#1111;$first_element] = $array&#1111;$rowCounter]&#1111;$first_element];						 
					&#125;			
					else if ($second_element == $colCounter)		
					&#123;
****						$array&#1111;$rowCounter]&#1111;$first_element] =  $array&#1111;$rowCounter]&#1111;$second_element];
						$array&#1111;$rowCounter]&#1111;$second_element] = $temp_array&#1111;$rowCounter]&#1111;$first_element];
					&#125;
					else
					&#123;
****						$array&#1111;$rowCounter]&#1111; $colCounter] =  $array&#1111;$rowCounter]&#1111; $colCounter];
					&#125;
					
				&#125;
			&#125;
		
&#125;

Posted: Wed Jan 08, 2003 2:41 pm
by volka
you're accessing array elements that does not exist (yet) in $temp_array.
But why so complicated? As fas as I understood your function

Code: Select all

<pre><?php
$arr = array();
$arr[] = array(1,2,3);
$arr[] = array('a','b','c');
$arr[] = array('A','B','C');


function swap2D(&$element, $i, $idx)
{
	$temp = $element[$idx[0]];
	$element[$idx[0]]=$element[$idx[1]];
	$element[$idx[1]]=$temp;
}

array_walk($arr, 'swap2D', array(1,2));
print_r($arr);
?></pre>
should do pretty much the same

Still having Trouble....

Posted: Thu Jan 09, 2003 11:20 am
by pratik678
Hi Friends,

I am having some Trouble, one of you help me but. i am still having trouble ot swap the column of the 2D Array. the person who help me out is using the array, index=0 and 2dimention, but i want ot swap the whole column. So please help me i have posed my code in the first message please look at that and these is a good code, but unfortunatly i don't uderstand how to use it.

function swap2D(&$element, $i, $idx)
{
$temp = $element[$idx[0]];
$element[$idx[0]]=$element[$idx[1]];
$element[$idx[1]]=$temp;
}

Posted: Thu Jan 09, 2003 12:15 pm
by volka
array_walk applies a function to each element of an array.
swap2D should exchange to elements of an array. Which elements is given in the third parameter as $idx[0] and $idx[1].
So calling array_walk with swap2D as callback-function should swap the elements in the second dimension for each element of the first dimesion.
Just try the example ;)
But maybe we should walk through. As mentioned array_walk will aply the callback to each element of the given array (parameter 1). So the first time swap2D is called $element is equal to array(1,2,3) and $idx is array(1,2).
  • $temp = $element[1]; // = 2
  • $element[1]=$element[2]; // = 3
  • $element[2]=$temp; // = 2
  • $element == array(1,3,2)
since the element was passed by reference (&$element) changes will have effects on the original array.
After array_walk applied the callback to each element of $arr it is equal to

Code: Select all

$arr == array( array(1,3,2), array('a','c','b'), array('A', 'C', 'B'))
which is what you wanted (I think) :D

illuminate us

Posted: Thu Jan 09, 2003 12:16 pm
by AVATAr
Illuminate us with an example of what you want to do

:wink:

Look at my file tell me what i am doing wrong

Posted: Thu Jan 09, 2003 12:19 pm
by pratik678
I am tring everything i can....Please have a look at my files

-----------------------data------------------------------
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15

Code: Select all

######################default.php################
<HTML>
<BODY>

<FORM METHOD=POST ACTION="next.php">

<P><TEXTAREA NAME=MyData COLS=65 ROWS=10>
Blah
</TEXTAREA></P>

<p>
	<INPUT TYPE=SUBMIT>
</p>
</FORM>
</BODY>
</HTML>
------------------------------next.php---------------------------------------
<HTML>
<BODY>
<?


function swap2D(&$element, $i, $idx) 
&#123;   
   
   $j  = $idx&#1111;0];  
   echo "$i -----> $j<BR>";	
   $temp = $element&#1111;$i]&#1111;$j];
   echo $element&#1111;0]&#1111;1];
   /*$element&#1111;$i]&#1111;$idx&#1111;0]]=$element&#1111;$i]&#1111;$idx&#1111;1]]; 
   $element&#1111;$i]&#1111;$idx&#1111;1]]=$temp; */
&#125; 

// this is a replacement for the internal explode function so that
//   strings representing 2D arrays (ie matrices) maybe made again
//   into their array format
function double_explode($first_break,$second_break,$string)
&#123;
	// creates array based on the first division character
	$array = explode($first_break, $string);
	
		// loops through the created array in search of any strings
		//   that need to be converted to arrays
		for ($k=0; $k < count($array); $k++)
		&#123;
			// the number of times the second division character
			//   is present
			$count = substr_count($array&#1111;$k], $second_break);
			// if the second division character is present; ie,
			//   if the string needs to be converted to an array
			if ($count>0)
			&#123;
				// creates array from string and puts it in
				//   the main array where the string was
				$array&#1111;$k] = explode($second_break, $array&#1111;$k]);
			&#125;
		&#125;
	return $array;
&#125;

// just as double_explode functions akin to explode, so too does
// double_implode to implode
function double_implode($first_break, $second_break, $array)
&#123;
	$string = "";
	$k=0;
	$num_length = count($array);

	foreach ($array as $key => $value)
	&#123;
			// if the element of the main array is an array
			if (is_array($array&#1111;$key]))
			&#123;
				// append the string of the converted arary to
				// the string being constructed of the whole
				// main array 
				$string .= implode($second_break, $array&#1111;$key]);
			&#125;
			else
			&#123;
				// append the element to the string
				$string .= $array&#1111;$key];
			&#125;
			$k++;
			
			//change here!
			// append the delimiting character to the string
			if ($k<$num_length)
				$string .= $first_break;
	&#125;
			
			return $string;
&#125;

function displayResultSet($array)
&#123;
		//Indicate if the resultset is empty
		if (empty($array))
		&#123;
				echo "<CENTER>\n";
				echo "<BR>\n";
				echo "Action Returned No Rows!";
				echo "</CENTER>\n";
				return ;
		&#125;

				echo "<BR>\n";
				echo "<CENTER>\n";
				echo "<TABLE CELLSPACING=5 CELLPADDING=5 BORDER=3 ";
				echo "BORDERCOLOR="0000FF" >\n";
				echo "<THEAD>\n";
				echo "<TR>\n";

		$keycount = 0;
		//Output each key as a column heading
		while (list ($key,$value) = each($array&#1111;0]))
		&#123;
				$keycount++;
				echo "<TH>";
				echo $key;
				echo "</TH>\n";
		&#125;
				echo "</TR>\n";
				echo "</THEAD>\n";

		//Output each value as elements of a table starting a new row
		//each time the key number of columns is reached
		$rowcount = sizeof ($array);

		for($rowCounter = 0;$rowCounter < $rowcount;$rowCounter++)
		&#123;
			echo "<TR>\n";
			for($colCounter = 0; $colCounter < $keycount; $colCounter++)
			&#123;
				echo "<TD>\n";
				echo " ";
				echo $array&#1111;$rowCounter]&#1111;$colCounter];
				echo " ";
				echo "</TD>\n";
			&#125;
		&#125;
				echo "</TR>\n";
				echo "</TABLE>\n";
				echo "</CENTER>\n";
				echo "<BR>\n";
&#125;

$temparray = double_explode("\n","\t",$MyData);
displayResultSet($temparray); 
array_walk($temparray, 'swap2D', array(3,2));
echo "after swap takes place";
displayResultSet($temparray); 


?>
</BODY>
</HTML>

Posted: Thu Jan 09, 2003 12:32 pm
by volka
$temp = $element[$i][$j];
I'm sorry if this was not clear. $i is set by array_walk and is the index of the outer element.
Both indices for swaping are in the parameter $idx ($idx[0], $idx[1]).
e.g.

Code: Select all

function myWalk($element, $i)
{
   echo $i, '=>', $element, "\n";
}

$arr = array(1=>'just', 'a'=>'an', 'B'=>'example');
array_walk($arr, 'myWalk');
is a simple version of print_r() for an one-dimesional array;

Hi

Posted: Thu Jan 09, 2003 12:41 pm
by pratik678
I figued out that part, but my problem is that . I am trying to use $i as in inbound to access the second demistion. this is what i was tring

function swap2D(&$element, $i, $idx)
{

$j = $idx[0];
echo "$i -----> $j<BR>";
$temp = $element[$i][$j];
echo $element[0][1];
/*$element[$i][$idx[0]]=$element[$i][$idx[1]];
$element[$i][$idx[1]]=$temp; */
}

but it is not printing anything when i print $temp or the $element[0][1];

Thanks

Posted: Thu Jan 09, 2003 1:08 pm
by volka
if a (fully) 2-dimensional array is passed to array_walk the callback will receive an 1-dimensional array in each call. Try this example

Code: Select all

<?php
function myWalkA($element, $i)
{
	echo '<fieldset><legend>', $i, '</legend><pre>';
	
	print_r($element);
	
	echo '<pre></fieldset>';
}


function myWalkB($element, $i)
{
	echo 'element 1 of ', $i, ' is ', $element[1], '<br/>';
}


function myWalkC($element, $i, $param)
{
	echo 'element ', $param , ' of ', $i, ' is ', $element[$param], '<br/>';
}

function myWalkD($element, $i, $idx)
{
	echo 'element ', $idx[0] , ' of ', $i, ' is ', $element[$idx[0]], '<br/>';
	echo 'element ', $idx[1] , ' of ', $i, ' is ', $element[$idx[1]], '<br/>';
}


$arr = array('first'=>array(1,2,3), 'second'=>array('a', 'b', 'c'), 'third'=>array('A', 'B', 'C'));

array_walk($arr, 'myWalkA');
echo '<hr/>';
array_walk($arr, 'myWalkB');
echo '<hr/>';
array_walk($arr, 'myWalkC', 2);
echo '<hr/>';
array_walk($arr, 'myWalkD', array(0,1));
?>
and also try the original swap2D-function ;)