Solved

Adding Key and Value to PHP Associative Array

Posted on 2011-03-22
2
249 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now