Файл: adultscript-2.0.3-pro/files/libraries/framework/array.php
Строк: 129
<?php
defined('_VALID') or die('Restricted Access!');
class VArray {
public static function move($input, $index, $direction)
{
$array = $input;
if ($direction == 'up') {
if ((count($array) > $index) && ($index > 0)) {
array_splice($array, $index-1, 0, $input[$index]);
array_splice($array, $index+1, 1);
}
} else {
if (count($array) > $index) {
array_splice($array, $index+2, 0, $input[$index]);
array_splice($array, $index, 1);
}
}
return $array;
}
public static function combine($keys, $values)
{
$keys_count = count($keys);
$values_count = count($values);
$size = ($keys_count > $values_count) ? $values_count : $keys_count;
$keys = array_slice($keys, 0, $size);
$values = array_slice($values, 0, $size);
return array_combine($keys, $values);
}
public static function to_xml($data, $rootNodeName = 'data', &$xml=NULL, $format=FALSE)
{
// turn off compatibility mode as simple xml throws a wobbly if you don't.
if (ini_get('zend.ze1_compatibility_mode') == 1)
{
ini_set ('zend.ze1_compatibility_mode', 0);
}
if (is_null($xml))
{
$xml = simplexml_load_string("");
}
// loop through the data passed in.
foreach($data as $key => $value)
{
// if numeric key, assume array of rootNodeName elements
if (is_numeric($key))
{
$key = $rootNodeName;
}
// delete any char not allowed in XML element names
$key = preg_replace('/[^a-z0-9-_.:]/i', '', $key);
// if there is another array found recrusively call this function
if (is_array($value))
{
// create a new node unless this is an array of elements
$node = self::is_assoc($value) ? $xml->addChild($key) : $xml;
// recrusive call - pass $key as the new rootNodeName
self::to_xml($value, $key, $node);
}
else
{
// add single node.
$value = htmlentities($value, ENT_QUOTES, 'UTF-8');
$xml->addChild($key,$value);
}
}
if ($format === TRUE) {
$doc = new DOMDocument('1.0');
$doc->preserveWhiteSpace = false;
$doc->loadXML( $xml->asXML() );
$doc->formatOutput = true;
return $doc->saveXML();
}
// pass back as string. or simple xml object if you want!
return $xml->asXML();
}
public static function from_xml($xml)
{
if (is_string($xml)) {
$xml = new SimpleXMLElement($xml);
}
$children = $xml->children();
if (!$children) {
return (string) $xml;
}
$array = array();
foreach ($children as $key => $node) {
$node = self::from_xml($node);
// support for 'anon' non-associative arrays
if ($key == 'anon') {
$key = count($array);
}
// if the node is already set, put it into an array
if ( isset($array[$key])) {
if (!is_array($array[$key]) || $array[$key]['0'] === NULL) {
$array[$key] = array($array[$key]);
}
$array[$key][] = $node;
} else {
$array[$key] = $node;
}
}
return $array;
}
// determine if a variable is an associative array
public static function is_assoc($array)
{
return (is_array($array) && 0 !== count(array_diff_key($array, array_keys(array_keys($array)))));
}
// create a tree for a array (used only for subcategories)
public static function tree($categories)
{
$tree = array();
foreach ($categories as $category) {
$cat_id = (int) $category['cat_id'];
$parent_id = (int) $category['parent_id'];
if ($parent_id === 0) {
if (isset($tree[$cat_id])) {
$tree[$cat_id]['cat_id'] = $cat_id;
$tree[$cat_id]['parent_id'] = $parent_id;
$tree[$cat_id]['name'] = $category['name'];
$tree[$cat_id]['description'] = (isset($category['description'])) ? $category['description'] : NULL;
$tree[$cat_id]['slug'] = (isset($category['slug'])) ? $category['slug'] : NULL;
$tree[$cat_id]['total_videos'] = (isset($category['total_videos'])) ? $category['total_videos'] : NULL;
$tree[$cat_id]['adv'] = (isset($category['adv'])) ? $category['adv'] : NULL;
$tree[$cat_id]['status'] = (isset($category['status'])) ? $category['status'] : NULL;
$tree[$cat_id]['title'] = (isset($category['title'])) ? $category['title'] : NULL;
$tree[$cat_id]['meta_title'] = (isset($category['meta_title'])) ? $category['meta_title'] : NULL;
$tree[$cat_id]['meta_desc'] = (isset($category['meta_desc'])) ? $category['meta_desc'] : NULL;
$tree[$cat_id]['meta_keys'] = (isset($category['meta_keys'])) ? $category['meta_keys'] : NULL;
} else {
$tree[$cat_id] = $category;
$tree[$cat_id]['subcategories'] = array();
}
} else {
if (!isset($tree[$parent_id])) {
$tree[$parent_id] = array();
$tree[$parent_id]['subcategories'] = array();
}
$tree[$parent_id]['subcategories'][] = $category;
}
}
return $tree;
}
}