256 Kilobytes

Answers in Server-Side | By Some Guy

Published | Last Update

329 views, 0 RAMs, and 1 comment

Tags: PHP, Errors, Function, Programming

Profile Photo - Some GuySome GuyBasic Subscriber

Can someone please explain to me why I get the warning for an invalid argument for the following code? I am just trying to run a program for my work and this keeps happening.

Code:

$data = $_POST;

    if (count($data) > '1') {
        var_dump($data);

        foreach ($data as $k => $row) {
            var_dump($row);

                foreach ($row as $k2 => $img) {
                    echo $img;
                }
    }

Data:

array (size=2)
  'image' => 
    array (size=2)
      0 => string '608' (length=3)
      1 => string '610' (length=3)
  'submit' => string 'Save' (length=11)
array (size=2)
  0 => string '608' (length=3)
  1 => string '610' (length=3)

608610

string 'Save' (length=11)

Download more RAM. 🐏 ⨉ 0Posted by Some Guy 2 years ago

Edit History

• [2018-12-16 9:29 PST] Some Guy (2 years ago)
🕓 Posted at 16 December, 2018 09:29 AM PST

This is a debug account.

Profile Photo - Hash BrownHash BrownInternet ActivistEnglandStaff

Hello,

You need to use count() and is_array() to make sure your data is actually an array and it's not empty.

The submit value is causing the issue, on the second loop submit is causing your code to break.

Do some checks like this:

$data = $_POST;

if (count($data) > 1)
{
    var_dump($data);

    foreach ($data as $k => $row)
    {
        var_dump($row);

        if(is_array($row) && count($row)>0)//you should add this.
        {
            foreach ($row as $k2 => $img)
            {
                echo $img;
            }
        }
    }
}

Download more RAM. 🐏 ⨉ 0Posted by Hash Brown 2 years ago 🕓 Posted at 19 December, 2018 08:46 AM PST

"THAT DOG IS GETTING RAPED" - Terry A. Davis

Post a New Comment

Do you like having a good time?

Register an Account

You can also login to an existing account or reset your password. All use of this site is subject to the terms of service and privacy policy.

Read Quality Articles

Read some quality articles. If you can manage to not get banned for like five minutes, you can even post your own articles.

View Articles →

Argue with People on the Internet

Use your account to explain why people are wrong on the Internet forum.

View Forum →

Vandalize the Wiki

Or don't. I'm not your dad.

View Wiki →

Ask and/or Answer Questions

If someone asks a terrible question, post a LMGTFY link.

View Answers →

Make Some Money

Hire freelancers and/or advertise your goods and/or services. Hire people directly. We're not a middleman or your dad. Manage your own business transactions.

Register an Account

Answers— Read More

Find more related content below!