2

I have an indefinite of texts which needs to be translated into different languages,
e.g. $text1 needs to be translated in French and Hungarian.

$text1 = array ('fr', 'hr');
$text2 = array ('bg', 'el', 'hr');
$text3 = array ('bg', 'el', 'en', 'es');
$text4 = array ('bg', 'el', 'en', 'es');
$text5 = array ('bg', 'el', 'en', 'es', 'fr', 'hr');

Now I am looking for a way to combine texts and languages in order to get a minimum of language combinations, to get something like this:

$order1 = array('languages' => array('bg', 'el'), 'texts' => array ('text2', 'text3', 'text4', 'text5'));
$order2 = array('languages' => array('en', 'es'), 'texts' => array ('text3', 'text4', 'text5'));
$order3 = array('languages' => array('hr'),       'texts' => array ('text1', 'text2', 'text5'));
$order4 = array('languages' => array('fr'),       'texts' => array ('text1', 'text5'));

I have absolutly no idea how to start. Could anybody please give me a hint? Thank you very much.

4
  • You asked a really hard question! Trying to do that... Commented Jul 19, 2018 at 12:46
  • Can we add the var name to the end of each array for making it easier to code? For example : $text1 = array ('fr', 'hr','text1'); Commented Jul 19, 2018 at 12:48
  • Yes, that is no problem. Thanks for your help! :-) Commented Jul 19, 2018 at 12:56
  • I'm not sure if this question so hard or am I missing some important easy notes in PHP! I'll try to solve this even for days! But It will be awesome that someone comes and give a short good answer! Commented Jul 19, 2018 at 14:01

2 Answers 2

2

This was a pretty tricky problem, indeed.

Here's my solution:

// first collect all texts per individual language
for( $i = 1, $data = []; ( $key = 'text' . $i++ ) && isset( ${$key} ); ) {
  $data = array_merge_recursive( $data, array_fill_keys( ${$key}, $key ) );
}

// then walk through them and find other languages that match the same texts
for( $i = 1, $orders = []; count( $data ) > 0; $i++ ) {
  $texts = reset( $data );
  $order = [ 'languages' => [], 'texts' => $texts ];
  while( false !== ( $lang = array_search( $texts, $data, true ) ) ) {
    $order[ 'languages' ][] = $lang;
    unset( $data[ $lang ] );
  }
  $orders[ 'order' . $i ] = $order;
}

view sample on eval.in


Notes

  1. The orders are not in the order of your example. Let me know if that's a prerequisite.
  2. If you insist on having individual order variables ($order1, $order2, etc.), in stead of an orders array, you could do extract( $orders ); to extract them into the current scope, but I would advice against that, for you could accidentally overwrite pre-existing variables, if you are not careful.
Sign up to request clarification or add additional context in comments.

2 Comments

Interesting answer!
Thank you very much for your help!
1

-->Link for 3v4l.org full code<--

You have to do a while to check all the array positions

while($aux< count($order1)

And you also need a $containPrev1 that will turn false if the $text don't have all the 'languages' in $order1.

You use the function "in_array" to check if you element of the array is in the $text. You will check all the elements cause you are in a while cicle.

in_array($order1[$aux], $text1)

If its not in the array you set the $containPrev1 to false to point the that atleast one of elements of $order1 are not in $text1 and soo you will ignor that $text1 for the rest of the while.

Im adding elements into a string and then explode() the string to create a array, you can directaly creat an array soo you should check it and adapt the code to that (I assumed you added the 'text1' as the last element of the array).

$finalstr = $finalstr . $text1[$lastpos].' ';
$arrayfinal = explode(" ", $finalstr);

The code is too complex and you can simplify the code repetions with functions, use it as a base and improve it.

Sorry any bad english and the lack of proper text format, I'm still new to stackoverflow

1 Comment

Thank you very much for your effort!

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.