Solved

Adding Key and Value to PHP Associative Array

Posted on 2011-03-22
2
271 Views
Last Modified: 2012-06-27
Experts,
I am having some trouble with my code.  I have the following:
foreach( $files as $fname )
{
	$filedate = date("m/d/Y", filemtime($fname));
	$file_name = substr($fname,0,7).' Entry Report - '.$filedate;
	$final[$fname] = $file_name;
	
}

sort($final);
print_r($final);

Open in new window


I am wanting to add key and value to the $final array.  The value goes in fine, but the output of the print_r displays numerical keys instead of the value I assigned. Both $fname and $file_name have the correct values assigned. What am I doing wrong?

Thanks..
0
Comment
Question by:kwilliams123
2 Comments
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
ID: 35192164
use asort($final)
instead of sort, it reassigns the keys
0
 

Author Closing Comment

by:kwilliams123
ID: 35192179
That did it..  Thanks for your prompt response.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question