PHP code example of pointybeard / helpers-functions-arrays

1. Go to this page and download the library: Download pointybeard/helpers-functions-arrays 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/ */

    

pointybeard / helpers-functions-arrays example snippets




ointybeard\Helpers\Functions\Arrays;

var_dump(Arrays\array_is_assoc(['a' => 1, 'b' => 2]));
// bool(true)

var_dump(Arrays\array_is_assoc([4, 5, 6, 7]));
// bool(false)

$a = [1, 2, 3, 4];
Arrays\array_insert_at_index($a, 2, "apple", "banana", "orange");
print_r($a);
// Array
// (
//     [0] => 1
//     [1] => 2
//     [2] => apple
//     [3] => banana
//     [4] => orange
//     [5] => 3
//     [6] => 4
// )

$a = [1, 3, 'animal' => 'chicken', 1, 2, 3, 4];
Arrays\array_insert_at_index($a, 4, ['food' => 'cabbage']);
print_r($a);
// Note that array key 'food' is not preserved
// Array
// (
//     [0] => 1
//     [1] => 3
//     [animal] => chicken
//     [2] => 1
//     [3] => cabbage
//     [4] => 2
//     [5] => 3
//     [6] => 4
// )

print_r(Arrays\array_remove_empty([
    1, 2, 3, 4, '', ['a', 'b', 'c', '', 'e']
]));
// Array
// (
//   [0] => 1
//   [1] => 2
//   [2] => 3
//   [3] => 4
//   [5] => Array
//     (
//       [0] => a
//       [1] => b
//       [2] => c
//       [4] => e
//     )
// )

print_r(Arrays\array_remove_empty([
    "fruit" => [
        "apple",
        "banana",
        ""
    ],
    "cars" => [],
    "ancestors" => [
        "charlie" => "",
        "betty" => [
            "children" => [
                "pete",
                "sarah" => [
                    "children" => [
                        1 => "heidi",
                        2 => "mary",
                        3 => "adam",
                        4 => ""
                    ]
                ],
                "bob" => [
                    "children" => []
                ]
            ]
        ]
    ]
], 3));
// Array
// (
//   [fruit] => Array
//     (
//       [0] => apple
//       [1] => banana
//     )
//   [ancestors] => Array
//     (
//       [betty] => Array
//         (
//           [children] => Array
//             (
//               [0] => pete
//               [sarah] => Array
//                 (
//                   [children] => Array
//                     (
//                       [1] => heidi
//                       [2] => mary
//                       [3] => adam
//                     )
//                 )
//               [bob] => Array
//                 (
//                   [children] => Array
//                     (
//                     )
//                 )
//             )
//         )
//     )
// )

var_dump(Arrays\array_remove_empty([
    "", NULL, false
]));
// array(0) {}

try {
    print_r(Arrays\array_remove_empty([
        "one", "two"
    ], "INVALID_DEPTH_VALUE"));
} catch (\Exception $ex) {
    var_dump($ex->getMessage());
}
// string(46) "depth must be NULL or a positive integer value"