PHP – Read CSV Data into PHP Array

First post for a while! The website will be updated more regularly now though. Here is a quick PHP snippet that will read CSV data from a file specified and return a Multidimensional Array with keys based on column names.

	private function __readCSV($fileName) {
			$file = fopen($fileName, 'r'); 
			$fields = array();
			$CSVdata = array();
			
			if ($file) {
				while (($data = fgetcsv($file)) !== false) {
					if(empty($fields)) {
						$fields = $data;
						continue;
					}

					$row = array_combine($fields, $data);
					$CSVdata[] = $row;
				}
				fclose($file);
				unset($file, $row, $data, $fields);
				
				return $CSVdata;
			}
		}

Just call the function with the full path to the file. Any questions then leave a comment below. Enjoy.

PHP – Custom MVC Framework with Smarty3 and ActiveRecords

After using a few different PHP MVC’s I decided to work on my own framework when I had some spare time. Over the past few days I’ve put together a lightweight and feature rich small PHP MVC Framework. Note that it should currently be considered beta software, It should be useful for anyone wanting to develop their own MVC framework, feel free to use this as a base for your own framework or projects.

Read More

PHP Object to Multidimensional Array

Below is a piece of code showing how to transform an Object into a Multidimensional Array, there are quite a few functions in PHP that return stdClass objects such as some MySQL, JSON or XML functions. If it is a one dimensional array, you’ll probably typecast it (array)$data; to get the result you want – But if the Object has multiple dimensions to it and you typecast it you’ll get a mixture of Array with elements that contain Objects.

Read More

PHP Simple permission framework with Bitwise operations

Below is an example class of a basic permission framework with permissions worked out by using bitwise operations. This provides basic functions such as hasAccess() can() and permNeeded()

I won’t be going into detail regarding binary math and why it works as it does, as there are many other sites out there explaining this in great detail. This is just an example code post.

Read More