Check out my twitter script by CMXads
PHP Gallery PHP Gallery is a free php image script. | Till death do us part Axels New Music releases | Evil gear Christmas is almost here | Sharp Stuff Switchblades Icepicks and german knives |
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.
Build an associative array from any delimited text file the the easy way using file, explode, unset, in array, array combine
associative array file csv array combine
associative-array-from-file-the-easy-way added Oct 27 2014
<?php
$target='b1234';
$file='myfile.csv';
$result='';
if(file_exists($file)){
//product file is not large file is fine to use
//otherwise use fopen
$lines = file($file);
if ($lines)
{
//one way to create an associative array from text file with csv type header row
//in this case get first row explode our field names make array and trim
$fields = explode(';',trim($lines[0]));
// print_r($fields);
//now unset the first line or line[0] with fields
unset($lines[0]);
// print_r($lines);
//make sure we have lines!
if(count($lines)>1){
//get each line
foreach($lines as $line){
//explode line into array and trim
$line = explode(';',trim($line));
//check if target key exists
if(in_array($target,$line)){
//check field count matches value count
if (sizeof($line)==sizeof($fields)){
//combine fields and values into associative array
$result = array_combine($fields,$line);
//remove empty elements except 0
//this works in one shot but throws notice
//$result = array_diff($result, array( '' ) );
//so we do it this way
$result = array_filter($result);
$result=array_slice($result, 0 );
}
}
}
}
}
}
if(!$result){
echo 'Data error';
exit;
}
/>