Reputation: 1426
I want to swap only main diagonal elements of matrix of an multidimensional array like below
Before swapping
11 12 13 14 15
21 22 23 24 25
31 32 33 34 35
41 42 43 44 45
51 52 53 54 55
After swapping -it should be
55 12 13 14 15
21 44 23 24 25
31 32 33 34 35
41 42 43 22 45
51 52 53 54 11
What i did is - i am able to get diagonal elements as below, its quite simple but i cannot proceed further
<?php
$array = array (
array( '11','12','13','14','15'),
array( '21','22','23','24','25'),
array( '31','32','33','34','35'),
array( '41','42','43','44','45'),
array( '51','52','53','54','55')
);
foreach ( $array as $key => $val ) {
print "<br>".$val[$key];
}
?>
Upvotes: 0
Views: 1481
Reputation: 1426
Here is working example ;)
<?php
$array = array (
array( '11','12','13','14','15','16'),
array( '21','22','23','24','25','26'),
array( '31','32','33','34','35','36'),
array( '41','42','43','44','45','46'),
array( '51','52','53','54','55','56'),
array( '61','62','63','64','65','66')
);
//print "<pre>";
//print_r($array); exit;
$cnt = count($array);
$i = 0; $j = count($array) - 1;
foreach ( $array as $key => $val ) {
$newArr[$i][$j] = $val[$key];
$j--; $i++;
}
$reversed = array_reverse($newArr);
$result = array_replace_recursive($array,$reversed);
matrix($array,$cnt);
print "=======================<br><br>";
matrix($result,$cnt);
function matrix( $result,$cnt )
{
for($i=0;$i<$cnt;$i++)
{
for($k=0;$k<$cnt;$k++)
{
echo $result[$i][$k]." ";
echo "<br>";
}
}
}
?>
Upvotes: 0
Reputation: 1477
I think what you are missing is being able to access your $array by both 'x' and 'y' coordinates by: $array[$x][$y].
<?php
$array = array (
array( '11','12','13','14','15'),
array( '21','22','23','24','25'),
array( '31','32','33','34','35'),
array( '41','42','43','44','45'),
array( '51','52','53','54','55')
);
print_array($array);
echo "--------------\n";
//swap
for( $i=0; $i<2; $i++ ){
$temp= $array[$i][$i];
$array[$i][$i]= $array[4-$i][4-$i];
$array[4-$i][4-$i]=$temp;
}
print_array($array);
function print_array( $array )
{
for($i=0;$i<5;$i++)
{
for($j=0;$j<5;$j++)
echo $array[$i][$j]." ";
echo "\n";
}
}
?>
I'll let you figure out why the swap loop has $i<2
in it whereas the print loop has $i<5
:)
Yours, TonyWilk
Upvotes: 1
Reputation: 1651
It's sounds like you're a bit lost. This isn't too hard, I suggest you dividing it into smaller problems which might make it easier for you to deal with.
First create a function to get the diagonal array based on matrix size
suggestion
You have a nxn
matrix represented by 2d array a[i][j]
then (pseudo code):
while i < n do:
diagonal << a[i][j]
i,j+=1
Then, create a swap function for the diagonal, there's reverse_array
function in PHP that might help.
Finally, when having reversed-diagonal calculated you can just print the old array and swap its diagonal with the new one.
There are shorter ways and much efficient ones, but in my opinion this how you should do it so next time you're facing this kind of problem you'll know the flow.
Upvotes: 1