开发者

Best way to test if array key exists

开发者 https://www.devze.com 2022-12-09 11:03 出处:网络
Imagine that I want to create an array from another array like this: $array = array(\'bla\' => $array2[\'bla\'],

Imagine that I want to create an array from another array like this:

 $array = array('bla' => $array2['bla'],
                'bla2' => $array2['bla2'],
                'foo' => $array2['foo'],
                'Alternative Bar' => $array['bar'],
                'bar' => $array2['bar']);

What is the best way to test either the $array2 has that index I'm passing to the other array, without using an if for each index I want to add.

Note that the key from the $array can be different from the $arr开发者_StackOverfloway2


What I did was, creating a template to the array with the keys that I want e.g.

$template = array('key1', 'key2', 'key3');

Then, I would merge the template with the other array, if any key was missing in the second array, then the value of the key would be null, this way I don't have the problem of warnings telling me about offset values.

$template = array('key1', 'key2', 'key3');
$array = array_merge($template, $otherarray);


if i understood right...

$a = array('foo' => 1, 'bar' => 2, 'baz' => 3);
$keys = array('foo', 'baz', 'quux');

$result = array_intersect_key($a, array_flip($keys));

this will pick only existing keys from $a


A possibility would be:

   $array = array(
        'bla'  => (isset($array2['bla']) ? $array2['bla'] : ''),
        'bla2' => (isset($array2['bla2']) ? $array2['bla2'] : ''),
        'foo'  => (isset($array2['foo']) ? $array2['foo'] : ''),
        'xxx'  => (isset($array2['yyy']) ? $array2['yyy'] : ''),
        'bar'  => (isset($array2['bar']) ? $array2['bar'] : '')
     );

If this shoud happen more dynamically, I would suggest to use array_intersect_key, like soulmerge posted. But that approach would have the tradeoff that only arrays with the same keys can be used.

Since your keys in the 2 arrays can vary, you could build something half-dynamic using a mapping array to map the keys between the arrays. You have at least to list the keys that are different in your arrays.

//key = key in $a, value = key in $b
$map = array( 
    'fooBar' => 'bar'    
); 

$a = array(
    'fooBar' => 0,
    'bla' => 0,
    'xyz' => 0
); 

$b = array(
    'bla' => 123,
    'bar' => 321,
    'xyz' => 'somevalue'  
);

foreach($a as $k => $v) {
    if(isset($map[$k]) && isset($b[$map[$k]])) {
        $a[$k] = $b[$map[$k]];
    } elseif(isset($b[$k])){
        $a[$k] = $b[$k];
    }
}

That way you have to only define the different keys in $map.


You want to extract certain keys from array1 and set non-existing keys to the empty string, if I understood you right. Do it this way:

# Added lots of newlines to improve readability
$array2 = array_intersect_key(
    array(
        'bla' => '',
        'bla2' => '',
        'foo' => '',
        # ...
    ),
    $array1
);


Perhaps this...

$array = array();
foreach ( $array2 as $key=>$val )
{
    switch ( $key )
    {
        case 'bar':
            $array['Alternative bar'] = $val;
            break;
        default:
            $array[$key] = $val;
            break;            
    }
}

For any of the "special" array indexes, use a case clause, otherwise just copy the value.

0

精彩评论

暂无评论...
验证码 换一张
取 消