public static function serializeAttr(array $data, array $order=[]){
		$attr = [];

		/* convert data to sorted array */
		$sorted = static::sortedAttr($data, $order);

		foreach ( $sorted as list($key, $value) ){
			if ( is_array($value) ){
				/* ignore empty arrays */
				if ( count($value) === 0 ){
					continue;
				}
				foreach ( static::_serializeAttrArray($key, $value) as $sub ){
					$value = htmlspecialchars($sub[1], ENT_QUOTES | ENT_HTML5);
					$attr[] = "{$sub[0]}=\"{$value}\"";
				}
			} else {
				if ( $value === true ){
					$attr[] = "$key";
				} else if ( $value === false ){
					/* ignore */
				} else {
					$value = htmlspecialchars($value, ENT_QUOTES | ENT_HTML5);
					$attr[] = "$key=\"$value\"";
				}
			}
		}
		return implode(' ', $attr);
	}
public static function serializeAttr(array $data, array $order=[]){
		$attr = [];

		/* convert data to sorted array */
		$sorted = static::sortedAttr($data, $order);

		foreach ( $sorted as list($key, $value) ){
			if ( is_array($value) ){
				/* ignore empty arrays */
				if ( count($value) === 0 ){
					continue;
				}
				foreach ( static::_serializeAttrArray($key, $value) as $sub ){
					$value = htmlspecialchars($sub[1], ENT_QUOTES | ENT_HTML5);
					$attr[] = "{$sub[0]}=\"{$value}\"";
				}
			} else {
				if ( $value === true ){
					$attr[] = "$key";
				} else if ( $value === false ){
					/* ignore */
				} else {
					$value = htmlspecialchars($value, ENT_QUOTES | ENT_HTML5);
					$attr[] = "$key=\"$value\"";
				}
			}
		}
		return implode(' ', $attr);
	}
public static function serializeAttr ( array $ data , array $ order = [ ] ) { $ attr = [ ] ; /* convert data to sorted array */ $ sorted = static :: sortedAttr ( $ data , $ order ) ; foreach ( $ sorted as list ( $ key , $ value ) ) { if ( is_array ( $ value ) ) { /* ignore empty arrays */ if ( count ( $ value ) === 0 ) { continue ; } foreach ( static :: _serializeAttrArray ( $ key , $ value ) as $ sub ) { $ value = htmlspecialchars ( $ sub [ 1 ] , ENT_QUOTES | ENT_HTML5 ) ; $ attr [ ] = "{$sub[0]}=\"{$value}\"" ; } } else { if ( $ value === true ) { $ attr [ ] = "$key" ; } else if ( $ value === false ) { /* ignore */ } else { $ value = htmlspecialchars ( $ value , ENT_QUOTES | ENT_HTML5 ) ; $ attr [ ] = "$key=\"$value\"" ; } } } return implode ( ' ' , $ attr ) ; }
Takes key-value array and serializes them to a string as
'key="value" foo="bar"'.

['foo' => 'bar']            becomes foo="bar".
['class' => ['foo', 'bar']  becomes class="foo bar"
['data' => ['foo' => 'bar'] becomes data-foo="bar"

@param $data Data to serializeattr
@param $order Array with predefined order keys should appear
in. Keys not in this array will appear last in any order.
Takes key - value array and serializes them to a string as key = value foo = bar .