Solved

Adding Key and Value to PHP Associative Array

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

734 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