function wk_opts_build($input, $output, $options) { $command = ""; foreach ($options as $key => $option) { if (null !== $option && false !== $option) { if (true === $option) { $command .= ' --' . $key; } elseif (is_array($option)) { if (is_array_assoc($option)) { foreach ($option as $k => $v) { $command .= ' --' . $key . ' ' . escapeshellarg($k) . ' ' . escapeshellarg($v); } } else { foreach ($option as $v) { $command .= " --" . $key . " " . escapeshellarg($v); } } } else { $command .= ' --' . $key . " " . escapeshellarg($option); } } } $command .= ' ' . escapeshellarg($input) . ' ' . escapeshellarg($output); return $command; }
function array_to_object($array) { if (is_array($array)) { if (!is_array_assoc($array)) { // Don't convert to object return array_map(__FUNCTION__, $array); } return (object) array_map(__FUNCTION__, $array); } return $array; }
public function testIsArrayAssoc() { $this->assertFalse(is_array_assoc(null)); $this->assertFalse(is_array_assoc(true)); $this->assertFalse(is_array_assoc(false)); $this->assertFalse(is_array_assoc(1)); $this->assertFalse(is_array_assoc('')); $this->assertFalse(is_array_assoc([])); $this->assertFalse(is_array_assoc([1, 2, 3])); $this->assertFalse(is_array_assoc(['1', '2', '3'])); $this->assertTrue(is_array_assoc(['a' => '1', '2', '3'])); $this->assertTrue(is_array_assoc(['a' => '1', 'b' => '2', 'c' => '3'])); }
/** * Reset array indexes while keep string indexes intact */ function array_reindex(&$array) { // if this is a if (!is_array_assoc($array)) { $array = array_values($array); } if (is_array_deeper($array, 1)) { foreach ($array as $key => $value) { if (is_array($value)) { array_reindex($array[$key]); } } } }
protected function execute(InputInterface $input, OutputInterface $output) { if (!empty($this->appFolder)) { $this->container->setScalar('rootFolder', $this->appFolder); } $this->checkAndSetArrayFile(); $info = pathinfo($this->arrayFile); $services = (require $this->arrayFile); if (!is_array_assoc($services)) { throw new InvalidConfigException("Looks the the {$info['basename']} did not return an associative array!"); } $this->output->writeln("<info>The following services will be " . "availble from your application</info>"); foreach ($services as $interface => $service) { $this->output->writeln("\t{$interface} => {$service}"); } $outfile = $this->container->get('rootFolder') . '/config/services.json'; $this->output->writeln("<info>Creating {$outfile}</info>"); file_put_contents($outfile, json_encode($services, JSON_PRETTY_PRINT)); $this->output->writeln("<info>Done.</info>"); }
/** * Processing of complicated rendering structures * * @static * * @param string $input * @param array|bool|string $data * * @return string */ static function __callStatic($input, $data) { if ($data === false) { return false; } if (is_scalar($data)) { $data = [$data]; } elseif (isset($data[1]) && $data[1] === false && !isset($data[2])) { unset($data[1]); } $input = trim($input); /** * Analysis of called tag. If nested tags presented */ if (static::analyze_selector($input)) { /** * If tag name ends with pipe "|" symbol - for every element of array separate copy of current tag will be created */ if (strpos($input[0], '|') !== false) { $input[0] = substr($input[0], 0, -1); $output = []; /** * When parameters are not taken in braces - make this operation, if it is necessary */ if (count($data) > 2 || isset($data[1]) && is_array_indexed($data[1])) { $data = [$data]; } foreach ($data[0] as $d) { if (isset($d[0]) && is_array_indexed($d[0]) && !in_array($d[0][0], static::$unit_atributes)) { if (isset($d[1]) && (!is_array($d[1]) || is_array_indexed($d[1]) && !in_array($d[1][0], static::$unit_atributes))) { $output_ = []; foreach ($d as $d_) { $output_[] = static::__callStatic($input[1], $d_); } $output[] = $output_; unset($output_); } else { $output[] = [static::__callStatic($input[1], $d[0]), isset($d[1]) ? $d[1] : '']; } } else { $output[] = static::__callStatic($input[1], $d); } } unset($d); } elseif (!isset($data[1]) || is_array_assoc($data[1])) { $output = static::__callStatic($input[1], [isset($data[0]) ? $data[0] : '', isset($data[1]) ? $data[1] : false]); $data[1] = []; } else { $output = static::__callStatic($input[1], $data); $data[1] = []; } return static::__callStatic($input[0], [$output, isset($data[1]) ? $data[1] : false]); } if (substr($input, -1) == '|') { $input = substr($input, 0, -1); $data = [$data]; } /** * Fix for textarea tag, which can accept array as content */ if (strpos($input, 'textarea') === 0 && isset($data[0]) && is_array_indexed($data[0]) && !is_array($data[0][0])) { $data[0] = implode("\n", $data[0]); } /** * If associative array given then for every element of array separate copy of current tag will be created */ if (is_array_indexed($data)) { if (count($data) > 2) { $output = ''; foreach ($data as $d) { $output .= static::__callStatic($input, $d); } return $output; } elseif (strpos($input, 'select') !== 0 && strpos($input, 'datalist') !== 0 && strpos($input, 'input') !== 0 && (is_array_indexed($data[0]) && (!isset($data[1]) || !is_array($data[1]) || is_array_indexed($data[1]) && !in_array($data[1][0], static::$unit_atributes)))) { $output = ''; foreach ($data as $d) { $output .= static::__callStatic($input, $d); } return $output; } elseif (is_array_indexed($data[0]) && (strpos($input, 'select') !== 0 && strpos($input, 'datalist') !== 0 || is_array_indexed($data[0][0]) && !in_array($data[0][0][0], static::$unit_atributes))) { $output = ''; foreach ((array) $data[0] as $d) { $data[1] = isset($data[1]) ? $data[1] : []; if (!is_array($d) || !isset($d[1]) || !is_array($d[1])) { $output .= static::__callStatic($input, [$d, $data[1]]); } elseif (is_array_indexed($d[1]) && !in_array($d[1], static::$unit_atributes)) { $output .= static::__callStatic($input, [$d[0], $data[1]]) . static::__callStatic($input, [$d[1], $data[1]]); } else { $output .= static::__callStatic($input, [$d[0], static::array_merge($data[1], $d[1])]); } } return $output; } elseif (!is_array($data[0]) && !in_array($data[0], static::$unit_atributes) && isset($data[1]) && (!is_array($data[1]) || is_array_indexed($data[1]) && !in_array($data[1][0], static::$unit_atributes))) { $output = ''; foreach ($data as $d) { $output .= static::__callStatic($input, $d); } return $output; } } else { $data[0] = $data; } if (!isset($data[0])) { $data[0] = ''; } /** * Second part of expression - fix for "select" and "datalist" tags because they accept array as values */ if (!is_array($data[0]) || (strpos($input, 'select') === 0 || strpos($input, 'datalist') === 0) && !isset($data[0]['in'])) { $data[0] = ['in' => $data[0]]; } if (isset($data[1])) { $data = static::array_merge($data[0], $data[1]); } else { $data = $data[0]; } $attrs = []; /** * Attributes processing */ if (($pos = mb_strpos($input, '[')) !== false) { $attrs_ = explode('][', mb_substr($input, $pos + 1, -1)); $input = mb_substr($input, 0, $pos); foreach ($attrs_ as &$attr) { $attr = explode('=', $attr, 2); if (isset($attr[1])) { $attrs[$attr[0]] = $attr[1]; } else { $attrs[] = $attr[0]; } } unset($attrs_, $attr); } /** * Classes processing */ if (($pos = mb_strpos($input, '.')) !== false) { if (!isset($attrs['class'])) { $attrs['class'] = ''; } $attrs['class'] = trim($attrs['class'] . ' ' . str_replace('.', ' ', mb_substr($input, $pos))); $input = mb_substr($input, 0, $pos); } unset($pos); /** * Id and tag determination */ $input = explode('#', $input); $tag = $input[0]; if (isset($input[1])) { $attrs['id'] = $input[1]; } $attrs = static::array_merge($attrs, $data); unset($data); if ($tag == 'select' || $tag == 'datalist') { if (isset($attrs['value'])) { $in = ['in' => $attrs['in'], 'value' => $attrs['value']]; unset($attrs['in'], $attrs['value']); } else { $in = ['in' => $attrs['in']]; unset($attrs['in']); } } elseif (isset($attrs['in'])) { $in = $attrs['in']; unset($attrs['in']); } else { $in = ''; } if (isset($attrs['insert'])) { $insert = $attrs['insert']; unset($attrs['insert']); $data = [$in, $attrs]; static::inserts_processing($data, $insert); $html = ''; foreach ($data as $d) { if (method_exists(get_called_class(), $tag)) { $html .= static::$tag($d[0], $d[1]); } elseif (in_array($tag, static::$unpaired_tags)) { $d[1]['tag'] = $tag; $d[1]['in'] = $d[0]; $html .= static::u_wrap($d[1]); } else { $html .= static::wrap($d[0], $d[1], $tag); } } return $html; } if (method_exists(get_called_class(), $tag)) { $in = static::$tag($in, $attrs); } elseif (in_array($tag, static::$unpaired_tags)) { $attrs['tag'] = $tag; $attrs['in'] = $in; $in = static::u_wrap($attrs); } else { $in = static::wrap($in, $attrs, $tag); } return $in; }
/** * Convert an multi-dimensional array to xml. * http://stackoverflow.com/questions/1397036/how-to-convert-array-to-simplexml * * @param object $object Link to SimpleXMLElement object * @param array $data Array which need to convert into xml */ function array_to_xml(array $data, SimpleXMLElement $object) { foreach ($data as $key => $value) { if (is_array($value)) { if (is_array_assoc($value)) { // For associative arrays use keys as child object $new_object = $object->addChild($key); array_to_xml($value, $new_object); } else { // For sequential arrays use parent key as child foreach ($value as $new_value) { if (is_array($new_value)) { array_to_xml(array($key => $new_value), $object); } else { $object->addChild($key, $new_value); } } } } else { //$object->$key = $value; // See note about & here - http://php.net/manual/en/simplexmlelement.addchild.php#112204 $object->addChild($key, $value); } } }
function http_build_multi_query($data, $key = null) { $query = array(); if (empty($data)) { return $key . '='; } $is_array_assoc = is_array_assoc($data); foreach ($data as $k => $value) { if (is_string($value) || is_numeric($value)) { $brackets = $is_array_assoc ? '[' . $k . ']' : '[]'; $query[] = urlencode(is_null($key) ? $k : $key . $brackets) . '=' . rawurlencode($value); } else { if (is_array($value)) { $nested = is_null($key) ? $k : $key . '[' . $k . ']'; $query[] = http_build_multi_query($value, $nested); } } } return implode('&', $query); }
public function testArrayIndexed() { $this->assertFalse(is_array_assoc(array('wibble', 'wubble', 'wobble'))); }
/** * Checks whether array is indexed or not * * @param array $array Array to be checked * * @return bool */ function is_array_indexed($array) { if (!is_array($array) || empty($array)) { return false; } return !is_array_assoc($array); }
/** * Returns [n..$] * * @param array $array * @param int $n * * @return array */ function array_rest($array, $n = 1) { return array_slice($array, $n, null, is_array_assoc($array)); }
/** * Flattens the oprovided array * @param type $data * @param type $all * @param string $lastkey */ protected function flatten_config($data, &$all, $lastkey = '') { if (is_array_assoc($data)) { if (!empty($lastkey)) { $lastkey = $lastkey . '.'; } foreach ($data as $key => $value) { $this->flatten_config($value, $all, $lastkey . $key); } } else { $all[$lastkey] = $data; } }
/** * @dataProvider providerIsArrayAssoc * @group arrays */ public function testIsArrayAssoc($result, $array) { $this->assertSame($result, is_array_assoc($array)); }
private static function _chunk($conditions) { return is_array_assoc($conditions) ? array_chunk($conditions, 1, TRUE) : $conditions; }
/** * Sets a list of scalers into the conatiner * @param array $scalars * @throws InvalidConfigException */ public function setScalars(array $scalars) { if (!is_array_assoc($scalars)) { throw new InvalidConfigException('The \\$scalars argument must be an associatibe array!'); } foreach ($scalars as $name => $value) { $this->setScalar($name, $value); } }
/** * @param mixed $form * @param string $prefix * @return array */ function form_flatten($form, $prefix = '') { $ret = array(); if (is_object($form)) { $form = get_object_vars($form); } if (is_array($form)) { if (is_array_assoc($form)) { foreach ($form as $k1 => $v1) { $ret = array_merge($ret, form_flatten($v1, empty($prefix) ? $k1 : $prefix . "[{$k1}]")); } } elseif (is_array_list($form)) { foreach ($form as $k1 => $v1) { $ret = array_merge($ret, form_flatten($v1, empty($prefix) ? $k1 : $prefix . "[]")); } } else { debug_assert(false, "Mixed array cannot be encoded by multipart_form_data_encode."); } } else { $ret[] = array('name' => $prefix, 'value' => $form); } return $ret; }
function is_arrayish_assoc($val) { $arrCopy = getAsArray($val); if (!$arrCopy || !is_array($arrCopy) || !count($arrCopy)) return false; if (is_array($arrCopy)) return is_array_assoc($arrCopy); return false; }