将一个二维数组转为 hashmap 或者 取值

如果省略 $valueField 参数，则转换结果每一项为包含该项所有数据的数组。

@param array $arr 二维数组
@param string $keyField 唯一 如果不是唯一会出现覆盖$value , 为空变成取某一字段的值 $valueField不能忽略
@param string $valueField

@return array
160551
public static function to_map($arr, $keyField = null, $valueField = null)
    {
        $map = [];
        if ($valueField) {
            foreach ($arr as $row) {
                if ($keyField) {
                    $map[$row[$keyField]] = $row[$valueField];
                } else {
                    $map[] = $row[$valueField];
                }
            }
        } else {
            foreach ($arr as $row) {
                if ($keyField) {
                    $map[$row[$keyField]] = $row;
                } else {
                    $map[] = $row;
                }
            }
        }
        return $map;
    }
1194641
public function setValueArrayInt(array &$valueArray, $key, $value)
    {
        Validate::isString($key, 'key');

        if (!empty($value)) {
            Validate::isInteger($value, 'value');
            $valueArray[$key] = $value;
        }
    }
1087341
public function &toArrayOf($fieldName, $keyField = null)
    {
        $arr = [];
        foreach ($this as $object) {
            if ($keyField === null) {
                $arr[] = $object->$fieldName;
            } else {
                $arr[$object->$keyField] = $object->$fieldName;
            }
        }

        return $arr;
    }
634233
protected function prepareWhereIn($field, array $values = [], $cacheKey)
    {
        if (is_array($field)) {
            foreach ($field as $name => $values) {
                $this->prepareWhereIn($name, $values, $cacheKey);
            }
        } elseif (count($values)) {
            $this->builderCache->store($cacheKey, [$field => $values]);
        }
    }
781478
public static final SerIterable array(final Class<?> valueType) {
        final List<Object> list = new ArrayList<>();
        return new SerIterable() {
            @Override
            public SerIterator iterator() {
                return array(build(), Object.class, valueType);
            }
            @Override
            public void add(Object key, Object column, Object value, int count) {
                if (key != null) {
                    throw new IllegalArgumentException("Unexpected key");
                }
                if (count != 1) {
                    throw new IllegalArgumentException("Unexpected count");
                }
                for (int i = 0; i < count; i++) {
                    list.add(value);
                }
            }
            @Override
            public Object[] build() {
                Object[] array = (Object[]) Array.newInstance(valueType, list.size());
                return list.toArray(array);
            }
            @Override
            public Class<?> valueType() {
                return valueType;
            }
            @Override
            public List<Class<?>> valueTypeTypes() {
                return EMPTY_VALUE_TYPES;
            }
        };
    }
1308375
private function parseKeys(array $arr) {
        $output = $this->getArrayValue();
        $append_regex = '/\s*\+\s*$/';
        foreach ($arr as $k => $v) {
            if (is_array($v) && FALSE === strpos($k, '.')) {
                // this element represents a section; recursively parse the value
                $output[$k] = $this->parseKeys($v);
            } else {
                // if the key ends in a +, it means we should append to the previous value, if applicable
                $append = false;
                if (preg_match($append_regex, $k)) {
                    $k = preg_replace($append_regex, '', $k);
                    $append = true;
                }

                // transform "a.b.c = x" into $output[a][b][c] = x
                $current = & $output;

                $path = $this->property_nesting ? explode('.', $k) : array($k);
                while (($current_key = array_shift($path)) !== null) {
                    if ('string' === gettype($current)) {
                        $current = array($current);
                    }

                    if (!array_key_exists($current_key, $current)) {
                        if (!empty($path)) {
                            $current[$current_key] = $this->getArrayValue();
                        } else {
                            $current[$current_key] = null;
                        }
                    }
                    $current = & $current[$current_key];
                }

                // parse value
                $value = $v;
                if (!is_array($v)) {
                  $value = $this->parseValue($v);
                }

                if ($append && $current !== null) {
                    if (is_array($value)) {
                        if (!is_array($current)) {
                            throw new LogicException("Cannot append array to inherited value '{$k}'");
                        }
                        $value = array_merge($current, $value);
                    } else {
                        $value = $current . $value;
                    }
                }

                $current = $value;
            }
        }

        return $output;
    }
945367
private function fieldList($key, $value, array $fields)
    {
        return [
            'type'   => $fields[$key]['type'],
            'before' => ['render' => Json::parse($value[0])],
            'after'  => ['render' => Json::parse($value[1])],
        ];
    }
683270
public function makeArrayFieldValue($value)
    {
        $value = $this->_unserializeValue($value);
        if (!$this->_isEncodedArrayFieldValue($value)) {
            $value = $this->_encodeArrayFieldValue($value);
        }

        return $value;
    }
296603
public final function map($where, $valueField, $keyField = null, $rows = []) {
        $sql = $this->select($valueField, $keyField);
        $sql->where($where);
        $rst = $sql->toArray($valueField, $keyField, $rows);
        $this->checkSQL($sql);

        return $rst;
    }
1265958
private function recurArrayChange($array, $keyArray, $value = null)
    {
        $key0 = $keyArray[0];
        if (count($keyArray) === 1) {
            $this->changeValue($array, $key0, $value);
        } else if (is_array($array) && isset($keyArray[1])) {
            unset($keyArray[0]);
            $keyArray = array_values($keyArray);
            if (!isset($array[$key0])) {
                $array[$key0] = [];
            }
            $array[$key0] = $this->recurArrayChange($array[$key0], $keyArray, $value);
        } else {
            $this->changeValue($array, $key0, $value);
        }
        return $array;
    }
14433
public function enum(string $enumClass, array $valueLabelMap) : FieldBuilderBase
    {
        return $this->field->enum($enumClass, $valueLabelMap);
    }
393183
public static function fromAssocArray(array $keyValueOptions, string $valueType = 'string') : ArrayFieldOptions
    {
        $options = [];

        foreach ($keyValueOptions as $value => $label) {
            settype($value, $valueType);
            $options[] = $label instanceof IFieldOption
                    ? $label
                    : new FieldOption($value, $label);
        }

        return new self($options);
    }
150300
private function getArrayIndexByKeyValue($array, $value, $key = 'id')
    {
        foreach ($array as $index => $result) {
            if ($result[$key] === $value) {
                return $index;
            }
        }

        return false;
    }
1319154
public function manyToPHP(array $values, array $fields, Driver $driver)
    {
        foreach ($fields as $field) {
            if (!isset($values[$field])) {
                continue;
            }

            $values[$field] = json_decode($values[$field], true);
        }

        return $values;
    }
161199
public static function in($value, array $array, $returnKey = false)
    {
        $inArray = in_array($value, $array, true);

        if ($returnKey) {
            if ($inArray) {
                return array_search($value, $array, true);
            }

            return null;
        }

        return $inArray;
    }
1227199
public function asArray($key = null, $value = null)
    {
        if (!is_null($key))
        {
            Helpers\Validate::table($this->name)->field($key);
        }
        if (!is_null($value))
        {
            Helpers\Validate::table($this->name)->field($value);
        }

        $datas = array();
        if (!$this->resetKeys)
        {
            if (is_null($key) && is_null($value))
            {
                return $this->data;
            }
            else
            {
                foreach ($this->data as $rowKey => $data)
                {
                    $datas[$rowKey] = array();
                    foreach ($data as $row)
                    {
                        if (is_null($key))
                        {
                            $datas[$rowKey][] = $row->{$value};
                        }
                        elseif (is_null($value))
                        {
                            $datas[$rowKey][$row->{$key}] = $row;
                        }
                        else
                        {
                            $datas[$rowKey][$row->{$key}] = $row->{$value};
                        }
                    }
                }
            }
        }
        else
        {
            if (is_null($key) && is_null($value))
            {
                foreach ($this->data as $data)
                {
                    $datas[] = get_object_vars($data);
                }
            }
            else
            {
                foreach ($this->data as $data)
                {
                    if (is_null($key))
                    {
                        $datas[] = $data->{$value};
                    }
                    elseif (is_null($value))
                    {
                        $datas[$data->{$key}] = $data;
                    }
                    else
                    {
                        $datas[$data->{$key}] = $data->{$value};
                    }
                }
            }
        }

        return $datas;
    }
905729
public function validate($field, $value, $validator) {
		if($this->match_keys === true) {
			return array_key_exists($value, $this->array);
		} else {
			return in_array($value, $this->array);
		}
	}