EJ Willis
EJ Willis

Reputation: 89

Keep Getting an "Array to string conversion" Error when trying to run concatenation method

So I'm working on a problem where I need to convert an array of values into a string so that it can be displayed for an excel document. But I keep on getting an "Array to string conversion" error when I try to run the export. The code is

$counties = "";
if(isset($forms->form_info['county'])){
  for($i = 0; $i < count($forms->form_info['county']); $i++){
    if(is_int($i / 10) && $i != 0){
      $counties .= $forms->form_info['county'][$i] . ",\n";
    } elseif($i == (count($forms->form_info['county']) - 1)) {
      // Keep getting the error on the line below
      $counties .= $forms->form_info['county'][$i] . " ";
    } else {
      $counties .= $forms->form_info['county'][$i] . ", ";
    }
  }
}

I've done a dd of a gettype on counties as well as the form element I'm trying to use, and both appeared as strings, so I'm lost on where the supposed array that's being converted is. I've tried making $form->form_info['county'][$i] into its own variable and concatenating it to the counties variable but received the same issue.

The result of dd($forms->form_info['county'][$i]); is

"Bernalillo County"

The result of dd($forms->form_info['county']); is

array:1 [▼
   0 => "Bernalillo County"
]

The result of a dd(var_dump($forms->form_info['county'][$i])); is

string(17) "Bernalillo County"
null

And here is the direct screenshot of the issue

Php error screen

Upvotes: 1

Views: 2267

Answers (3)

EJ Willis
EJ Willis

Reputation: 89

Credit to miken32 for his solution to get me most of the way there. The fix came from a simple if statement that is checked before the foreach. So the solution ended up being:

  $counties = "";
  if(isset($forms->form_info['county'])){
    if(count($forms->form_info['county']) < 2){
      $counties = $forms->form_info['county'][0];
    } else {
      foreach(array_chunk($forms->form_info['county'], 10) as $v) {
        $counties .= implode(", ", $v) . ",\n";
      }
      $counties = substr($counties, 0, -2);
    }
  }

But the real reason that the function was failing, was that the data I had been using was cast as an array(explode) rather than just an explode, so it was actually a multidimensional array.

Upvotes: 0

miken32
miken32

Reputation: 42694

Obviously the data is not what you expect, but this code is very verbose for what you're trying to do: separate the array values with commas, putting a line break after every tenth element. We can do that easily with use of array_chunk.

<?php
$data = str_split("abcdefghijklmnopqrstuvwxyz");
$output = "";

foreach(array_chunk($data, 10) as $v) {
    $output .= implode(", ", $v) . ",\n";
}
// get rid of that final comma space
echo substr($output, 0, -2);

Output:

a, b, c, d, e, f, g, h, i, j,
k, l, m, n, o, p, q, r, s, t,
u, v, w, x, y, z

Adapting to your code:

$counties = "";
if(isset($forms->form_info['county'])){
    foreach(array_chunk($forms->form_info['county'], 10) as $v) {
        $counties .= implode(", ", $v) . ",\n";
    }
    $counties = substr($counties, 0, -2);
}

Upvotes: 2

Oras
Oras

Reputation: 1096

If I understand this well, you're expecting an array from $forms->form_info['county'] ?

if that's the case please use implode

Your code would be:

if(isset($forms->form_info['county']) && is_array($forms->form_info['county'])){
   $countries = implode(",", $forms->form_info['county']);
}

Upvotes: -1

Related Questions