Sort two coupled arrays

Hi gang:

Here's the problem -- I want to sort and combine two arrays into one
sorted array. Here's a real-world example:

Array 1
(
[1] => 75
[2] => 31
[3] => 31
[4] => 31
[5] => 40
)

Array 2
(
[1] => Personal Email
[2] => Personal Phone
[3] => Web site
[4] => Text Message
[5] => USPS mail
)

After the operation, I want this:

Array
(
[75] => Personal Email
[40] => USPS mail
[31] => Personal Phone
[31] => Web site
[31] => Text Message
)

Note: This is a descending-sort of Array 1 while being coupled to
index of Array 2. In other words, the order of Array 2 depends upon
the order of Array 1 -- the two arrays are coupled.

I've solved this problem, but my solution is pretty lame. There has
to be a better/slicker way.

Suggestions?

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
TedD [ Mi, 07 April 2010 22:09 ] [ ID #2038044 ]

Re: Sort two coupled arrays

Am 07.04.2010 22:09, schrieb tedd:
> Hi gang:
>
> Here's the problem -- I want to sort and combine two arrays into one
> sorted array. Here's a real-world example:
>
> Array 1
> (
> [1] => 75
> [2] => 31
> [3] => 31
> [4] => 31
> [5] => 40
> )
>
> Array 2
> (
> [1] => Personal Email
> [2] => Personal Phone
> [3] => Web site
> [4] => Text Message
> [5] => USPS mail
> )
>
> After the operation, I want this:
>
> Array
> (
> [75] => Personal Email
> [40] => USPS mail
> [31] => Personal Phone
> [31] => Web site
> [31] => Text Message
> )
>
> Note: This is a descending-sort of Array 1 while being coupled to
> index of Array 2. In other words, the order of Array 2 depends upon
> the order of Array 1 -- the two arrays are coupled.
>
> I've solved this problem, but my solution is pretty lame. There has to
> be a better/slicker way.
>
> Suggestions?
>
> Cheers,
>
> tedd


array_combine($key_array, $value_array)

:)

-- Piero

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Piero Steinger [ Mi, 07 April 2010 22:26 ] [ ID #2038045 ]

Re: Sort two coupled arrays

Piero Steinger wrote:
> Am 07.04.2010 22:09, schrieb tedd:
>
>> Hi gang:
>>
>> Here's the problem -- I want to sort and combine two arrays into one
>> sorted array. Here's a real-world example:
>>
>> Array 1
>> (
>> [1] => 75
>> [2] => 31
>> [3] => 31
>> [4] => 31
>> [5] => 40
>> )
>>
>> Array 2
>> (
>> [1] => Personal Email
>> [2] => Personal Phone
>> [3] => Web site
>> [4] => Text Message
>> [5] => USPS mail
>> )
>>
>> After the operation, I want this:
>>
>> Array
>> (
>> [75] => Personal Email
>> [40] => USPS mail
>> [31] => Personal Phone
>> [31] => Web site
>> [31] => Text Message
>> )
>>
>> Note: This is a descending-sort of Array 1 while being coupled to
>> index of Array 2. In other words, the order of Array 2 depends upon
>> the order of Array 1 -- the two arrays are coupled.
>>
>> I've solved this problem, but my solution is pretty lame. There has to
>> be a better/slicker way.
>>
>> Suggestions?
>>
>> Cheers,
>>
>> tedd
>>
>
>
> array_combine($key_array, $value_array)
>
> :)
>
> -- Piero
>

And then:

krsort($combined_array);


Cheers,

Mattias

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Mattias Thorslund [ Mi, 07 April 2010 22:36 ] [ ID #2038046 ]

Re: Sort two coupled arrays

On Wed, Apr 07, 2010 at 04:09:47PM -0400, tedd wrote:

> Hi gang:
>
> Here's the problem -- I want to sort and combine two arrays into one
> sorted array. Here's a real-world example:
>
> Array 1
> (
> [1] => 75
> [2] => 31
> [3] => 31
> [4] => 31
> [5] => 40
> )
>
> Array 2
> (
> [1] => Personal Email
> [2] => Personal Phone
> [3] => Web site
> [4] => Text Message
> [5] => USPS mail
> )
>
> After the operation, I want this:
>
> Array
> (
> [75] => Personal Email
> [40] => USPS mail
> [31] => Personal Phone
> [31] => Web site
> [31] => Text Message
> )
>
> Note: This is a descending-sort of Array 1 while being coupled to
> index of Array 2. In other words, the order of Array 2 depends upon
> the order of Array 1 -- the two arrays are coupled.
>
> I've solved this problem, but my solution is pretty lame. There has
> to be a better/slicker way.

Just so I understand the way arrays work in PHP (gee, I *thought* I
did!), as you add the final three elements in the final array, won't
they overwrite each other? I was under the impression that a
*numerically* indexed array has a constraint that the numeric indexes be
unique, if not contiguous. Am I wrong? If so, please provide a
reference. Or are those "numbers" really strings?

Paul

--
Paul M. Foster

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Paul M Foster [ Mi, 07 April 2010 23:02 ] [ ID #2038047 ]

Re: Sort two coupled arrays

On Wed, Apr 7, 2010 at 5:02 PM, Paul M Foster <paulf [at] quillandmouse.com> wro=
te:
> On Wed, Apr 07, 2010 at 04:09:47PM -0400, tedd wrote:
>
>> Hi gang:
>>
>> Here's the problem -- I want to sort and combine two arrays into one
>> sorted array. Here's a real-world example:
>>
>> Array 1
>> (
>> =C2=A0 =C2=A0 [1] =3D> 75
>> =C2=A0 =C2=A0 [2] =3D> 31
>> =C2=A0 =C2=A0 [3] =3D> 31
>> =C2=A0 =C2=A0 [4] =3D> 31
>> =C2=A0 =C2=A0 [5] =3D> 40
>> )
>>
>> Array 2
>> (
>> =C2=A0 =C2=A0 [1] =3D> Personal Email
>> =C2=A0 =C2=A0 [2] =3D> Personal Phone
>> =C2=A0 =C2=A0 [3] =3D> Web site
>> =C2=A0 =C2=A0 [4] =3D> Text Message
>> =C2=A0 =C2=A0 [5] =3D> USPS mail
>> )
>>
>> After the operation, I want this:
>>
>> Array
>> (
>> =C2=A0 =C2=A0 [75] =3D> Personal Email
>> =C2=A0 =C2=A0 [40] =3D> USPS mail
>> =C2=A0 =C2=A0 [31] =3D> Personal Phone
>> =C2=A0 =C2=A0 [31] =3D> Web site
>> =C2=A0 =C2=A0 [31] =3D> Text Message
>> )
[snip]
> Just so I understand the way arrays work in PHP (gee, I *thought* I
> did!), as you add the final three elements in the final array, won't
> they overwrite each other? I was under the impression that a
> *numerically* indexed array has a constraint that the numeric indexes be
> unique, if not contiguous. Am I wrong? If so, please provide a
> reference. Or are those "numbers" really strings?
>
> Paul
>
> --
> Paul M. Foster

Array indexes have to be unique regardless of whether they are numeric
or strings.

<?php

$a =3D array
(
1 =3D> '75',
2 =3D> '31',
3 =3D> '31',
4 =3D> '31',
5 =3D> '40',
);

$b =3D array
(
1 =3D> 'Personal Email',
2 =3D> 'Personal Phone',
3 =3D> 'Web site',
4 =3D> 'Text Message',
5 =3D> 'USPS mail',
);

$x =3D array_combine($a, $b);
var_export($x);
/*
array (
75 =3D> 'Personal Email',
31 =3D> 'Text Message',
40 =3D> 'USPS mail',
)
*/

echo "\n";

krsort($x);

var_export($x);
/*
array (
75 =3D> 'Personal Email',
40 =3D> 'USPS mail',
31 =3D> 'Text Message',
)
*/

?>


Andrew

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Andrew Ballard [ Mi, 07 April 2010 23:35 ] [ ID #2038048 ]

Re: Sort two coupled arrays

Try this insted

array(
[0]=3D>array( [0]=3D>75, [1] =3D> Personal Email)
[1]=3D>array( [0]=3D>31, [1] =3D> Personal Phone)
[2]=3D>array( [0]=3D>31, [1] =3D> Web site)
[3]=3D>array( [0]=3D>31, [1] =3D> Text Message)
[4]=3D>array( [0]=3D>40, [1] =3D> USPS mail)

MvH / Hans =C3=85hlin
Tel: +46761488019
http://www.kronan-net.com/
irc://irc.freenode.net:6667 - TheCoin



2010/4/7 tedd <tedd [at] sperling.com>:
> Hi gang:
>
> Here's the problem -- I want to sort and combine two arrays into one sort=
ed
> array. Here's a real-world example:
>
> Array 1
> (
> =C2=A0 =C2=A0[1] =3D> 75
> =C2=A0 =C2=A0[2] =3D> 31
> =C2=A0 =C2=A0[3] =3D> 31
> =C2=A0 =C2=A0[4] =3D> 31
> =C2=A0 =C2=A0[5] =3D> 40
> )
>
> Array 2
> (
> =C2=A0 =C2=A0[1] =3D> Personal Email
> =C2=A0 =C2=A0[2] =3D> Personal Phone
> =C2=A0 =C2=A0[3] =3D> Web site
> =C2=A0 =C2=A0[4] =3D> Text Message
> =C2=A0 =C2=A0[5] =3D> USPS mail
> )
>
> After the operation, I want this:
>
> Array
> (
> =C2=A0 =C2=A0[75] =3D> Personal Email
> =C2=A0 =C2=A0[40] =3D> USPS mail
> =C2=A0 =C2=A0[31] =3D> Personal Phone
> =C2=A0 =C2=A0[31] =3D> Web site
> =C2=A0 =C2=A0[31] =3D> Text Message
> )
>
> Note: This is a descending-sort of Array 1 while being coupled to index o=
f
> Array 2. In other words, the order of Array 2 depends upon the order of
> Array 1 -- the two arrays are coupled.
>
> I've solved this problem, but my solution is pretty lame. There has to be=
a
> better/slicker way.
>
> Suggestions?
>
> Cheers,
>
> tedd
> --
> -------
> http://sperling.com =C2=A0http://ancientstones.com =C2=A0http://earthston=
es.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
ahlin.hans [ Do, 08 April 2010 00:17 ] [ ID #2038161 ]

Re: Sort two coupled arrays {my solution]

At 5:35 PM -0400 4/7/10, Andrew Ballard wrote:
>On Wed, Apr 7, 2010 at 5:02 PM, Paul M Foster <paulf [at] quillandmouse.com> wrote:
>Array indexes have to be unique regardless of whether they are numeric
>or strings.

Ahhh, so you start to see the problem, eh?

Let's look at the problem again (a vote collection problem):

Array 1
(
[1] => 75
[2] => 31
[3] => 31
[4] => 31
[5] => 40
)

Array 1 is an array that contains the count of votes ($votes[] ) for
the index. IOW, index 1 received 75 votes.

Array 2
(
[1] => Personal Email
[2] => Personal Phone
[3] => Web site
[4] => Text Message
[5] => USPS mail
)

Array 2 is an array that contains the names for the items ($items[] )
voted upon. As such, index 1 (Personal Email) received 75 votes.

Now, I have this data in two different arrays and I wanted to combine
the data into one array and then preform a descend sort.

This is the way I solved it:

$final = array();

for($i =1; $i <=5; $i++)
{
$final[$i][] = $votes[$i];
$final[$i][] = $items[$i];
}

echo("<pre>");
echo('<br>');
print_r($final);
echo('<br>');

array_multisort($final, SORT_DESC);

echo('<br>');
print_r($final);
echo('<br>');
echo("</pre>");

I was hoping that someone might present something clever.

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
TedD [ Do, 08 April 2010 00:29 ] [ ID #2038162 ]
PHP » gmane.comp.php.general » Sort two coupled arrays

Vorheriges Thema: Re: Sort two coupled arrays {my solution]
Nächstes Thema: Greate day for you,