Jump to content

Invalid argument supplied for foreach() warning


Recommended Posts

This part of my code is causing a  Invalid argument supplied for foreach() warning. I understand that the array is empty but not sure how to format this. This is used for importing a excell database. If there is no files for that particular item it will enter empty data to that table. Which is what I don't want. But making these changes causes the error.

if($data[11] == "" || empty($data[11]))
			$documents = "";
		else
			$documents = explode(",", $data[11]);

            
           
		foreach($documents as $addFile)
		{
			if(strpos($addFile, ".doc") !== false)
				$addFileType = "DOC";
			else
				$addFileType = "PDF";

And the problem is???

 

Sometimes there may be spaces before file names due to user error. If someone edits the exell file. Then when its imported I want to strip the blank space before entering it in the database.

 

 

$documents = explode(",", $data[11]);

... you said it yourself, using trim.

 

Not sure what you are trying to get at, without you pasting more code so we can see where the filename is being used / where the trim would need to apply.

 

Alternatively, you could just apply it to the whole documents array using array_filter

Is $data[11] the item that needs trimmed? It seems like you are just exploding it.

 

$documents = explode(",", $data[11]);
array_walk($documents, 'ltrim');

 

Should do what you want, not really knowing what you want and thinking that you do not know what you want.

Thanks Premiso for trying to help me out with this. Yes I now what I want to do just not how to. I need to trim any blank or empty space that may appear before a document's name. I tried that and got this error:

Warning: array_walk() expects parameter 1 to be array, string given in

 

You need to POST your code when you make changes, and post WHAT you tried.

* Please only comment if you are willing and able to help.

I posted the code I tried from premiso. I will post it again.

$documents = explode(",", $data[11]);

array_walk($documents, 'ltrim');

if ( ! empty($documents) && is_array($documents))  // added
		array_walk($documents, 'ltrim');
		foreach($documents as $addFile)

 

Try it like that.

 

Or like this:

 

if ( ! empty($documents) && is_array($documents))  // added
		array_walk($documents, 'ltrim');
		foreach($documents as $addFile) {
			$addFile = ltrim($addFile);

I posted the code I tried from premiso. I will post it again.

No, YOU didn't. She posted that code. YOU posted an error. There are plenty of people who post on here who would read her post, change the code, and say it didn't work. There's no logical reason for that code to cause that error, explode CANNOT return a string. It can return a BOOLEAN or an array. Therefore, the REST of your code matters too.

 

 

 

* Please only comment if you are willing and able to help.

I'm certainly able, but if you're not able to learn how to GET help, you won't get it. Don't worry, I won't bother trying to help you anymore.

array_walk cannot be used with call-back functions that return values. It needs to be used with call-back functions that modify a value that is passed by reference.

 

array_map works with a call-back function that return a value.

Premiso, Sorry I should have been more clear. This part of my code works. There is a addFile and also a addFileTYPE,  The addFile is the part that needs the spaces trimmed.

if($data[11] == "" || empty($data[11]))
			$documents = "";
		else
			$documents		= explode(",", $data[11]);

if ( ! empty($documents) )  

		foreach($documents as $addFile)
		{
			if(strpos($addFile, ".doc") !== false)
			$addFileType = "DOC";
			else
				$addFileType = "PDF";

			$insertAddFile = "INSERT INTO " FILES . "(`prod_id`, `file_name`, `file_type`) VALUES('$lastId', '$addFile', '$addFileType')";
			$wpdb->query($insertAddFile);

Thanks. I tried what you suggested and it changed all my file types to "PDF" and also didn't strip the spaces.

if ( ! empty($documents) )  

		foreach($documents as $addFile)
		{
			if(strpos($addFile, ".doc") !== false)
			$addFileType = "DOC";

			else
				$addFileType = "PDF";


	if ( ! empty($documents) && is_array($documents))  // added
		foreach($documents as $addFile) {
			$addFile = ltrim($addFile);

		}		



			$insertAddFile = "INSERT INTO ". FILES . "(`prod_id`, `file_name`, `file_type`) VALUES('$lastId', '$addFile', '$addFileType')";
			$wpdb->query($insertAddFile);
		}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.