[Solved] How to make own array_keys function in php?

  

2
Topic starter

How can I create my own array_keys function?

Like the main one in Php (with examples): https://www.php.net/manual/en/function.array-keys.php

10x

1 Answer
2

Here is an example of my own generated php array_keys function:

<?php
 
$int1  = [ 1, 2, 3 ];
$names2 = [ 'Ivanov', 'Petrov' ];
$names3 = [ 'Ivan' => 'Ivanov', 'Petar' => 'Petrov' ];
 
function my_array_keys( $arr ) {
    $data = [ ];
    foreach ( $arr as $k => $v ) {
        $data[] = $k;
    }
 
    return $data;
}
 
$keys1 = my_array_keys( $int1 );
$keys2 = my_array_keys( $names2 );
$keys3 = my_array_keys( $names3 );
 
echo '<pre>';
print_r( $keys1 );
echo '</pre>';
 
echo '<pre>';
print_r( $keys2 );
echo '</pre>';
 
echo '<pre>';
print_r( $keys3 );
echo '</pre>';

Note: array_keys() returns the keys, numeric and string, from the array.

Share: