hideto-d-kurt/arr-to-arr

modify array to array what you want.

1.0.5 2019-08-05 09:01 UTC

This package is auto-updated.

Last update: 2024-04-05 19:42:00 UTC


README

Latest Stable Version Total Downloads License

Requirements

 * PHP >= 7.1.0

Installation

composer require hideto-d-kurt/arr-to-arr

Usage

Sort by integer value with key

Example

<?php 
require_once __DIR__ . '/../vendor/autoload.php'; // Autoload files using Composer autoload
use ArrToArr\ArrayToArray;

$arr = [
    [
        'id' => 1,
        'name' => 'a'
    ],
    [
        'id' => 3,
        'name' => 'c'
    ],
    [
        'id' => 4,
        'name' => 'd'
    ],
    [
        'id' => 2,
        'name' => 'b'
    ]
];

$new_arr = ArrayToArray::sortByKeyInteger($arr, 'id', 'ASC');
print_r($new_arr);

Result

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => a
        )

    [1] => Array
        (
            [id] => 2
            [name] => b
        )

    [2] => Array
        (
            [id] => 3
            [name] => c
        )

    [3] => Array
        (
            [id] => 4
            [name] => d
        )

)

Remove mongodb object id '_id'

Example

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
use ArrToArr\ArrayToArray;

class Users extends Eloquent 
{
    protected $collection = 'users';
    
    public static function getModel() {
        return new Users();
    }

    public static function getAllUser()
    {
        $arrToarr = new ArrayToArray();
        $result = self::get();
        $result = json_decode($result, true);
        $result = $arrToarr->removeMongoId($result);
        return $result;
    }
}

Sort by integer value with Array set

Example

<?php 
require_once __DIR__ . '/../vendor/autoload.php'; // Autoload files using Composer autoload
use ArrToArr\ArrayToArray;

$data = [
    [
        'id' => 1,
        'name' => 'a'
    ],
    [
        'id' => 5,
        'name' => 'e'
    ],
    [
        'id' => 3,
        'name' => 'c'
    ],
    [
        'id' => 2,
        'name' => 'b'
    ],
    [
        'id' => 4,
        'name' => 'd'
    ],
    [
        'id' => 6,
        'name' => 'f'
    ],
    [
        'id' => 7,
        'name' => 'g'
    ],
    [
        'id' => 8,
        'name' => 'h'
    ],
    [
        'id' => 9,
        'name' => 'i'
    ],
    [
        'id' => 10,
        'name' => 'j'
    ],
    [
        'id' => 11,
        'name' => 'k'
    ],
    [
        'id' => 12,
        'name' => 'l'
    ],
    [
        'id' => 13,
        'name' => 'm'
    ],
    [
        'id' => 14,
        'name' => 'n'
    ],
    [
        'id' => 15,
        'name' => 'o'
    ],
    [
        'id' => 16,
        'name' => 'p'
    ]
];
$arr = ArrayToArray::sortByKeyInteger($data, 'id', 'ASC');
$size_index = count($arr) - 1;
$find = 6;
$key = 'id';
$result = ArrayToArray::binarySearchArraySet($arr, 0, $size_index, $find, $key);
if(($result == -1)) {
    echo "Element is not present in array\n";
} else {
    echo "Element is present at index ".$result."\n";
    print_r($arr[$result]);
}

Result

Element is present at index 5
Array
(
    [id] => 6
    [name] => f
)

Add new key to array set

Example

$arr = [
    [
        'id' => 1,
        'name' => 'a'
    ],
    [
        'id' => 3,
        'name' => 'd'
    ],
    [
        'id' => 4,
        'name' => 'c'
    ],
    [
        'id' => 2,
        'name' => 'b'
    ]
];

$new_arr = ArrayToArray::addNewKey($arr, 'email', ['id', 'name']);
print_r($new_arr);

Result

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => a
            [email] => 
        )

    [1] => Array
        (
            [id] => 3
            [name] => d
            [email] => 
        )

    [2] => Array
        (
            [id] => 4
            [name] => c
            [email] => 
        )

    [3] => Array
        (
            [id] => 2
            [name] => b
            [email] => 
        )

)