Reply to Efficiency question: Overwrite array value, or ask if it is set?

Your name:

Reply:


Posted by Markus Ernst on 07/06/06 08:52

Hi

While looping through an array I extract all keys on the fly, as in this
example:

$data = array(
array('name' => 'Helen', 'age' => '40'),
array('name' => 'Mark', 'email' => 'mark@xyz.com', 'age' => '90'),
);
$keys = array();
foreach ($data as $row) {
foreach ($row as $key => $value) {
// do other things here
$keys[$key] = 1;
}
}
$column_names = array_keys($keys);

Now my question is about the line
$keys[$key] = 1;

This overwrites existing entries and thus creates a set of unique keys.
This could also be done without overwriting:
if (!isset($keys[$key])) $keys[$key] = 1;

So I wonder which is more efficient - overwriting the array entry at
every loop, or checking for it with the if statement.

Thanks for a comment
Markus

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация