Skip to content

Instantly share code, notes, and snippets.

@m-torin
Last active November 5, 2017 17:13
Show Gist options
  • Save m-torin/1005110 to your computer and use it in GitHub Desktop.
Save m-torin/1005110 to your computer and use it in GitHub Desktop.
xmlparse
<?php
/**
* Cast all the DataOne features SimpleXMLObject into an array
*
* @param SimpleXMLObject $objects
* @return array
*/
function array_cast_features( $data ) {
$return = array();
foreach( $data as $objects ) {
foreach( $objects->children() as $object ) {
$attrs = $object->attributes();
if( $attrs ):
$key = esc_attr( $attrs->name );
if( ! isset( $return[$key] ) )
$return[$key] = array();
foreach( $object->children() as $child_object ) {
if( $child_object->attributes() ):
$child_key = esc_attr( $child_object->attributes()->name );
if( ! isset( $return[$key][$child_key] ) )
$return[$key][$child_key] = array();
if( array_search( (string)$child_object->value, $return[$key][$child_key] ) === false )
$return[$key][$child_key][] = (string) $child_object->value;
endif;
}
endif;
}
}
return $return;
}
/**
* Cast all DataOne specifications SimpleXMLObject into an array
*
* @parama SimpleXMLObject $data
* @return array
*/
function array_cast_specifications( $data ) {
$return = array();
foreach( $data as $objects ) {
foreach( $objects->children() as $object ) {
$attrs = $object->attributes();
if( $attrs ):
$key = esc_attr($attrs->name);
$object = (array) $object;
$object = array_filter( $object, 'strip_empty_var' );
unset( $object['@attributes'] );
if( ! isset( $return[$key] ) )
$return[$key] = array();
$return[$key][] = $object;
endif;
}
}
foreach( $return as $key => $value ) {
$return[$key] = array_merge_special( $return[$key] );
}
return $return;
}
/**
* VinQuery xml to array
*
* @param SimpleXMLObject $results
* @return array
*/
function VinQuery_to_array( $results ) {
$return = array();
foreach( $results as $item ) {
foreach( $item->children() as $child ) {
$attrs = $child->attributes();
if( $attrs->Key ) {
$key = esc_attr( (string)$attrs->Key );
$value = (string)$attrs->Value;
$unit = (string)$attrs->Unit;
if( ! isset( $return[$key] ) )
$return[$key] = array();
if( strip_empty_var( $value ) && array_search( $value, $return[$key] ) === false ) {
$return[$key][] = $value.$unit;
}
}
}
}
return $return;
}
/**
* Fill vinQuery fields
*
* @param array $field, custom field holder
* @param array $keys, custom field group keys
* @param array $data, VinQuery results
*/
function fill_vinQuery_fields( &$field, $keys, $data ) {
foreach( $keys as $key ) {
if( ! $field[$key] && vin_array_key_exists( $key, $data) ) {
$field[$key] = $data[$key];
}
}
}
/**
* Strip empty variables
*
* @param mixed
* @return bool
*/
function strip_empty_var( $value ) {
if( $value == 'N/A' || $value == 'No data' )
return false;
return ! empty( $value );
}
/**
* Merge multiple arrays keys into a single key and cast values as array
*
* @param array $data, arrays to be merged
* @return array
*/
function array_merge_special( $data ) {
$return = array();
foreach( $data as $arr ) {
foreach( $arr as $key => $value ) {
if( ! isset( $return[$key] ) )
$return[$key] = array();
if( array_search( $value, $return[$key] ) === false && $value )
$return[$key][] = $value;
}
}
return $return;
}
/**
* Array exists
*
* A simple sanitization is made before checking for array_key_exists.
*
* @param $key, key to check for
* @param $array
* @return bool
*/
function vin_array_key_exists( $key, $array ) {
if( ! is_array( $array) )
return false;
return array_key_exists( $key, $array );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment