Jump to content

[SOLVED] a little 'foreach' problem


Heylance

Recommended Posts

yup! it's official, not quite getting it. a little help please!!!

 

I'm trying (again) to sort an array by the strlen of the values.

This is a solution I got here before, but have run into a snag.

 

$my_cars = array('chevy', 'chevys', 'dodge', 'dodges', 'lancer', 'mitsubishi', 'nissan');

if($my_cars){
   foreach($my_cars as $key => $val){
     $car = strlen($val);
     $car_list[$car] = $val;
  }
}

echo "<pre>\n";
print_r($car_list);
echo "</pre>\n";

Array
(
    [5] => dodge
    [6] => nissan
    [10] => mitsubishi
)

ksort($car_list);
$car_list = array_reverse($car_list);

echo "<pre>\n";
print_r($car_list);
echo "</pre>\n";

Array
(
    [0] => mitsubishi
    [1] => nissan
    [2] => dodge
)

 

Where did chevys, dodges and lancer go?? They got lost back in the foreach section.

 

As you can see, I am trying to recreate the array and using the strlen to sort on. I was given this solution a few weeks ago. It seems that if any of the values are the same strlen, only the last one in the array is kept. What to do?

 

Any suggestions.... Is it possible to do???

Link to comment
Share on other sites

Yes, it is possible. The problem is that you are using the string length as the array index. Since 'chevys' and 'lancer' have the same string length the index of [6] first gets defined as 'chevys', then it gets redefined as 'lancers'.

 

Here's one solution:

 

<?php

function sortByLength($a, $b) {
    if (strlen($a)==strlen($b)) {
        //If length =, sort by alpha
        return strcmp($a, $b);
    }
    return (strlen($a)>strlen($b)) ? 1 : -1; 
}

$my_cars = array('chevy', 'chevys', 'dodge', 'dodges', 'lancer', 'mitsubishi', 'nissan');

if($my_cars){
  usort($my_cars, "sortByLength");
}

echo "<pre>\n";
print_r($my_cars);
echo "</pre>\n";

?>

Link to comment
Share on other sites

Here's another approach

<?php

$my_cars = array('chevy', 'chevys', 'dodge', 'dodges', 'lancer', 'mitsubishi', 'nissan');

if($my_cars){
   foreach($my_cars as $key => $val){
     $tmp_list[$val] = strlen($val);
  }
  asort($tmp_list);
  $my_cars = array_keys($tmp_list);
}

echo "<pre>\n";
print_r($my_cars);
echo "</pre>\n";

?>

Link to comment
Share on other sites

Thanks mjdamato,

 

Bingo, you made it look too easy!

Your second solution was the easiest for me to get a grip on. I threw a little arsort it and Baaammm!

Just what the doctor ordered.

Thanks so much!!!!

 

On the first one, I will have to study a little, which is good..

One question

On the line:

  return (strlen($a)>strlen($b)) ? 1 : -1;

 

What is the ? for?

Remember I'm still a neeewwwbie.

 

Link to comment
Share on other sites

That is a shorthand version of an If/Else statement. I believe the correct name is the ternarry operator. Basically, that line states is the length of A is greater than the length of B return 1, else return -1.

 

Here is a short article on it: http://php.codenewbie.com/articles/php/1480/The_Ternary_Operator-Page_1.html

 

 

Personally, I woudl think the first example is more efficient, but I have not tested that hypothesis.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.