Link to home
Create AccountLog in
PHP

PHP

--

Questions

--

Followers

Top Experts

Avatar of zattz
zattz

php implode / explore
I was wondering if somebody could help me with some code. I am a php newbie.

I keep getting the following error in my apache logs: "PHP Warning:  implode(): Argument must be an array". I presume it is because the array is empty or does not have enough content. Can somebody give some advice on how to get rid of the warnings. It seems to function fine.

$sentences = $content->getSentences();

$chunks = array_chunk($sentences, count($sentences) / 2);
$sections = array_chunk($chunks[0], count($chunks[0]) / 7);

$main_content = implode($chunks[1]);

$section1  = implode($sections[0]);
$section2  = implode($sections[1]);
$section3  = implode($sections[2]);
$section4  = implode($sections[3]);
$section5  = implode($sections[4]);
$section6  = implode($sections[5]);
$section7  = implode($sections[6]);

Open in new window

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of gr8gonzogr8gonzo🇺🇸

Implode is a way to join the elements of an array together with some kind of delimiter. For example:

$x = array("The","quick","brown","fox");

echo implode("#",$x); // Results in "The#quick#brown#fox"
echo implode(" ",$x); // Results in "The quick brown fox"

Avatar of gr8gonzogr8gonzo🇺🇸

And just for the sake of completeness, explode() does the opposite. It takes a string and converts it into an array by using a delimiter. Like this:

$string = "The quick brown fox";
$array = explode(" ",$string); // Results in Array("The","quick","brown","fox")

The implode/explode functions are pretty useful for any kind of delimited strings or to find the individual lines in a piece of content (explode() the whole contents by the newline \n character)

Avatar of zattzzattz

ASKER

I know the script is supposed to do. I just don't like the warning messages flooding my log files.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of gr8gonzogr8gonzo🇺🇸

Yes, but it's because you don't have a delimiter as your first parameter. I was trying to give you the proper usage. If you don't want a delimiter, then pass an empty string. For example:

$main_content = implode("",$chunks[1]);

Avatar of Ray PaseurRay Paseur🇺🇸

The difference is stylistic, not substantive but I would code it this way:

$main_content = implode(NULL,$chunks[1]);

Before your script uses a function that requires an array for input you can test the input data to see if it is an array.  (#protip: All PHP functions are documented in the PHP.net web site -- required reading if you're going to try to write programs in PHP).
http://php.net/manual/en/function.is-array.php

A really good book to help you get a jump-start in PHP...
http://www.sitepoint.com/books/phpmysql5/

Avatar of zattzzattz

ASKER

Using $main_content = implode("",$chunks[1]);  I now get

PHP Warning:  implode(): Argument must be an array

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of zattzzattz

ASKER

Sorry I meant to say the error is now "PHP Warning:  implode(): Invalid arguments passed"

ASKER CERTIFIED SOLUTION
Avatar of gr8gonzogr8gonzo🇺🇸

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of zattzzattz

ASKER

I got it to work by checking for null...

Avatar of Ray PaseurRay Paseur🇺🇸

I got it to work by checking for null...
The correct way would be to check to see if the data element is an array.  Many PHP functions return an array on success or FALSE on failure.  A test for NULL will fail both of those conditions.  Use this function instead (see comment at ID: 38192318 for the explanation).
http://php.net/manual/en/function.is-array.php

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.

PHP

PHP

--

Questions

--

Followers

Top Experts

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.