From 928c520170bd9c8f407c3f8f35d8f5bb718e10a6 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Wed, 21 May 2014 00:45:42 -0700 Subject: [PATCH] CRM_Utils_Array::index() -- Allow indexing when some keys are NULL/undefined --- CRM/Utils/Array.php | 4 ++-- tests/phpunit/CRM/Utils/ArrayTest.php | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CRM/Utils/Array.php b/CRM/Utils/Array.php index 6518865625..b90d217050 100644 --- a/CRM/Utils/Array.php +++ b/CRM/Utils/Array.php @@ -706,9 +706,9 @@ class CRM_Utils_Array { $node = &$result; foreach ($keys as $key) { if (is_array($record)) { - $keyvalue = $record[$key]; + $keyvalue = isset($record[$key]) ? $record[$key] : NULL; } else { - $keyvalue = $record->{$key}; + $keyvalue = isset($record->{$key}) ? $record->{$key} : NULL; } if (isset($node[$keyvalue]) && !is_array($node[$keyvalue])) { $node[$keyvalue] = array(); diff --git a/tests/phpunit/CRM/Utils/ArrayTest.php b/tests/phpunit/CRM/Utils/ArrayTest.php index 2118c25b2f..4204e1c283 100644 --- a/tests/phpunit/CRM/Utils/ArrayTest.php +++ b/tests/phpunit/CRM/Utils/ArrayTest.php @@ -57,12 +57,18 @@ class CRM_Utils_ArrayTest extends CiviUnitTestCase { 'familiar' => TRUE, 'value' => 'Hey' ); + $inputs[] = array( + 'msgid' => 'greeting', + 'familiar' => TRUE, + 'value' => 'Universal greeting' + ); $byLangMsgid = CRM_Utils_Array::index(array('lang', 'msgid'), $inputs); $this->assertEquals($inputs[4], $byLangMsgid['en']['greeting']); $this->assertEquals($inputs[1], $byLangMsgid['en']['parting']); $this->assertEquals($inputs[2], $byLangMsgid['fr']['greeting']); $this->assertEquals($inputs[3], $byLangMsgid['fr']['parting']); + $this->assertEquals($inputs[5], $byLangMsgid[NULL]['greeting']); } function testCollect() { -- 2.25.1