0

I have a PHP array that looks like this

$alphabet= array('a','b','c')

$alphabet is a input i need a result like $result

Expected output:

$result= array(
  [0]=> "a"
  [1]=> "b"
  [2]=> "c"
  [3]=> "ab"
  [4]=> "ac"
  [5]=> "bc"
  [6]=> "abc"
)

Note: here, I would not like sorting to use. Thanks!

6
  • 1
    and your question is? Commented May 21, 2015 at 8:23
  • Have you tried anything to achieve this? Commented May 21, 2015 at 8:24
  • $alphabet is a given input and I want a result $results Commented May 21, 2015 at 8:26
  • 1
    Please update your question with, well,.. your specific question. What have you tried? Commented May 21, 2015 at 8:28
  • stackoverflow.com/questions/16311003/… Commented May 21, 2015 at 8:31

1 Answer 1

0

Use usort and costum sort function:

$array = array("a", "bc", "bb", "aa", "cc", "bb");

function sortByValueLength($a, $b)
{
    $aLength = mb_strlen($a, 'utf-8');
    $bLength = mb_strlen($b, 'utf-8');
    if ($aLength == $bLength) {
        return strcmp($a, $b);
    }

    return $aLength - $bLength;
}

usort($array, 'sortByValueLength');

var_export($array);

Example result here

Sign up to request clarification or add additional context in comments.

1 Comment

Question is not related to sort I think

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.