0

Find below the var_dump:

Array ( [0] => Merk op dat als u verschillende verzekeringsovereenkomsten gaat vergel [1] => ijken, u niet enkel de geraamde kosten en lasten van de overeenkomsten [2] => met elkaar mag vergelijken, maar ook andere elementen in aanmerking m [3] => oet nemen, zoals de reikwijdte van de waarborgen, het bedrag van event [4] => uele franchises of de uitsluitingsclausules. De hierboven opgegeven ) Array ( [0] => ramingen geven een beter zicht op het premiegedeelte dat wordt aangewe [1] => nd voor de dekking van het risico dat door de verzekeringsovereenkomst [2] => wordt gedekt. Het saldo van de premie, na aftrek van de taksen en bij [3] => dragen alsook van de acquisitie- en administratiekosten, bestaat immer [4] => s uit het gedeelte van de premie dat wordt aangewend om de ) Array ( [0] => contractueel vastgelegde prestaties te verrichten en uit de andere kos [1] => ten dan hierboven vermeld (waaronder de samengevoegde en onderling ged [2] => eelde kosten van de schadegevallen en het beheer ervan). Deze ramingen [3] => zijn berekend op grond van de boekhoudkundige gegevens van het laatst [4] => e boekjaar van de verzekeringsonderneming, als goedgekeurd door haar ) algemene vergadering.Array ( [0] => La prime a t adapte en fonction de notre dernier tarif. Vous avez l [1] => e droit de rsilier votre contrat dans les 3 mois qui suivent cette no [2] => tification. La rsiliation se fait par lettre recommande. ) Array ( [0] => Votre attention est attire sur le fait qu'une comparaison entre plusi [1] => eurs contrats d'assurance ne doit pas se limiter comparer l'estimati [2] => on des cots et frais de chaque contrat mais doit galement prendre en [3] => considration d'autres lments, tels que l'tendue des garanties, le [4] => montant des franchises ventuelles ou les clauses d'exclusion. Les ) Array ( [0] => estimations communiques ci-dessus permettent de mieux apprcier la pa [1] => rtie de la prime qui sert couvrir le risque assur par le contrat d' [2] => assurance. Le solde de la prime, aprs dduction des taxes et contribu [3] => tions ainsi que des frais d'acquisition et d'administration, reprsent [4] => e en effet la part de la prime affecte l'excution des prestations ) Array ( [0] => contractuelles ainsi que les frais non mentionns ci-dessus (y inclus [1] => le cot mutualis des sinistres et de leur gestion). Ces estimations s [2] => ont calcules sur la base des donnes comptables du dernier exercice c [3] => omptable de l'entreprise d'assurances telles qu'approuves par son ass [4] => emble gnrale. )

As you can see this example combines single and multidimensional arrays, and I need to put them in a single string separated by blank spaces.

2
  • 2
    Your array is not valid its missing the , also its much easier testing wise if you output with var_export instead of print_r and you have no quotes in you array either. Commented Aug 12, 2018 at 2:02
  • Your strings are also missing the quotes ('string' or "string" - beware of the difference between quotes and double quotes in PHP) Commented Aug 12, 2018 at 4:05

1 Answer 1

3

One word, Recursion

function mutliImplode($glue, $pieces = ''){
    if(!is_array($pieces) && is_array($glue)){
    //compatibility with native implode where glue and piecess can be swapped
         list($glue, $pieces) = array($pieces, $glue); //swap their values
    }

    $flat = [];
    foreach($pieces as $item){
        //if it's a sub-array, call this function again with the sub-array (recursion)
        if(is_array($item)) $item = mutliImplode($glue, $item);

        $flat[] = $item;      
    }

    //by building a flat array, we can avoid having to remove extra "glue"
    //if we just concatenated $item = $item.$glue. At the end we would 
    //have an extra $glue that we would have to remove...
    return implode($glue, $flat);
}

$arr = [ 'one', ['two', 'three']];
echo mutliImplode(' ', $arr);

Output

one two three

Sandbox

Also if there is no "glue" you can call it just like native implode (which you can call by substituting "glue" with "pieces", and leaving the second argument out)

mutliImplode($arr);

Output

onetwothree

Or you can even swap the arguments, just like native implode. Although its recommend to do $glue then $pieces. I just thought like throwing that in as an extra challenge for myself.

   echo mutliImplode($arr, ' ');

Output

 one two three

Reference http://php.net/manual/en/function.implode.php

implode — Join array elements with a string

Note: implode() can, for historical reasons, accept its parameters in either order. For consistency with explode(), however, it may be less confusing to use the documented order of arguments.

This of course assumes you input a valid array, it also works on non-nested array, albeit a bit slower then the native implementation.

Cheers!

Sign up to request clarification or add additional context in comments.

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.