OnArray.php 1.39 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 1/10/16
 * Time: 11:47 AM
 */


namespace qfq\helper;

require_once(__DIR__ . '/../../qfq/Constants.php');

class OnArray {

    /**
     * @param array $dataArray
     * @param string $keyValueGlue
     * @param string $rowGlue
     * @return string
     */
    public static function toString(array $dataArray, $keyValueGlue = '=', $rowGlue = '&') {
        $dataString = '';

        foreach ($dataArray as $key => $value) {
            $dataString .= $key . $keyValueGlue . $value . $rowGlue;
        }

        $glueLength = strlen($rowGlue);
        return substr($dataString, 0, strlen($dataString) - $glueLength);
    }

    /**
     * Sort array by array keys
     *
     * @param array $a
     */
    public static function sortKey(array &$a) {

        $result = array();

        $keys = array_keys($a);
        sort($keys);
        foreach ($keys as $key) {
            $result[$key] = $a[$key];
        }
        $a = $result;

        return;
    }
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

    /**
     * Trim all elemements in an array.
     * The array has to be a 1-dimensional array.
     *
     * @param array $arr
     * @param string $character_mask
     * @return array
     */
    public static function trimArray(array $arr, $character_mask) {
        foreach ($arr as $key => $item) {
            $arr[$key] = trim($item, $character_mask);
        }
        return $arr;
    }
66
}