From: tedd on
At 5:35 PM -0400 4/7/10, Andrew Ballard wrote:
>On Wed, Apr 7, 2010 at 5:02 PM, Paul M Foster <paulf(a)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
From: Ryan Sun on
rsort(array_combine(array2, array1));

you should expect array(
'Personal Email' => 75,
'USPS mail' => 40,
'Personal Phone' => 31,
'Web site' => 31,
'Text Message' => 31
)

logically, the items are your key but not the count of votes


On Wed, Apr 7, 2010 at 6:29 PM, tedd <tedd.sperling(a)gmail.com> wrote:
> At 5:35 PM -0400 4/7/10, Andrew Ballard wrote:
>>
>> On Wed, Apr 7, 2010 at 5:02 PM, Paul M Foster <paulf(a)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
>
>
From: Andrew Ballard on
On Wed, Apr 7, 2010 at 6:46 PM, Ryan Sun <ryansun81(a)gmail.com> wrote:
> On Wed, Apr 7, 2010 at 6:29 PM, tedd <tedd.sperling(a)gmail.com> wrote:

[snip]

>>
>> 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.
>>
[snip]
>
> rsort(array_combine(array2, array1));
>
> you should expect array(
> 'Personal Email' => 75,
> 'USPS mail' => 40,
> 'Personal Phone' => 31,
> 'Web site' => 31,
> 'Text Message' => 31
> )
>
> logically, the items are your key but not the count of votes
>

That's the ticket. The solution is pretty simple now that we
understand the nature of the problem. :-)

Andrew
From: tedd on
At 6:46 PM -0400 4/7/10, Ryan Sun wrote:
>rsort(array_combine(array2, array1));
>
>you should expect array(
> 'Personal Email' => 75,
> 'USPS mail' => 40,
> 'Personal Phone' => 31,
> 'Web site' => 31,
> 'Text Message' => 31
>)
>
>logically, the items are your key but not the count of votes
>

Logically, combine_array() will work to combine the two arrays as you
said, as shown here:

http://www.webbytedd.com/bbbb/array-combine/

I've tried rsort(), but is does not solve the problem presented,
which was to sort the items depending upon their values.

Thus far, my solution is the only one that works.

Cheers,

tedd

--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
From: tedd on
At 8:28 AM -0400 4/8/10, Andrew Ballard wrote:
>On Wed, Apr 7, 2010 at 6:46 PM, Ryan Sun <ryansun81(a)gmail.com> wrote:
>
> >
>> rsort(array_combine(array2, array1));
>>
>> you should expect array(
>> 'Personal Email' => 75,
>> 'USPS mail' => 40,
>> 'Personal Phone' => 31,
>> 'Web site' => 31,
>> 'Text Message' => 31
>> )
>>
>> logically, the items are your key but not the count of votes
>>
>
>That's the ticket. The solution is pretty simple now that we
>understand the nature of the problem. :-)
>
>Andrew

Andrew:

Half the solution is understanding the problem.

However, the above solution only solves half the problem.

Cheers,

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