0

I have an string like this:-

$sa_position = "state/Gold,International/Participant,School/Participant,School/Bronze,national/Bronze,School/Gold";

And I want filter this string somehow to get top 3 prizes and sort by prizes(e.g. state/Gold,state/Gold,national/Bronze)

1
  • 3
    php.net/explode for starter. And you'd better explain your goal with no code, as it's misleading. Commented Apr 9, 2011 at 5:40

3 Answers 3

1

You do not have a delimiter on the explode function.

explode ( string $delimiter , string $string [, int $limit ] )
Sign up to request clarification or add additional context in comments.

4 Comments

explode is not his only problem
I realise and am currently researching a solution.
Yeah. A solution. "Welcome to Stackowerflow - a FREE codez site!"
@Col. Shrapnel, I think I did a lot of free coding in my answer, but I can't help letting a newbie savor the power of list reduce - although in a bizarre implementation. He might become a lisper in the future.
0

Tada! Here is your sort. Simple but elegant :)

<?php
$sa_position = explode(",", "state/Gold,International/Participant,School/Participant,School/Bronze,national/Bronze,School/Gold");
//$new         = sortMedals($sa_position, array('Gold', 'Silver', 'Bronze', 'Participant'));
$gold = array();
$silver = array();
$bronze = array();
$participated = array();
foreach($sa_position as $item)
{
    if(stripos($item, "silver"))
        $silver[] = $item;
    else if(stripos($item, "bronze"))
        $silver[] = $item;
    else if(stripos($item, "gold"))
        $gold[] = $item;
    else if(stripos($item, "participant"))
        $participated[] = $item;
}
$new = array_merge($gold, $silver, $bronze, $participated);
print_r($new);
?>

3 Comments

I cant help but think he is ganna take the code and not mark an answer O_O
if I want to sort by level(e.g. International/Gold, state/Gold, school/Gold,) after sort by prizes, the code is how to write?
you would do the same foreach sorting on each of the medals but with different words instead of 'bronze' 'silver' gold'
0

There's quite a few problems in your code. Here's what you should do:

  1. explode needs a delimiter. You want to separate it by commas, so do explode(",","state/Gold,International/Participant,School/Participant,School/Bronze,national/Bronze,School/Gold").
  2. You should not use array_diff: in fact, the way you did it would remove just the elements that only contain 'Participant' - while they're more complex strings.

What you need is a beautiful function called array_filter. It's one of the few really great things in PHP, so bear me for a little while I explain how it works.

It takes two arguments: the array, and a function.

It returns an array containing only the elements that, once passed to the function, will return true.

Let's come back to your particular case. To check if a string contains a substring (that is, to check if in a given array element there is a 'Participant' string) you can use strpos($haystack, $needle). It will return the position of your substr, or FALSE if it is not present.

Another concept (solution is coming) we will use is pretty new in php, and is called "anonymous function". It's a function created on the fly, without a name, usually to be used as callback.

Here is the code:

 $string = "state/Gold,International/Participant,School/Participant,School/Bronze,national/Bronze,School/Gold";
 $new_array = array_filter(
   explode(",",$string), //so, every element of this array gets checked against
   function ($var) {     //this function here. if true is returned, it goes in $new_array
      return (strpos($haystack, 'Participant')=== NULL);
   }
 );

5 Comments

You're following his wrong algorightm but fail to achieve his goal
fuuu, didn't read the updated question. anyway, read this thing. it took me 7 minutes.
@Col. Shrapnel, I could do it with a one-line regex - just wanted to write something instructive. Also, his goal was different at the beginning. Also, please avoid assuming anyone != you's a newbie.
nope, I added nothing in his question but only deleted code. His code was different but goal was the same ;) Always read the question through - it helps. Actually it took me 3 times to read until I got to realize
@Col. Shrapnel, you're so right. I was mislead by "and I try to use array_diff to filter this array", thinking it was in fact about filtering out those particular results - the sorting being optional. I stand corrected.

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.