I am converting a csv file into a 2D array. The file is basically a list of songs, where each line (array) has the info about the songs such as name, author, length, quality etc
song1,author a, 3mins, 128kbits
song2, author b, 4mins, 96kbits
song3, author c, 2mins, 128kbits
song4, author d, 5mins, 192kbits
I want a 2D array like this
$dm = array (); // (the 2D array)
in this array a set of arrays, each representing a song
song1 => array (song=song1 author=author a, len=3mins q=128kbits
song2 => array (song=song2 author=author b, len=4mins q=96kbits
song3 => array (etc....)
song4 => array (etc...)
I managed to create the 2D array like this with push_array in a loop
[0] => array (song=song1 author=author a, len=3mins q=128kbits
[1] => array (song=song2 author=author b, len=4mins q=96kbits
[2] => array (etc....)
[3] => array (etc...)
but I cannot form the code to rename [0] to song1 => , song2 => , etc
I tried:
push_array ($dm['song'$s], array (song => $dat[1] author => $dat[2] len => $dat[3] q => $dat[4]))
... but this but it gave an error - "first argument should be an array" and while i got song1, song2,song3, etc elements fine, they were empty
2D Array element names
Moderator: General Moderators
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: 2D Array element names
Easier to just write it. lol
Code: Select all
$songs = array();
$columns = array('song', 'author', 'time', 'quality');
$lines= explode("\n", file_get_contents('filename.ext'));
foreach ($lines as $row $line) {
foreach (explode(',', $line) as $column => $value) {
if (!empty($columns[$column])) {
$songs[$row][$columns[$column]] = $value;
}
}
}Re: 2D Array element names
Here is the code (simplified)
while (!feof($file)) // LOOP UNTIL END OF csv FILE
{ $s++ ; // counter
$line = fgets($file); // open csv file
$line = chop($line);
$dat = split ("¬",$line); // ¬ = csv file separated by this sign // create an array from line contents
if ($dat[1] != "") // if not a blank line, then process
{ Print "<br><br><font color=blue> Processing Song No: " . $s . "<b> ";
Print ($dat [3]) . "</b></font>" ;
$temp = array (num => $dat[1], author => $dat[2], song => $dat[3], eng => $dat[4], typ => $dat[5],
len => $dat[6], size => $dat[7], ext => $dat[8], qty => $dat[9], lyr => $dat[10],
fav => $dat[11], r1 => $dat[12], r2 => $dat[13], r3 => $dat[14], sng_fn => $dat[15],
lyr_fn => $dat[16], lyr_ed => $dat[17], rem => $dat[18] );
array_push ($dm, $temp); // works fine but first key elemnts [0] =>
// array_push ($dm[Song.$s], $temp); // key elements renamed well but empty (due to a php error).
}
}
while (!feof($file)) // LOOP UNTIL END OF csv FILE
{ $s++ ; // counter
$line = fgets($file); // open csv file
$line = chop($line);
$dat = split ("¬",$line); // ¬ = csv file separated by this sign // create an array from line contents
if ($dat[1] != "") // if not a blank line, then process
{ Print "<br><br><font color=blue> Processing Song No: " . $s . "<b> ";
Print ($dat [3]) . "</b></font>" ;
$temp = array (num => $dat[1], author => $dat[2], song => $dat[3], eng => $dat[4], typ => $dat[5],
len => $dat[6], size => $dat[7], ext => $dat[8], qty => $dat[9], lyr => $dat[10],
fav => $dat[11], r1 => $dat[12], r2 => $dat[13], r3 => $dat[14], sng_fn => $dat[15],
lyr_fn => $dat[16], lyr_ed => $dat[17], rem => $dat[18] );
array_push ($dm, $temp); // works fine but first key elemnts [0] =>
// array_push ($dm[Song.$s], $temp); // key elements renamed well but empty (due to a php error).
}
}
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: 2D Array element names
I'd wager that mine is simpler, especially since it's shorter and does what you're asking for. 
But what do I know...
But what do I know...
Re: 2D Array element names
Thanks, I have posted my code without realising your quick post
Now I have a second question
So assume we have this 2D array
[1] => array ( song => 'Money' author => 'Elvis' q => '128')
[2] => array ( song => 'Virgin' author => 'Madonna' q => '96')
[3] => array ( song => 'Dance' author => 'Sveta' q => '192')
[4] => array ( song => 'Love' author => 'John' q => '128')
[5] => array ( song => 'Go' author => 'Madonna' q => '256')
now I want to sort the main array according any of the key, for example by author, then quality so as I have the following sorting:
[1] => array ( song => 'Money' author => 'Elvis' q => '128')
[2] => array ( song => 'Love' author => 'John' q => '128')
[3] => array ( song => 'Virgin' author => 'Madonna' q => '96')
[4] => array ( song => 'Go' author => 'Madonna' q => '256')
[5] => array ( song => 'Dance' author => 'Sveta' q => '192')
Hope it can be done easily!
Now I have a second question
So assume we have this 2D array
[1] => array ( song => 'Money' author => 'Elvis' q => '128')
[2] => array ( song => 'Virgin' author => 'Madonna' q => '96')
[3] => array ( song => 'Dance' author => 'Sveta' q => '192')
[4] => array ( song => 'Love' author => 'John' q => '128')
[5] => array ( song => 'Go' author => 'Madonna' q => '256')
now I want to sort the main array according any of the key, for example by author, then quality so as I have the following sorting:
[1] => array ( song => 'Money' author => 'Elvis' q => '128')
[2] => array ( song => 'Love' author => 'John' q => '128')
[3] => array ( song => 'Virgin' author => 'Madonna' q => '96')
[4] => array ( song => 'Go' author => 'Madonna' q => '256')
[5] => array ( song => 'Dance' author => 'Sveta' q => '192')
Hope it can be done easily!
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: 2D Array element names
Not tested but it should give you the idea (and maybe even work):
Code: Select all
$lines = file($file);
$keys = array('song', 'author', 'len', 'qual');
$i = 0;
foreach($lines as $line) {
$vals = explode('¬', $line);
//build main array
$result['song'.$i] = array_combine($keys, $vals);
//build sortable arrays
$song['song'.$i] = $result['song'.$i]['song'];
$auth['song'.$i] = $result['song'.$i]['author'];
$len['song'.$i] = $result['song'.$i]['len'];
$qual['song'.$i] = $result['song'.$i]['qual'];
//etc
$i++;
}
array_multisort($auth, SORT_ASC, $qual, SORT_ASC, $result);mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: 2D Array element names
To sum that up, use array_multisort() on the array in order to sort it. Abra's code also cleverly uses array_combine(), a function that I never looked at because I thought it was an alias for array_merge(). With that, I could remove the inner foreach loop...
Code: Select all
$songs = array();
$columns = array('song', 'author', 'time', 'quality');
$lines= explode("\n", file_get_contents('filename.ext'));
foreach ($lines as $line) {
$songs[] = array_combine($columns, explode(',', $line);
}Re: 2D Array element names
Thanks very much to all of you, esp. the sort codes. I learnt that handling arrays is not very easy like Mysql, (which I am familiar with) but for this small table, it was not worthwhile to utilise a new mysql account from my domain (limit of 5, used 4). Great forum by helpful, expert forum members!
