/    Sign up×
Community /Pin to ProfileBookmark

re-arranging array elements

i have a array of string elements like this:

$myArray = array(“apple”,”orange”,”pear”,”peach”);

if i select pear, i want the array to be searched
and if ‘pear’ is found, i want to move it from where
it is to the front of the array.

is there an easy way to do this?

i can detect that ‘pear’ exists using in_array(‘pear’,$my_array),
but can i simply change the key to zero to make it the first element?

or do i have to loop through the array to find it’s index and store it as a variable($found), then unset it, and then use unshift($my_array,$found);

wouldn’t reorganizing the key be faster since i’m not actually deleting the element.

if so how can i code it to simply change the key to move that element to the front of the array? i can do it with a loop but it seems like just changing the key would be much faster.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@bsmbahamasauthorSep 13.2010 — this does not rearrange the pointers, this simply finds the element's key

and unsets it, and then prepends it to the front of the array.

[CODE]
<?php
$myArray = array("apple","orange","pear","peach");
$element = "pear";
if ( in_array($element,$myArray ) )
{
//show original array
echo "<p><pre>"; print_r($myArray); echo "</pre></p>";
//find the key
$key = array_search($element, $myArray);
//delete the element
unset($myArray[$key]);
//prepend to the front of the array
array_unshift($myArray,$element);
//show updated array
echo "<p><pre>"; print_r($myArray); echo "</pre></p>";
}
?>
[/CODE]
Copy linkTweet thisAlerts:
@NogDogSep 13.2010 — The problem with only changing the key would be if another element already had that key, in which case it would be overwritten by the new value. First thing that comes to mind -- which means there is likely a better way ? -- would be:
[code=php]
$array = array_unique(array_merge(array($value), $array));
[/code]


This assumes that you in fact do not want/need duplicate values in the array.
Copy linkTweet thisAlerts:
@bsmbahamasauthorSep 13.2010 — thanks nogdog seems intuitive, but doesn't array unique keep the keys and just set them to null/undefined meaning that my array would be unique but may contain some undefined values too? i might be confusing it with javascript arrays though.

i need to lookup array merge to see how that works too.

thanks.

so far the solution i posted above works well too. the script i'm running checks for existing elements to make sure it is not in the array.

in the example i supplied it would check to see if the fruit existed before adding it, and if it already existed it unsets it and then prepends it to the array, so i can add new unique elements or move the existing matching element to the top, it's gonna re-arrange content on my page based on what you clicked on in the past to keep content you like near the top of the page, it uses cookies and saves preferences on the server

thanks much
Copy linkTweet thisAlerts:
@NogDogSep 13.2010 — Well, you could add array_values() into it:
[code=php]
$array = array_values(array_unique(array_merge(array($value), $array)));
[/code]

At this point it may be getting overly complex for the sake of a one-liner. Perhaps...
[code=php]
$ix = array_search($value, $array);
unset($array[$ix]);
$array = array_merge(array($value), $array);
[/code]

?
Copy linkTweet thisAlerts:
@bsmbahamasauthorSep 13.2010 — yes that does look very elegant and to the point, and a quick check of the manual says that array_search will only retrun the first value. shouldn't be more than one match anyways because i use in_array() to check for matches before adding new elements.

thanks again.
×

Success!

Help @bsmbahamas spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...