Skip to content

Instantly share code, notes, and snippets.

@tim-cotten
Last active March 5, 2021 12:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tim-cotten/5acaabbe338f98a50b637ecdcb704ee8 to your computer and use it in GitHub Desktop.
Save tim-cotten/5acaabbe338f98a50b637ecdcb704ee8 to your computer and use it in GitHub Desktop.
PHP Bugs: Arrays Don't Like Undefined Offsets (Example D)
<?php
// Set relative product URLs
foreach ($products as &$product) {
$url = "&product_id={$product['id']}";
$name = explode(" -- ", $product['name']); // like: Chocolates -- 587
$url .= "&name={$name[0]}";
if ($name[1] != "") {
$name[1] = str_pad($name[1], 8, "0", STR_PAD_LEFT); // format as 00000587
$url .= $name[1];
}
$product['url'] = $url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment