Forum Moderators: coopster

Message Too Old, No Replies

Counting Array Unique Removals and Corresponding Fields

         

ocon

8:25 pm on Feb 5, 2014 (gmt 0)

10+ Year Member Top Contributors Of The Month



I have a two-dimensional array I'd like to restructure. The "values" field are different 'votes' for how a listing should be named, "distance" is how far away the voter is from that location, "time" is when the vote took place, and "count" is based off the counts for corresponding unique "values".

I'd like to make "values" unique, only keep the "distance", "time" and "count" values that correspond to the first time a unique value was created, and reorganize the index.

Turn something like this:


$array = Array
(
[values] => Array
(
[0] => San Diego Zoo
[1] => San Diego Zoo
[2] => SD Zoo
)

[distance] => Array
(
[0] => 5
[1] => 350
[2] => 21
)

[time] => Array
(
[0] => 2014-02-03 08:57:29
[1] => 2014-02-03 08:57:54
[2] => 2014-02-03 08:58:12
)

[count] => Array
(
[0] => 2
[1] => 2
[2] => 1
)
)


Into this:


$array = Array
(
[values] => Array
(
[0] => San Diego Zoo
[1] => SD Zoo
)

[distance] => Array
(
[0] => 5
[1] => 21
)

[time] => Array
(
[0] => 2014-02-03 08:57:29
[1] => 2014-02-03 08:58:12
)

[count] => Array
(
[0] => 2
[1] => 1
)
)


I'm not sure how best to do this. I'm thinking to use array_unique on "values" ($array["values"]=array_unique($array["values"]);) but I wouldn't know how to remove the appropriate "distance", "time", and "count" values.

coopster

8:46 pm on Mar 4, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Did you ever figure this out, ocon?

Perhaps something like this would work:
  1. Initialize a new array
  2. Loop over primary array, see if value exists in new array.
  3. No? Add it to the new array.
  4. Yes? Take the key, unset the corresponding key in the other arrays.

Make sense?