Custom PHP Class for working with arrays. Code example open

Custom PHP Class for working with arrays. Code example

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators
Tested: PHP 5.6 +
class Custom_Walker {

	public $items = array();

	function __construct( $items ) {

		$this->items = $items;
	}

	function get_item( $name, $value ) {

		if ( ! $this->items ) {
			return false;
		}

		foreach ( $this->items as $item ) {

			if ( isset( $item->$name ) && $item->$name == $value ) {
				return $item;
			}
		}

		return false;
	}

	function get_item_value( $byname, $nameValue, $getName ) {

		if ( ! $this->items ) {
			return false;
		}

		if ( ! $item = $this->get_item( $byname, $nameValue ) ) {
			return false;
		}

		return isset( $item->$getName ) ? $item->$getName : false;
	}

	function get_items( $args = false ) {

		if ( ! $this->items ) {
			return false;
		}

		if ( ! $args ) {
			return $this->items;
		}

		$items = array();
		foreach ( $this->items as $item ) {

			$correct = true;
			foreach ( $args as $key => $value ) {
				if ( is_array( $value ) ) {
					if ( ! in_array( $item->$key, $value ) ) {
						$correct = false;
						break;
					}
				} else {
					if ( $item->$key != $value ) {
						$correct = false;
						break;
					}
				}
			}

			if ( $correct ) {
				$items[] = $item;
			}
		}

		return $items;
	}

	function get_field_values( $field_name ) {

		if ( ! $this->items ) {
			return false;
		}

		$fields = array();
		foreach ( $this->items as $item ) {
			if ( ! isset( $item->$field_name ) ) {
				continue;
			}
			$fields[] = $item->$field_name;
		}

		return $fields;
	}

	function get_index_values( $index_field, $value_field ) {

		if ( ! $this->items ) {
			return false;
		}

		$pack = array();
		foreach ( $this->items as $item ) {
			if ( ! isset( $item->$index_field ) || ! isset( $item->$value_field ) ) {
				continue;
			}
			$pack[ $item->$index_field ] = $item->$value_field;
		}

		return $pack;
	}

	function is_set( $name, $value ) {

		if ( ! $this->items ) {
			return false;
		}

		foreach ( $this->items as $item ) {

			if ( isset( $item->$name ) && $item->$name == $value ) {
				return true;
			}
		}

		return false;
	}

	function count( $args = false ) {
		return count( $this->get_items( $args ) );
	}

}

This code defines a PHP class Rcl_Walker with several methods.

The class is designed to work with an array of items, which can be set as a property during instantiation (__construct() method). The main purpose of this class is to provide convenient methods for working with the items in the array.

The methods provided by this class include:

get_item(): This method searches through the items in the array and returns the first item that has a specific value for a specified field ($name).
Returns false if not found.

get_item_value(): This method is similar to get_item(), but instead of returning the entire item, it returns the value of a specified field ($getName) from the item that matches the search criteria. get_items(): This method filters the items in the array based on one or more search criteria (specified as an associative array $args). Returns an array of all items that match the criteria.

get_field_values(): This method returns an array of values for a specified field ($field_name) from all items in the array.

get_index_values(): This method returns an associative array where the keys are taken from a specified field ($index_field) and the values are taken from another specified field ($value_field) from all items in the array.

is_set(): This method searches through the items in the array and returns true if at least one item has a specific value for a specified field ($name). Returns false if not found.

count(): This method returns the number of items in the array that match the search criteria (specified as an associative array $args). If no criteria are specified, returns the total number of items in the array.

0

More

Leave a Reply

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

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation