1

Confused as to why I am getting this message

Warning: implode(): Invalid arguments passed

Here is my script

$out = "";
while($row = $results->fetch_array()) {

    // do stuff here to create an indexed array

    $arraycount = count($array);
    for ($i=0; $i < $arraycount; $i++) {
        foreach($array as $arr) {
            $out .= implode(";", $arr) . "<br>";
        }
    }
}

I know $array is a proper indexed array.. if I do this before or after my for/foreach loop:

echo '<pre>';
print_r($array);
echo '</pre>';

The output is all my arrays

Array
(
    [0] => Array 1 Node 1
    [1] => Array 1 Node 2
    [2] => Array 1 Node 3
    [3] => Array 1 Node 4
    [4] => Array 1 Node 5
)

Array
(
    [0] => Array 2 Node 1
    [1] => Array 2 Node 2
    [2] => Array 2 Node 3
    [3] => Array 2 Node 4
    [4] => Array 2 Node 5
)

Array
(
    [0] => Array 3 Node 1
    [1] => Array 3 Node 2
    [2] => Array 3 Node 3
    [3] => Array 3 Node 4
    [4] => Array 3 Node 5
)

etc. etc. etc.

The output I'm looking to get out of all of this, if you can't tell by the loop, is a csv-like file with a ; delimiter.

This loop works fine outside of the loop, but then I'm having the issue of it not retrieving all the results from each loop-- so doing it within the loop as I am attempting I believe is ideal.

Anyone know what I'm doing wrong/why this isn't working in the while loop?

1
  • implode(";", $arr) -- Is $arr an array? implode() expects the second argument to be an array. Commented May 23, 2017 at 14:48

1 Answer 1

1

It looks like $array is an array of strings. In that case, you shouldn't need the inner for or foreach loops. If each instance of $array is one of these:

Array
(
    [0] => Array 1 Node 1
    [1] => Array 1 Node 2
    [2] => Array 1 Node 3
    [3] => Array 1 Node 4
    [4] => Array 1 Node 5
)

Then you should be able to implode that directly.

$out = "";
while($row = $results->fetch_array()) {

    // do stuff here to create an indexed array

    $out .= implode(";", $array) . "<br>";
}

By the way, the // do stuff here to create an indexed array may not be necessary. I'm not sure exactly what it does, but you can specify in your fetch method that it should fetch a numerically indexed array.

while($row = $results->fetch_array(MYSQLI_NUM)) { ...

(Or just use the fetch_row method instead.)

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

4 Comments

You are correct :) I did the something similar after reading @axiac's comment. Just removed the foreach portion and imploding my $array. You made it simpler though as I was still leaving in my for loop with the array count-- which I suppose is not needed. Anyways, thank you!!
@user8046043 If this is being output to a file, you may be able to use fputcsv instead of building the $out string and then outputting that to your file. It does accept different delimiters, so you could use semicolon instead of comma.
Good idea. My plan was to just copy the output and paste it in a text file and change it to a .csv. But the output is so large its freezing my browser so I'm having difficulties even copying it. Thanks for the tip
Oh yeah. In that case, creating the file directly should be a much better option. You're welcome. :)

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.