WP-Mix

A fresh mix of code snippets and tutorials

PHP foreach loop

I am always using this PHP code snippet, which basically loops through an array that may or may not include nested arrays. So to make things easier, I am posting here for quick reference.

foreach ($array as $key => $value) {
	if (is_array($value)) {
		foreach ($value as $k => $v) {
			
			// do something
		}
	} else {
		
		// do something
	}
}

This code loops through the $array and checks to see if the value if each key also is an array. If so, it loops through that array as well. Basic, but frequently used.

★ Pro Tip:

USP Pro