how to count foreach loop in php code example

Example 1: foreach loop array php

foreach (array as $value){ 
  //code to be executed; 
  print("value : $value");
} 

foreach (array as  $key => $value){ 
  //code to be executed; 
  print("key[$key] => $value");
}

Example 2: php wpdb foreach

get_header();
function get_records()
{
	//$con=connectsql();
	$records=$wpdb->query("SELECT * FROM $wpdb->posts WHERE post_type = 'elements' AND post_status = 'publish'");
	$all=array();
	while($data=$records->fetch_assoc())
	{
		$all[]=$data;
	}
	return $all;
}

Tags:

Php Example