Пример #1
0
/**
 * pick values from an array
 *
 * @param array $array input array
 * @param string|integer $key key
 * @param boolean $keepIndex if keep index
 * @return array
 * @since 1.0
 */
function rock_array_pick($array, $key, $keepIndex = false)
{
    if (!is_array($array)) {
        return array();
    }
    $ret = array();
    foreach ($array as $index => $row) {
        if (is_array($row)) {
            $value = rock_array_get($row, $key);
            if ($keepIndex) {
                $ret[$index] = $value;
            } else {
                $ret[] = $value;
            }
        }
    }
    return $ret;
}
Пример #2
0
 function attr($name)
 {
     return rock_array_get($this->_attrs, $name);
 }
Пример #3
0
 /**
  * load field data
  *
  */
 function doLoad()
 {
     $collection = $this->_mongodb->selectCollection($this->collection);
     $id = xn("id");
     $field = xn("field");
     $type = "integer";
     $data = null;
     if ($id) {
         $one = $collection->findOne(array("_id" => rock_real_id($id)));
         //not select field, because there is a bug in list, such as "list.0"
         $data = rock_array_get($one, $field);
         switch (gettype($data)) {
             case "boolean":
                 $type = "boolean";
                 break;
             case "integer":
                 $type = "integer";
                 break;
             case "long":
                 $type = "long";
                 break;
             case "float":
             case "double":
                 $type = "double";
                 break;
             case "string":
                 $type = "string";
                 break;
             case "array":
                 $type = "mixed";
                 break;
             case "object":
                 // int64 is returned as object (Kyryl Bilokurov <*****@*****.**>)
                 if (get_class($data) == "MongoInt64") {
                     $type = "long";
                 } else {
                     $type = "mixed";
                 }
                 break;
             case "resource":
                 $type = "mixed";
                 break;
             case "NULL":
                 $type = "null";
                 break;
         }
     }
     $exporter = new VarExportor($this->_mongodb, $data);
     $format = rock_cookie("rock_format", "json");
     $represent = $exporter->export($format);
     if ($format == "json") {
         $represent = json_unicode_to_utf8($represent);
     }
     $this->_outputJson(array("code" => 200, "type" => $type, "value" => $type == "long" ? $data->__toString() : $data, "represent" => $represent, "format" => $format));
 }
Пример #4
0
/**
 * pick values from an array, the make it as keys
 * 
 * <code>
 * $array = array(
 *   array("a" => 11, "b" => 12),
 *   array("a" => 21, "b" => 22)
 *   //...
 * );
 * 
 * $array2 = rock_array_combine($array, "a", "b");
 * </code>
 * 
 * then $array2 will be:
 * <code>
 * array(
 *   11 => 12,
 *   21 => 22
 * );
 * </code>
 * 
 * if $valueName not be set, the element value be "value":
 * 
 * <code>
 * $array2 = rock_array_combine($array, "a");
 * 
 * array(
 *   11 => array("a" => 11, "b" => 12),
 *   21 => array("a" => 21, "b" => 22)
 * );
 * </code>
 * 
 * support dot (.) operator in keyName and valueName:
 * - rock_array_combine($array, "a.b", "a.c"); 
 * $array[n][a][b] will be "key",$array[n][a][c] value be"value", here, n is index
 *
 * @param array $array array values to combine from
 * @param integer|string $keyName key name
 * @param integer|string $valueName value name
 * @return array
 * @since 1.0
 */
function rock_array_combine($array, $keyName, $valueName = null)
{
    $ret = array();
    foreach ($array as $row) {
        if (is_array($row)) {
            $keyValue = rock_array_get($row, $keyName);
            $value = is_null($valueName) ? $row : rock_array_get($row, $valueName);
            if ($keyValue) {
                $ret[$keyValue] = $value;
            } else {
                $ret[] = $value;
            }
        }
    }
    return $ret;
}
Пример #5
0
    ?>
selected="selected"<?php 
}
?>
>DESC</option></select></p>
			<p><input type="text" name="field[]" value="<?php 
h_escape(rock_array_get(x("field"), 3));
?>
" /> <select name="order[]"><option value="asc" <?php 
if (rock_array_get(x("order"), 3) == "asc") {
    ?>
selected="selected"<?php 
}
?>
>ASC</option><option value="desc" <?php 
if (rock_array_get(x("order"), 3) == "desc") {
    ?>
selected="selected"<?php 
}
?>
>DESC</option></select> </p>
		</td>
	</tr>
	<tr>
		<td colspan="2">
			<!-- query fields and hints -->
			<span id="fieldsAndHints" <?php 
if (x("command") != "findAll") {
    ?>
style="display:none"<?php 
}