OnArray.php 1.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 1/10/16
 * Time: 11:47 AM
 */


10
namespace qfq;
11
12
13

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

Carsten  Rose's avatar
Carsten Rose committed
14
15
/**
 * Class OnArray
16
 * @package qfq
Carsten  Rose's avatar
Carsten Rose committed
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
51
52
53
54
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;
    }
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

    /**
     * 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;
    }
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

    /**
     * Iterates over all records and return those with $row[$column]==$value
     *
     * @param array $dataArray
     * @param $column
     * @param $value
     * @return array
     */
    public static function filter(array $dataArray, $column, $value) {
        $result = array();

        foreach ($dataArray as $row) {
            if (isset($row[$column]) && $row[$column] == $value) {
                $result[] = $row;
            }
        }
        return $result;
    }
89
}