PHP function to select array members?

I’m wanting to do something that I would have thought would be often
required - but I can’t find a function for it.

I have a large associative array $a1 and I want to generate a
sub-array $a3 consisting only of those elements of $a1 whose keys are
included in a list. The list could be keys or values of another
array, $a2.

Like array_replace(), but without the “If the key exists in the
second array, and not the first, it will be created in the first
array.”

Like array_intersect_key(), but with the two arrays being allowed to
be different lengths.

Like array_intersect_assoc(), but with the values not having to be
equal as well as the keys.

Can’t see one in the list in the manual at php.net. Can write my own
easily enough, but I’d rather not if it’s already there, hiding.

David


David Ledger - Freelance Unix Sysadmin in the UK.
HP-UX specialist of hpUG technical user group (www.hpug.org.uk)
email@hidden
www.ivdcs.co.uk


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Could array_uintersect be what you’re looking for?

"Returns an array containing all the values of array1 that are present in all the arguments"

http://www.php.net/manual/en/function.array-uintersect.php

Joe

On 7 Apr 2011, at 18:28, David Ledger wrote:

I’m wanting to do something that I would have thought would be often required - but I can’t find a function for it.

I have a large associative array $a1 and I want to generate a sub-array $a3 consisting only of those elements of $a1 whose keys are included in a list. The list could be keys or values of another array, $a2.

Like array_replace(), but without the “If the key exists in the second array, and not the first, it will be created in the first array.”

Like array_intersect_key(), but with the two arrays being allowed to be different lengths.

Like array_intersect_assoc(), but with the values not having to be equal as well as the keys.

Can’t see one in the list in the manual at php.net. Can write my own easily enough, but I’d rather not if it’s already there, hiding.

David


David Ledger - Freelance Unix Sysadmin in the UK.
HP-UX specialist of hpUG technical user group (www.hpug.org.uk)
email@hidden
www.ivdcs.co.uk


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Oh no, that’s matching keys and values against values, not keys.

On 7 Apr 2011, at 18:43, Joe Billings wrote:

Could array_uintersect be what you’re looking for?

“Returns an array containing all the values of array1 that are present in all the arguments”

PHP: array_uintersect - Manual

Joe

On 7 Apr 2011, at 18:28, David Ledger wrote:

I’m wanting to do something that I would have thought would be often required - but I can’t find a function for it.

I have a large associative array $a1 and I want to generate a sub-array $a3 consisting only of those elements of $a1 whose keys are included in a list. The list could be keys or values of another array, $a2.

Like array_replace(), but without the “If the key exists in the second array, and not the first, it will be created in the first array.”

Like array_intersect_key(), but with the two arrays being allowed to be different lengths.

Like array_intersect_assoc(), but with the values not having to be equal as well as the keys.

Can’t see one in the list in the manual at php.net. Can write my own easily enough, but I’d rather not if it’s already there, hiding.

David


David Ledger - Freelance Unix Sysadmin in the UK.
HP-UX specialist of hpUG technical user group (www.hpug.org.uk)
email@hidden
www.ivdcs.co.uk


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

I’m surprised there isn’t something native, but this will certainly
work:

<?php
$foo = array('tree' => 'larch','monty' => 'python', 'and_now' => 'for  
something completely different');
$bar = array('tree','monty');
$baz = array();
foreach($bar as $k) $baz[$k] = $foo[$k];
print_r($baz);
?>

Walter

On Apr 7, 2011, at 1:28 PM, David Ledger wrote:

I’m wanting to do something that I would have thought would be often
required - but I can’t find a function for it.

I have a large associative array $a1 and I want to generate a sub-
array $a3 consisting only of those elements of $a1 whose keys are
included in a list. The list could be keys or values of another
array, $a2.

Like array_replace(), but without the “If the key exists in the
second array, and not the first, it will be created in the first
array.”

Like array_intersect_key(), but with the two arrays being allowed to
be different lengths.

Like array_intersect_assoc(), but with the values not having to be
equal as well as the keys.

Can’t see one in the list in the manual at php.net. Can write my own
easily enough, but I’d rather not if it’s already there, hiding.

David


David Ledger - Freelance Unix Sysadmin in the UK.
HP-UX specialist of hpUG technical user group (www.hpug.org.uk)
email@hidden
www.ivdcs.co.uk


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Thanks Joe & Walter

Seems an obvious function to include. More so than many that are
there. The included functions are a really odd and incomplete bunch.
Are the built-in functions binary, in a pseudo code, or interpreted?

David

At 15:19 -0400 7/4/11, Walter Lee Davis wrote:

I’m surprised there isn’t something native, but this will certainly work:

<?php
$foo = array('tree' => 'larch','monty' => 'python', 'and_now' => 
'for something completely different');
$bar = array('tree','monty');
$baz = array();
foreach($bar as $k) $baz[$k] = $foo[$k];
print_r($baz);
?>

Walter

On Apr 7, 2011, at 1:28 PM, David Ledger wrote:

I’m wanting to do something that I would have thought would be
often required - but I can’t find a function for it.

I have a large associative array $a1 and I want to generate a
sub-array $a3 consisting only of those elements of $a1 whose keys
are included in a list. The list could be keys or values of another
array, $a2.

Like array_replace(), but without the “If the key exists in the
second array, and not the first, it will be created in the first
array.”

Like array_intersect_key(), but with the two arrays being allowed
to be different lengths.

Like array_intersect_assoc(), but with the values not having to be
equal as well as the keys.

Can’t see one in the list in the manual at php.net. Can write my
own easily enough, but I’d rather not if it’s already there, hiding.


David Ledger - Freelance Unix Sysadmin in the UK.
HP-UX specialist of hpUG technical user group (www.hpug.org.uk)
email@hidden
www.ivdcs.co.uk


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

The included functions are compiled C code, so they run pretty fast. But foreach is compiled, and array lookups are very fast, since they run in memory, so my long-hand loop code should be nearly as fast as some hypothetical native function.

Walter


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options