Array to string conversion error when using implode
Asked Answered
F

7

35

I'm confused about an error I am getting stating Array to string conversion

The reason I'm confused is I'm trying to do exactly that, convert an array to a string, using implode which according to the manual should allow me to convert my array into a string. So why am I getting an error?

var $matches is an array. $error_c is the var I want to store the string.

print_r($matches); // prints the array correctly
$error_c = implode(',', $matches);
echo $error_c;

Outputs simply array and gives:

Notice: Array to string conversion in ...

The manual states that implode — Join array elements with a string so why do I get an error when I try to do it?

Edit: this is the output I get from $matches

Array ( [0] => Array ( [0] => C [1] => E [2] => R [3] => R [4] => O [5] => R [6] => C [7] => O [8] => N [9] => T [10] => A [11] => C [12] => T [13] => S [14] => U [15] => P [16] => P [17] => R [18] => E [19] => S [20] => S [21] => E [22] => D ) ) 
Fallible answered 19/9, 2014 at 8:39 Comment(4)
the element of your array should be string or have __toString capabilityHindustan
You got it! Check the answers ;)Panacea
$matches is an array . then how can u implode the whole array $matches without exploding it, if not then u have to mention with index array like $matches[0]Tonsil
This is a specific error encountered by folks sometimes when dealing with PHP - it's not broad at all. The error is literally "array to string conversion" on the specific line of code with "implode". I disagree with the closing of the question on this basis. In fact, I landed on this question precisely because its wording exactly matched the error I was looking for, and the answer helped me solved it. It's not too broad.Sensational
P
45

You have an array of arrays... Try this:

$error_c = implode(',', $matches[0]);
Panacea answered 19/9, 2014 at 8:43 Comment(0)
S
14
$error_c = implode(',', $matches[0]);
echo $error_c;

because your array contains arrays inside

Spock answered 19/9, 2014 at 8:43 Comment(0)
P
6

The issue is due to the fact that you are call implode on an Array which is two dimensionnal.

Array ( 
    [0] => Array ( 
        [0] => C 
        [1] => E 
        [2] => R 
        [3] => R 
        [4] => O 
        [5] => R 
        [6] => C 
        [7] => O 
        [8] => N 
        [9] => T 
        [10] => A 
        // ...
    ) 
) 

which is equivalent to

[["C", "E", "R", "R", "O", "R", "C", "O", "N", "..." ]]

What you should do before performing an implode is to flatten the array and after that you can call implode with the flattened array or by calling implode only with the first item in the main array which is an array.

Here is a question which provide guidance to flatten an array How to Flatten a Multidimensional Array?

With a function which allows to flatten an array you can perform the call like this

implode(flatten($array_of_data), $matches);

No need to access the first item only, whatever number of items are in $matches array the flatten function will return a one dimensionnal Array on which you can call implode

Protein answered 4/12, 2020 at 12:56 Comment(0)
B
2

Do that:

print_r($matches); // prints the array correctly
$error_c = implode(',', $matches[0]);
echo $error_c;
Beau answered 19/9, 2014 at 8:43 Comment(0)
J
2

You may use array_values() for array of arrays

e.g. implode (",", array_values($array))

Jetport answered 22/3, 2019 at 15:36 Comment(0)
C
0
#best way! fast and easy 

$string = 'whatever';
$array = (['new'],['old'],['whatever']);

    for($i = 0;$i < count($array);$i++)
    if(str_contains(json_encode($host),implode(',',$array[$i]))){
    $found = true;
    }if($found == true){echo "found";}else{echo "not found";}

# returns found
Chindwin answered 13/9, 2022 at 7:36 Comment(0)
A
-1

To just put whatever data is in the array into a string, try this

function whatever_to_string($in){
    ob_start();
    print_r($in);
    return ob_get_clean();
    }

The 'ob_*' functions control the output buffer.

http://php.net/manual/en/function.ob-start.php

http://php.net/manual/en/function.ob-get-clean.php

Ampere answered 11/4, 2018 at 22:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.