Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8864119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:09:30+00:00 2026-06-14T16:09:30+00:00

Seems strange I can’t find an answer to this, but here goes: This: foreach

  • 0

Seems strange I can’t find an answer to this, but here goes:

This:

foreach ($stuffs as $stuffRow) {
    foreach ($stuffRow as $stuff) {
        if($stuff=== null){
            unset($stuff);
        }
    }
}

is not working.

I know I shouldn’t say “isn’t working”, but that’s all I’ve got. The if condition works; I’ve tested it. However, when I apply unset, the key=>value is still there.

Note: PHP 5.3

Many thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T16:09:32+00:00Added an answer on June 14, 2026 at 4:09 pm

    While this might work (i never do it this way and not tested it):

    foreach ($stuffs as &$stuffRow) {
        foreach ($stuffRow as &$stuff) {
            if($stuff=== null){
                unset($stuff);
            }
        }
    }
    

    If i remember correctly, passing by reference isn’t a good idea in foreachs (but can’t remember why, might have been in the php doc somewhere, maybe someone can clarify in the comments, but i think it’s because foreach works on a copy of the array and passing a reference passes the reference to the copy, not the original, though that is just an educated guess). Instead, try this.

    foreach ($stuffs as $key1=>$stuffRow) {
        foreach ($stuffRow as $key2=>$stuff) {
            if($stuff=== null){
                unset($stuffs[$key1][key2]);
            }
        }
    }
    

    The reason your code didn’t work by the way is because foreach loops iterate over a COPY of your array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can't seem to find a way of doing this, but it seems strange
It seems really strange and I can't figure out why this is happening. This
This seems strange to me but I am sure there is an explanation If
Very strange bug I can't seems to figure out. I am trying to get
I am having a strange issue that I can't seem to resolve. Here is
It seems strange that the language apparently includes no suitable functionality. I find myself
This seems really strange to me: f = File.open(Dir.pwd+'/tmp','r') f.readlines do |l| puts #{f.lineno}:#{l}
I just tried the following code, but the result seems a little strange. It
I know this question seems strange. I use .NET Micro Framework to write a
Is this statement correct? Seems strange to me... From Apple FAQ: If I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.