Removeing duplicates inside for/foreach loop.

View: New views
6 Messages — Rating Filter:   Alert me  

Removeing duplicates inside for/foreach loop.

by Tom Shaw-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

$array = array(

  1 => array("order_number" => "10DA0DDEDAF97DB", "order_payment_first_name"
=>  "Mike", "order_payment_last_name" =>  "Smith"),

  2 => array("order_number" => "10DA0DDEDAF97DB", "order_payment_first_name"
=>  "Mike", "order_payment_last_name" =>  "Smith"),

  3 => array("order_number" => "45Y3453245T23T5", "order_payment_first_name"
=>  "Steve", "order_payment_last_name" =>  "Jobs"),

  4 => array("order_number" => "34RTB345T45T456", "order_payment_first_name"
=>  "Frank", "order_payment_last_name" =>  "Capra"),

  5 => array("order_number" => "567U457645645Y6", "order_payment_first_name"
=>  "John", "order_payment_last_name" =>  "Doe"),

  6 => array("order_number" => "7698234T456M963", "order_payment_first_name"
=>  "Bill", "order_payment_last_name" =>  "Williams"));

 

echo('<pre>');

print_r($array);

echo('</pre>');

 

My question is I want to remove duplicate order numbers in other words ditch
*one* of the Mike Smith rows there's a duplicate order number there.

And I need to do it in either a for/foreach loop. Somehow there's got to be
a simple way to check and only output non duplicate order numbers while I'm
looping thru the data. Any help would be greatly appreciated.

 

 

Thanks

php.code@...


Re: Removeing duplicates inside for/foreach loop.

by tedd-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

At 11:50 AM -0500 7/15/08, Niccolo Machiavelli wrote:
>$array = array(

-snip-

>My question is I want to remove duplicate order numbers in other words ditch
>*one* of the Mike Smith rows there's a duplicate order number there.
>
>And I need to do it in either a for/foreach loop. Somehow there's got to be
>a simple way to check and only output non duplicate order numbers while I'm
>looping thru the data. Any help would be greatly appreciated.

Niccolo:

Check out: array_unique

http://www.php.net/array_unique

Cheers,

tedd

--
-------
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: Removeing duplicates inside for/foreach loop.

by Shawn McKenzie :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Niccolo Machiavelli wrote:

> $array = array(
>
>   1 => array("order_number" => "10DA0DDEDAF97DB", "order_payment_first_name"
> =>  "Mike", "order_payment_last_name" =>  "Smith"),
>
>   2 => array("order_number" => "10DA0DDEDAF97DB", "order_payment_first_name"
> =>  "Mike", "order_payment_last_name" =>  "Smith"),
>
>   3 => array("order_number" => "45Y3453245T23T5", "order_payment_first_name"
> =>  "Steve", "order_payment_last_name" =>  "Jobs"),
>
>   4 => array("order_number" => "34RTB345T45T456", "order_payment_first_name"
> =>  "Frank", "order_payment_last_name" =>  "Capra"),
>
>   5 => array("order_number" => "567U457645645Y6", "order_payment_first_name"
> =>  "John", "order_payment_last_name" =>  "Doe"),
>
>   6 => array("order_number" => "7698234T456M963", "order_payment_first_name"
> =>  "Bill", "order_payment_last_name" =>  "Williams"));
>
>  
>
> echo('<pre>');
>
> print_r($array);
>
> echo('</pre>');
>
>  
>
> My question is I want to remove duplicate order numbers in other words ditch
> *one* of the Mike Smith rows there's a duplicate order number there.
>
> And I need to do it in either a for/foreach loop. Somehow there's got to be
> a simple way to check and only output non duplicate order numbers while I'm
> looping thru the data. Any help would be greatly appreciated.
>
>  
>
>  
>
> Thanks
>
> php.code@...
>
>
foreach ($array as $k => $v) {
        $result[$v['order_number']] = $v;
}
//then use $result

-Shawn

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


RE: Removeing duplicates inside for/foreach loop.

by Tom Shaw-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm firly certain array unique won't can't handle dimensional arrays. This
is what I have for code it almost works

                $order_data = array();
                while ($row = $db->fetchRow($result)) {
                        $order_data[] = '<a
href="'.setup_uri('orders').'?id=' . $row['order_number'] . '">' .
$row['order_payment_first_name'] . ' ' . $row['order_payment_last_name'] .
'</a>';
                }
                $db->freeresult($result);
               
                sort($order_data);
               
                if ($count = count($order_data)) {

                        for ($i = 2; $i < $count; $i++) {
                               
  if ($order_data[$i]['order_number'] ==
$order_data[$i-1]['order_number']) {
        unset($order_data[$i-1]);
  }

                                if (count($order_data) > 0) {
                                        $order_list = implode(', ',
$order_data);
                                } else {
                                        $order_list = '';
                                }
                               
                        }
                }

-----Original Message-----
From: tedd [mailto:tedd.sperling@...]
Sent: Tuesday, July 15, 2008 12:12 PM
To: php-general@...
Subject: Re: [PHP] Removeing duplicates inside for/foreach loop.

At 11:50 AM -0500 7/15/08, Niccolo Machiavelli wrote:
>$array = array(

-snip-

>My question is I want to remove duplicate order numbers in other words
ditch
>*one* of the Mike Smith rows there's a duplicate order number there.
>
>And I need to do it in either a for/foreach loop. Somehow there's got to be
>a simple way to check and only output non duplicate order numbers while I'm
>looping thru the data. Any help would be greatly appreciated.

Niccolo:

Check out: array_unique

http://www.php.net/array_unique

Cheers,

tedd

--
-------
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Parent Message unknown Re: Removeing duplicates inside for/foreach loop.

by Eric Butera-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, Jul 15, 2008 at 12:50 PM, Niccolo Machiavelli
<php.coder@...> wrote:

> $array = array(
>
>  1 => array("order_number" => "10DA0DDEDAF97DB", "order_payment_first_name"
> =>  "Mike", "order_payment_last_name" =>  "Smith"),
>
>  2 => array("order_number" => "10DA0DDEDAF97DB", "order_payment_first_name"
> =>  "Mike", "order_payment_last_name" =>  "Smith"),
>
>  3 => array("order_number" => "45Y3453245T23T5", "order_payment_first_name"
> =>  "Steve", "order_payment_last_name" =>  "Jobs"),
>
>  4 => array("order_number" => "34RTB345T45T456", "order_payment_first_name"
> =>  "Frank", "order_payment_last_name" =>  "Capra"),
>
>  5 => array("order_number" => "567U457645645Y6", "order_payment_first_name"
> =>  "John", "order_payment_last_name" =>  "Doe"),
>
>  6 => array("order_number" => "7698234T456M963", "order_payment_first_name"
> =>  "Bill", "order_payment_last_name" =>  "Williams"));
>
>
>
> echo('<pre>');
>
> print_r($array);
>
> echo('</pre>');
>
>
>
> My question is I want to remove duplicate order numbers in other words ditch
> *one* of the Mike Smith rows there's a duplicate order number there.
>
> And I need to do it in either a for/foreach loop. Somehow there's got to be
> a simple way to check and only output non duplicate order numbers while I'm
> looping thru the data. Any help would be greatly appreciated.
>
>
>
>
>
> Thanks
>
> php.code@...
>
>

Would it be possible to leave them out in your original query?

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: Removeing duplicates inside for/foreach loop.

by Jim Lucas-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Niccolo Machiavelli wrote:

> I'm firly certain array unique won't can't handle dimensional arrays. This
> is what I have for code it almost works
>
> $order_data = array();
> while ($row = $db->fetchRow($result)) {
> $order_data[] = '<a
> href="'.setup_uri('orders').'?id=' . $row['order_number'] . '">' .
> $row['order_payment_first_name'] . ' ' . $row['order_payment_last_name'] .
> '</a>';
> }
> $db->freeresult($result);

since you are getting your information from a db, use
        SELECT DISTINCT order_number, ....

This will ensure that only unique order_numbers are returned

>
> sort($order_data);
>
> if ($count = count($order_data)) {
>
> for ($i = 2; $i < $count; $i++) {
>
>   if ($order_data[$i]['order_number'] ==
> $order_data[$i-1]['order_number']) {
>         unset($order_data[$i-1]);
>   }
>
> if (count($order_data) > 0) {
> $order_list = implode(', ',
> $order_data);
> } else {
> $order_list = '';
> }
>
> }
> }
>
> -----Original Message-----
> From: tedd [mailto:tedd.sperling@...]
> Sent: Tuesday, July 15, 2008 12:12 PM
> To: php-general@...
> Subject: Re: [PHP] Removeing duplicates inside for/foreach loop.
>
> At 11:50 AM -0500 7/15/08, Niccolo Machiavelli wrote:
>> $array = array(
>
> -snip-
>
>> My question is I want to remove duplicate order numbers in other words
> ditch
>> *one* of the Mike Smith rows there's a duplicate order number there.
>>
>> And I need to do it in either a for/foreach loop. Somehow there's got to be
>> a simple way to check and only output non duplicate order numbers while I'm
>> looping thru the data. Any help would be greatly appreciated.
>
> Niccolo:
>
> Check out: array_unique
>
> http://www.php.net/array_unique
>
> Cheers,
>
> tedd
>


--
Jim Lucas

    "Some men are born to greatness, some achieve greatness,
        and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
     by William Shakespeare


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php