0

I have such array, where each key value contains meta information of picture divided by comma.
For the meta information A,B,C, A represents the ID of the picture, B the order of picture and C the color.

Array ( [0] => 657,1,1
        [1] => 658,2,1
        [2] => 659,3,1
        [3] => 660,4,1
        [4] => 661,5,1
        [5] => 662,6,1
        [6] => 663,7,1
        [7] => 875,15,3
        [8] => 877,17,3
        [9] => 874,14,3
        [10] => 869,9,2
        [11] => 872,12,2
        [12] => 959,13,2 )

I want to get a list of unique colors from all of array value. In such example it would be

echo  element[0]; result = 1
echo  element[1]; result = 2
echo  element[2]; result = 3  <- edited

and so on, if there is more than 3 colors in array.

Can you please help me on that? These array things are really hard for me.

3
  • What language is this? Looks kind of like PHP, but there's no $ before the variables. Commented Jul 5, 2013 at 13:27
  • Can you clarify the question? Why is the third result element[3], not element[2]? Commented Jul 5, 2013 at 13:32
  • Barmar, did it. Yes it is PHP Commented Jul 5, 2013 at 14:18

3 Answers 3

1

I think this may be what you want:

$element = array_values(array_unique(array_map(function($x) {
  $meta = explode(',', $x);
  return $meta[2];
}, $input)));
sort($element);
Sign up to request clarification or add additional context in comments.

7 Comments

this is great but he result has strange keys Array ( [0] => 1 [7] => 3 [10] => 2 )
Added a call to array_values() to renumber them from 0.
This is great! Is it possible to order them by values, like that Array ( [0] => 1 [1] => 2 [2] => 3 ) ?
Use asort(). Do you have a PHP book? There's a whole chapter on arrays that should explain everything you can do with them.
Thanks Baramr, i have a lot of them, but I have no time to read :) I think better learing is by doing something. Anyway, thank you!
|
0

If I understand what you want correctly ...

$colors = array();
$list = Array ( 0 => '657,1,1', 1 => '658,2,1', 2 => '659,3,1', 3 => '660,4,1', 4 => '661,5,1', 5 => '662,6,1', 6 => '663,7,1', 7 => '875,15,3', 8 => '877,17,3', 9 => '874,14,3', 10 => '869,9,2', 11 => '872,12,2', 12 => '959,13,2' );
foreach ($list as $k=>$v){
    list( $id, $order, $color ) = explode(',',$v);
    $colors[] = $color;
}
$colors = array_unique($colors);
sort($colors);

... will create an array $colors that has ...

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

... where 1,2,3 are the colors found in the $list

2 Comments

Noekio, I need more the unique from the colors, not the count of them.
Cool. Be sure to hit the 'answered' checkmark on this or any other of your questions whose answers worked for you. This helps others see what worked for you, and will improve the odds of your questions being answered in the future.
0

Are you looking to do something like this?

$array = array ( '657,1,1', '658,2,1', '659,3,1' );
function getColor($a) {
    $a = explode(',', $a);
    return $a[2];
}
echo getColor($array[0]);

Comments

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.