PHP code example of hideto-d-kurt / arr-to-arr

1. Go to this page and download the library: Download hideto-d-kurt/arr-to-arr library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hideto-d-kurt / arr-to-arr example snippets


 
rToArr\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);

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;
    }
}

 
rToArr\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]);
}

$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);

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] => 
        )

)

 * PHP >= 7.1.0