0

I have an array that looks like this:

Array
(
    [0] => [email protected] 20140827
    [1] => [email protected] 20130827
    [2] => [email protected] 20140825
    [3] => [email protected] 20120825
    [4] => [email protected] 20140826
)

Now I want to sort this array in php based on the numbers only so ignoring the e-mail address in the sort process.

6
  • 2
    Did you take a look at the list of sorting functions in the manual to see if any of them can help you? Commented Aug 27, 2014 at 11:48
  • ksort could help you there : php.net/manual/en/function.ksort.php Commented Aug 27, 2014 at 11:48
  • 1
    @Logar: ksort cannot help here. Commented Aug 27, 2014 at 11:49
  • @Logar ksort will give him aray sort by emails Commented Aug 27, 2014 at 11:50
  • @Jon: I totally misunderstood OP question, my bad Commented Aug 27, 2014 at 11:50

4 Answers 4

4

For example, assuming entries are always like email space number:

usort($ary, function($a, $b) {
    $a = intval(explode(' ', $a)[1]);
    $b = intval(explode(' ', $b)[1]);
    return $a - $b;
});

or in more complicated but efficient way using Schwartzian transform:

$ary = array_map(function($x) {
    return [intval(explode(' ', $x)[1]), $x];
}, $ary);

sort($ary);

$ary = array_map(function($x) {
    return $x[1];
}, $ary);
Sign up to request clarification or add additional context in comments.

Comments

1
<?php
$data = Array(0 => '[email protected] 20140827',
    1 => '[email protected] 20130827',
    2 => '[email protected] 20140825',
    3 => '[email protected] 20120825',
    4 => '[email protected] 20140826'
);

$count_data = count($data);

for($i=0;$i<$count_data;$i++)
{
    $new_data[trim(strstr($data[$i], ' '))]=$data[$i];
}
echo "<pre>"; print_r($new_data);
?>

this will return you

Array
(
    [20140827] => [email protected] 20140827
    [20130827] => [email protected] 20130827
    [20140825] => [email protected] 20140825
    [20120825] => [email protected] 20120825
    [20140826] => [email protected] 20140826
)

Now you can sort according to need by Key

1 Comment

You didn't actually sort the array, you merely mention it in a footnote to your code-only answer. Please improve this answer with an actual sort call and some explanation as to how your answer works and why it is a good idea.
0

You could loop through the array, explode the string on a space, ' ', then set part one $explodedString[1] as the key in a new array, then use ksort on the new array.

Untested code.

$oldArr;
$newArr = array();

foreach($oldArr as $oldStr){
    $tmpStr = explode(' ', $oldStr);
    $newArr[$tmpStr[1]] = $tmp[0]; //You could use $oldStr if you still needed the numbers.
}

ksort($newArr);

Comments

0

Use substr_replace() to remove the unwanted prefix from a temporary copy of your array and sort the original array by that. Demo

array_multisort(
    substr_replace($array, '',  0, -8),
    $array
);

var_export($array);

Output:

array (
  0 => '[email protected] 20120825',
  1 => '[email protected] 20130827',
  2 => '[email protected] 20140825',
  3 => '[email protected] 20140826',
  4 => '[email protected] 20140827',
)

If the number of characters at the end are variable, use whatever method you like to sanitize the strings. In this case, preg_replace('/.* /', '', $array) could have also been used.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.