settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: How to make own array_keys function in php?

+2 votes

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

asked in PHP category by user ak47seo

1 Answer

+1 vote

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.

answered by user eiorgert
...