0

I have an array of arrays as following:

$a = array(
  1 => array("sport", "geo"),
  2 => array("sport", "geo", "history"),
  3 => array("geo", "history"),
  4 => array("golf", "sport"),
  ...
);

From that I need to get keys, in such a way so that values are unique. So from that I would need to get something like:

$b = array( 1, 3, 4 );

$a[2] would be cut out, since it has the same values as $a[1], but since $a[2] is not there, $a[3] is fine.

If some values get completely cut out, that's fine. I will have 30+ keys, from which I need to get 10, which have unique values.

Key is a question ID, and values are tags.

I want to get 10 questions, which are different from each other (so that I don't get 10 questions about Sport).

I tried array_unique(), but that just returns this:

Array ( 
  [1] => Array ( 
    [0] => sport 
    [1] => geo 
  )
) 

which doesn't seem to help much.

Can you guys point me towards something that could help me?

I guess I could try to list all possible categories, make that array unique, sort it by random. I would need to preserve Keys, but Keys are unique...

4
  • every key is always unique in given array, so you need to think what From that I need to get keys, in such a way so that values are unique really means you, esp. you do not set any explicit keys in subarrays, which means that default numeric keys are completely unrelated and i.e. value of $a[1] is not necessary the same as i.e. $e[1] which makes any elimination quite pointless Commented Feb 6, 2018 at 16:01
  • shouldn't you cut out array[1] and array[3] and array[4] as all these value sets appear in array[2]? You need to clarify your order of priority of which values get kept and which get deleted? Commented Feb 6, 2018 at 16:01
  • You remove 2 because it contains sport and geo. Why don't you remove 3 because it contains geo? And 4 because it contains sport? Commented Feb 6, 2018 at 16:02
  • Btw, dont use anything like "I don't even know what to ask" at the beggining of your question anymore, neither on the internet nor live. Just ask. And better edit your question now, because after all it was pretty understandable :) Commented Feb 6, 2018 at 16:50

2 Answers 2

2

you can use array_diff() to detect unique tags

Returns an array containing all the entries from array1 that are not present in any of the other arrays.

Then use array_merge() to store unique value to our $tags variable

Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.

<?php

$a = array(
 1 => array("sport", "geo"),
 2 => array("sport", "geo", "history"),
 3 => array("geo", "history"),
 4 => array("golf", "sport")
);

 $tags = [];

foreach($a as $tag){
    $tags = array_merge($tags, array_diff( $tag, $tags));
 }

 print_r($tags);

Output :

Array
(
  [0] => sport
  [1] => geo
  [2] => history
  [3] => golf
)
Sign up to request clarification or add additional context in comments.

Comments

1

Just iterate through the initial array of questions, every time save the value (array of tags) to another temporary array with checking if actual tags already exists in temporary array - if not add the question to temporary array, if exists go next. Do it until you have 10 questions in your temporary array, if you finish the question array without already having 10 questions - repeat the iteration but this time add other questions even if the tags are repeating - until you have 10.

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.