Check out my twitter script by CMXads
Evil gear Christmas is almost here | Viking gear Gear Gifts and Apparel | PHP Gallery PHP Gallery is a free php image script. | Sell your soul Believeth in me an hath everlasting life |
here are some of the best and useful php scripts and snippets to help in your projects. Php displayed below. Use the search for specific script lookups. Click the category links to view scripts in Javascript and cgi. To add your own script click the link and add your useful script example.
associative array delimited file data return using array combine
array combine delimited file array associative
<?php
//set file fields array names for associative must match column count
$fields=array(
'id',
'product_code',
'product_name',
'product_desc',
'product_img',
'price',
'weight',
'catalog',
'catid',
);
//get file contents with lock and parse
function parse_file($filename,$delimiter) {
$str = FALSE;
if(file_exists($filename) and is_readable($filename)){
if($handle = @fopen($filename, 'r')){
while(!$str){
if(flock($handle, LOCK_SH)){
if($str = file_get_contents($filename)){
flock($handle, LOCK_UN);
}}}fclose($handle);
if($str){
global $fields;
$res=array();
$lines=array();
$output = array();
$output = explode("\n", $str);
$output=array_filter($output);
$output=array_map('trim', $output);
foreach ($output as $key=>$data) {
$lines = explode($delimiter, $data);
$res[]=array_combine($fields,$lines);
}
return $res;
}}}}
//file name and field delimiter
$res=parse_file('cart-data.txt',';');
//line 1 key value
echo '<br>Return any line data by line number[1] column[product_code]' . $res[1]['product_code'].'<br>';
//or foreach or for loop
echo '<pre>';
var_dump($res);
echo '</pre>';
?>