0

Im having a lot of difficulty with arrays in PHP. They require me to write a lot of codes such as isset(), empty(), array_key_exist(); And I really dont want to deal with these. If the key doesnt exist just handle it as a null.

$arr = [
   'location' => 'Paris'
]

$arr['country'] // boom crash. How to walkaround this?

Any suggestions?

EDIT

I dont want to use any if condition. No isset(), array_key_exist, exceptions, etc. I just want them to be null if the key doesn't exist? Is this possible in PHP? The application is very abstract and data may vary on each request.

Community
  • 1
  • 1
  • Check with [isset](http://php.net/isset) or [array_key_exists](http://php.net/array_key_exists). – Peon Jan 20 '14 at 07:49
  • I suppose PHP supports `try ... catch`, maybe you should take a look at that. – Realitätsverlust Jan 20 '14 at 07:51
  • I know about if conditions. I dont want them. I have a huge amount of data. This would require me to have many if(isset()) in the code. And thats not beautiful. Thanks. –  Jan 20 '14 at 07:51
  • `@$arr['country']` but just use the `if`, really... – elclanrs Jan 20 '14 at 07:51
  • @Error404 Why you want to write something invalid and expect php not to output any error? – Mr. Alien Jan 20 '14 at 07:51
  • Here you go, exception handling with PHP, found it.^^ http://php.net/manual/en/language.exceptions.php – Realitätsverlust Jan 20 '14 at 07:52
  • @YUNOWORK Exceptions doesn't help me. I want the program to continue even if a key doesn't exist. It's a very abstract application. –  Jan 20 '14 at 07:54
  • possible duplicate of [isset() and empty() make code ugly](http://stackoverflow.com/questions/1960509/isset-and-empty-make-code-ugly) – deceze Jan 20 '14 at 07:57
  • well ... `try catch` would do exactly what you want. But k, i dont get what you actually want. – Realitätsverlust Jan 20 '14 at 07:57
  • @YUNO No, exception catching doesn't do anything here, because notices are not exceptions. – deceze Jan 20 '14 at 07:58
  • Extend array iterator and do the checking in your class: http://www.php.net/manual/en/class.iterator.php – andho Jan 20 '14 at 08:08
  • why not `$value = array_key_exists($key, $arr)? $arr[$key]: null`? – IROEGBU Jan 20 '14 at 09:44

4 Answers4

0
function getValue(array $array, $key) {
    return isset($array[$key]) ? $array[$key] : null;
}

echo getValue($mysteryArray, 'mysteryKey');

Or:

$array += array_fill_keys(array('foo', 'bar', 'baz'), null);
echo $array['foo'];
deceze
  • 510,633
  • 85
  • 743
  • 889
  • This is better than set_error_handler as you mentioned. This inspired me to write my own answer. I will post it soon! Dankeschön! –  Jan 20 '14 at 08:06
0

My own function inspired from deceze. Works perfectly.

/**
 * Fill array with null on nonexistent keys
 *
 * @param array $arg
 * @param array $possible_keys
 */
function fillNull(array $arg, array $possible_keys){
    foreach($possible_keys as $key){
        $result[$key] = empty( $arg[$key] ) ? null : $arg[$key];
    }
    return $result;
}
0

You can use ArrayIterator or some class that gives you the interface you desire.

<?php

class MyArrayIterator extends ArrayIterator {

        public function __construct($array, $flags=0) {
                parent::__construct($array, $flags);
        }

        public function offsetGet($index) {
                if (!$this->offsetExists($index)) {
                        return null;
                }

                return parent::offsetGet($index);
        }

}

$arr = [
   'location' => 'Paris'
];

$arrIt = new MyArrayIterator($arr);

echo $arrIt['country'];

echo "Only this is echoed";
andho
  • 1,166
  • 1
  • 15
  • 27
-2

@$arr['country'] - suppress errors, bad pratice.

&$arr['country'] - use reference, could add additional elements to array, bad pratice.

sectus
  • 15,605
  • 5
  • 55
  • 97