Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I would like to transform this array in String with implode :

Array ( [0] => Array ( [technologies_id] => 1 [nom] => PHP [type_technologies_id] => 1 [nom_techno] => Developpement web ) [1] => Array ( [technologies_id] => 3 [nom] => ASP3 [type_technologies_id] => 1 [nom_techno] => Developpement web ) [2] => Array ( [technologies_id] => 5 [nom] => JavaScript [type_technologies_id] => 1 [nom_techno] => Developpement web ) [3] => Array ( [technologies_id] => 6 [nom] => CSS [type_technologies_id] => 1 [nom_techno] => Developpement web ) [4] => Array ( [technologies_id] => 7 [nom] => AJAX [type_technologies_id] => 1 [nom_techno] => Developpement web ) [5] => Array ( [technologies_id] => 17 [nom] => HTML [type_technologies_id] => 1 [nom_techno] => Developpement web ) ) Array ( [0] => Array ( [technologies_id] => 8 [nom] => Jquery [type_technologies_id] => 2 [nom_techno] => Frameworks web ) ) Array ( [0] => Array ( [technologies_id] => 22 [nom] => VB 6 [type_technologies_id] => 3 [nom_techno] => Developpement applicatif ) [1] => Array ( [technologies_id] => 23 [nom] => VB.NET [type_technologies_id] => 3 [nom_techno] => Developpement applicatif ) ) Array ( ) Array ( ) Array ( [0] => Array ( [technologies_id] => 28 [nom] => MySQL [type_technologies_id] => 6 [nom_techno] => SGDB(R) ) [1] => Array ( [technologies_id] => 30 [nom] => SQL Server Express [type_technologies_id] => 6 [nom_techno] => SGDB(R) ) [2] => Array ( [technologies_id] => 32 [nom] => SQLite [type_technologies_id] => 6 [nom_techno] => SGDB(R) ) [3] => Array ( [technologies_id] => 34 [nom] => Microsoft Access [type_technologies_id] => 6 [nom_techno] => SGDB(R) ) ) Array ( [0] => Array ( [technologies_id] => 39 [nom] => SQL [type_technologies_id] => 7 [nom_techno] => Langages d'interrogation de bases de donnees ) ) Array ( [0] => Array ( [technologies_id] => 46 [nom] => Windows [type_technologies_id] => 9 [nom_techno] => OS ) [1] => Array ( [technologies_id] => 49 [nom] => MAC OS [type_technologies_id] => 9 [nom_techno] => OS ) ) Array ( [0] => Array ( [technologies_id] => 54 [nom] => Merise [type_technologies_id] => 11 [nom_techno] => Methodologies ) ) 

I tried like this :

$string_comp = implode($competences);

but I obtained this :

ArrayArrayArrayArrayArrayArray

Thanks in advance for your help

share|improve this question
    
You have a multidimensional array, an array of arrays. Implode only works on 1 level of an array. –  Ben Fortune Nov 1 '13 at 10:26

6 Answers 6

up vote 4 down vote accepted

Try this custom function :)

function multi_implode($sep, $array)
{
    $_array = array();

    foreach($array as $val)
    {
        if(is_array($val)) $_array[] = multi_implode($sep, $val);
        else $_array[] = $val;
    }

    return implode($sep, $_array);
}

Your implode not working because you are have a multi-array.

share|improve this answer
    
When I call this function, what separator have I to send ? thank you –  beegees Nov 1 '13 at 10:43
    
@beegees, what ever you want :) Put ' ' (space) for example or ',' (comma). $sep is this separator of the arrays. –  KryDos Nov 1 '13 at 10:45
    
Thank you KryDos, you're a Gentleman. Your code works great. bee –  beegees Nov 1 '13 at 10:52
    
recursive array you made. Really good answer. –  Alexander Nov 1 '13 at 11:12

Try like this->

$array = array(1,2,3,4);
echo serialize($array);

// Prints

a:4:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;s:3:4;}
share|improve this answer

Just use this:

$res = '';
foreach ($arr as $item) {
    $res .= implode('', $item);
}
share|improve this answer
    
Or in an one line: $str = implode(array_map('implode', $array)) –  Akuma Nov 1 '13 at 10:31
    
I think you code doesn't work. Thanks anyway –  beegees Nov 1 '13 at 10:50
    
I test this code before write this –  Akuma Nov 1 '13 at 10:52
    
Strangely, it doesn't work for me. Thanks again. –  beegees Nov 1 '13 at 10:53

To get output in a string formate use serialize() function of php you can unserialize() also as its previous state.

share|improve this answer

Depends on how you want to access it but personally, I'd just convert it to JSON with json_encode($myvar);. Or have a look at this answer provided on Stackoverflow: Multidimensional Array PHP Implode.

share|improve this answer

You have an array of arrays. You must implode each subarray, like this:

foreach ($competences as $subArray)
{
    $string_comp .= implode($subArray);
}
share|improve this answer
    
Thank you. I have a parse error with your code ? –  beegees Nov 1 '13 at 10:43
    
@beegees I'm sorry, I forgot foreach syntax. I've made proper corrections. –  namikiri Nov 1 '13 at 10:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.