Skip to content

Instantly share code, notes, and snippets.

@harmony7
Last active June 5, 2018 01:25
Show Gist options
  • Save harmony7/c1c08f502eda17bcb585c231b823095d to your computer and use it in GitHub Desktop.
Save harmony7/c1c08f502eda17bcb585c231b823095d to your computer and use it in GitHub Desktop.
PHP gotcha

Today i ran into a PHP gotcha, and it had me stumped for a few minutes so i thought i'd share it with you.

$found = false;
foreach($arr as &$item) {
  if ($item['type'] == "mytype") {
    $found = true;
    break;
  }
}

if (!$found) {
  $item = [ 'type' => 'mytype' ];
  $arr[] = $item;
}

This was causing my custom item at the end to show up twice in my list and i was completely confused.

If you'd like to guess why.

I see why it happened but I find this to be unintuitive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment