Solved

Adding Key and Value to PHP Associative Array

Posted on 2011-03-22
2
304 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

636 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