Welcome to WebmasterWorld Guest from 54.196.245.74

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

How do you swap two values in an array?

     
11:11 am on Nov 12, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 12, 2006
posts:2493
votes: 22


Hello. I've had a good look through the docs, but I can't find a ready made php function to swap two values in an array, so I was hoping someone might know one.
Here's my problem...

I've got an array like this

Array( [0] => sydney [1] => paris [2] => london [3] => madrid )


The elements are in no particular order. the user chooses whichever order they want.

But i want them to be able to move the values up and down the list, one position at a time. So if they moved paris up the list, it would change to this

Array( [0] => paris [1] => sydney [2] => london [3] => madrid )


is there a function to swap two values around?
2:53 pm on Nov 12, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 24, 2005
posts:697
votes: 0


Here you go, It was fun so i made 2 more functions, allowing us to swap by value or by index, just for the fun or it. they may be useful for someone reading this post

<pre>
<?php
$cities=array();
$cities[]="Sydney";
$cities[]="Paris";
$cities[]="London";
$cities[]="Madrid";



print_r($cities);

/*
$cities=swapByValues($cities,"Paris","Madrid");
print_r($cities);
$cities=swapByIndex($cities,0,1);
print_r($cities);
*/

$cities=moveUp($cities,"Paris");
$cities=moveDown($cities,"London");


print_r($cities);

function moveUp($values,$value)
{
$index1=array_search($value,$values);
if($index1==0)
{
return $values;
}
else
{
$index2=$index1-1;
$value1=$values[$index1];
$value2=$values[$index2];
$values[$index1]=$value2;
$values[$index2]=$value1;
return $values;
}
}

function moveDown($values,$value)
{
$index1=array_search($value,$values);
$lastIndex=count($values)-1;
if($index1==$lastIndex)
{
return $values;
}
else
{
$index2=$index1+1;
$value1=$values[$index1];
$value2=$values[$index2];
$values[$index1]=$value2;
$values[$index2]=$value1;
return $values;
}
}

function swapByValues($values,$value1,$value2)
{
$index1=array_search($value1,$values);
$index2=array_search($value2,$values);
$values[$index1]=$value2;
$values[$index2]=$value1;
return $values;
}

function swapByIndex($values,$index1,$index2)
{
$value1=$values[$index1];
$value2=$values[$index2];
$values[$index1]=$value2;
$values[$index2]=$value1;
return $values;
}


?>
</pre>
3:30 pm on Nov 12, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Apr 30, 2007
posts:1394
votes: 0


There is no direct function. Here is a function to exchange 2 values of an array:


function exchange_array_elements($src_array, $index) {
if( empty($src_array) || !is_array($src_array) || count($src_array) < 2 ) return $src_array;
$j = count($src_array);

$index = max($index, 1);
$index = min($index, $j-1);

$replace = $src_array[$index];
array_splice($src_array, $index-1, 0, $replace);
array_splice($src_array, $index+1, 1);
return $src_array;
}
// test
$index = 2;
$src_array = array('sydney', 'paris', 'london', 'madrid');
$result_array = exchange_array_elements($src_array, $index);
print_r($result_array);



It will swap $index with $index-1 after validating the array and index.
8:01 am on Nov 16, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 24, 2005
posts:697
votes: 0


No interest in the solution?
9:13 am on Nov 16, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 12, 2006
posts:2493
votes: 22


sorry guys, i reckon your ideas would have been good. i managed to get a really easy one that only takes a few lines.
if you feed the position the user wants to 'move up' into $_GET['pos'], and the array of places is called $value, all i did was this

$position1 = $_GET['pos'];

$position2 = $position1 - 1;

$tmpa = $values[$position1];

$tmpb = $values[$position2];

$values[$position1] = $tmpb;

$values[$position2] = $tmpa;


and for the 'move down' version, you just change
$position2 = $position1 - 1

to
$position2 = $position1 + 1


suprised php doesnt have its own function for something like this though. i think they must everything but
12:54 pm on Nov 16, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Feb 22, 2009
posts:1396
votes: 0


>>No interest in the solution?

Thanks for that anyango, I'm sure that people will find this little snippet quite useful, I shall have a play with it myself as I can see a potential application for it.

londrum, good that you found a work around for this little issue, happy coding!

Cheers,
MRb
7:37 pm on Nov 16, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Apr 30, 2007
posts:1394
votes: 0


that only swaps two adjacent values, doesn't really exchange. And you will endup with a corrupt array if the passed value is outside the array boundaries.

If I had to drag/drop a row within a table an adjacent swap won't do

The elements are in no particular order. the user chooses whichever order they want.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members