PHP Dotted Array

PHP Dotted Array

Sometimes you may need such a script, which lets you access to an array fields with string having dots as a depth delimiter. Let’s have an array:

$array = array(
	'women' => array(
		'cloths' => 'always lack',
		'money' => 'never enough',
		'mind' => 'calm',
	),
	'men' => array(
		'cloths' => 'whatever',
		'money' => 'hidden',
		'mind' => 'reckless',
	),
);

Normally it looks like this:

echo $array['men']['cloths'];

But for the code clarity and also other benefits you could use it this way:

$da = new Picios\Lib\DottedArray($array);
echo $da->get('men.cloths');

In both cases the output will be:

whatever

So what the hell is that for, if the code is longer and it requires using additional class?

Now the benefits

code clarity

Like I said earlier, the code clarity. It’s easier to read and write

'men.cloths'

than

['man']['cloths']

easy controll

Moreover if you try to get to not existing array index with the code:

echo $array['women']['head'];

you’ll get a notice of undefined index. To prevent this, we use condition

if (array_key_exists('head', $array['women'])) {
	echo $array['women']['head'];
}

againts

if ($da->get('women.head')) {
	echo $da->get('women.head');
}

extendable

you can always extend the basic functionality of a class.

class MySession extends Picios\Lib\DottedArray
{

	public function getUser()
	{
		return $this->get('user.data');
	}


	public function isLoggedIn()
	{
		return $this->get('user.data') ? true : false;
	}
}

and then use it

$mysession = new MySession($_SESSION);
if ($mysession->isLoggedIn()) {
	var_dump($mysession->getUser());
}

Here’s a complete source code of the class. The snippet is a part of my own framework.
Use it well nerds

<?php

/**
 * Get or Set array node using dot notation
 *
 * @author picios
 * @copyright (c) 2016, picios
 */

namespace Picios\Lib;


class DottedArray
{

	private $array = array();
	
	public function __construct($array)
	{
		$this->array = $array;
	}

	public function set($key, $value = true)
	{
		$reference = & $this->getReference($key, true);
		$reference = $value;
	}

	public function &get($key = false)
	{
		if (!$key) {
			return $this->array;
		}
		return $this->getReference($key);
	}

	public function delete($key)
	{

		$keys = explode('.', $key);
		if (count($keys) < 2) {
			unset($this->array[$keys[0]]);
			return true;
		}
		$lastKeyToDelete = array_pop($keys);
		$longKey = implode('.', $keys);
		$reference = & $this->getReference($longKey);
		unset($reference[$lastKeyToDelete]);
	}

	protected function &getReference($key, $forWritting = false)
	{
		$keys = explode('.', $key);
		$reference = & $this->array;
		foreach ($keys as $levelKey) {
			if ($forWritting || isset($reference[$levelKey])) {
				$reference = & $reference[$levelKey];
			} else {
				$result = null;
				return $result;
			}
		}

		return $reference;
	}

}

You can also download it from Github.com

2 comments to “PHP Dotted Array”

  1. Wow, I this is what Ive been searching for. Thanks dude

  2. hi, it’s awesome. Works for me :*

Leave a comment

Your email address will not be published. Required fields are marked *