You are here: Selectively Extract Sub-Array from an Array « PHP « IT news, forums, messages
Selectively Extract Sub-Array from an Array

Posted by Tom Rawson on 10/19/05 11:13

On PHP 4.3.x ... (not using PHP 5)

Say I have two arrays, the first has keys 'key1' ... 'key20'. The
second has some other keys with different names (i.e. they do not
overlap those in the first array). I want to add certain elements from
array1 to array2. I can do it like this:

$array2['key3'] = $array1['key3'];
$array2['key8'] = $array1['key8'];
$array2['key17'] = $array1['key17'];

or like this:

$array2 += array('key3' => $array1['key3'], 'key8' => $array1['key8'],
'key17' => $array1['key17']);

What would be nicer is something like:

$array2 += array_select($array1, 'key3', 'key8', 'key17');

In other words ... a way to create an array that is a selected subset
of another.

Is there a function that does this that I'm missing? I realize I could
write one but I was looking for something built-in. Nothing I can see
under array functions in the manual seems to do it.

Thanks,

--
Tom

 

Navigation:

[Reply to this 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

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