Questions tagged [array-key]

array-key refers to key portion of array holding key and value pair. In PHP array_keys() returns the keys, numeric and string, from the input array.

array-key() refers to key portion of array holding key and value pair. In PHP array_keys() returns the keys, numeric and string, from the input array.

array array_keys ( array $array [, mixed $search_value [, bool $strict = FALSE ]] )

array_keys() — Return all the keys or a subset of the keys of an array

Example:

$array = array(0 => 100, "color" => "red");
print_r(array_keys($array));

$array = array("blue", "red", "green", "blue", "blue");
print_r(array_keys($array, "blue"));

Result:

Array
(
    [0] => 0
    [1] => color
)

Array
(
    [0] => 0
    [1] => 3
    [2] => 4
)

Reference

155 questions
2
votes
3 answers

Search in a 2D array... php

I have this array: $Fruit = array() $Fruit[$species][$property] = $value Array ( [Apple] => Array ( [Green] => 4 [Spots] => 3 [Red] => 3 [Spots] => 2 ) Now I want to search if a…
Thijs
  • 387
  • 5
  • 20
2
votes
2 answers

How to get the name of a key in array

This sounds quite simple, but i can't make it work. I'm trying to group keys with same value. I can get the key number but i cant get the name of the Key. i.e "London, Berlin". This is my code: $countries = array ( 'London' => 'Europe/London', …
Avel
  • 111
  • 1
  • 2
  • 9
2
votes
1 answer

php safe strings to use in a multi-dimensional array index/key

I want to use strings that come from parsing an external XML document as indices in an array. Since I do not know what kind of strings are held in that document - are there any checks or cleansing processes I should use to make sure there is never a…
Joseph Cape
  • 383
  • 1
  • 3
  • 16
2
votes
1 answer

Sorting an Array by key, numerically

I want to sort the arrays numerically, i.e. 188, 188-1, 188-2, 222, 222-1, 222,-2 etc. This is how the array currently looks: [188-1] => Array ( [time] => 1 ) [188-2] => Array ( [time] => 2 ) [188-3] => Array ( …
ditto
  • 5,917
  • 10
  • 51
  • 88
2
votes
6 answers

php explode and force array keys to start from 1 and not 0

I have a string that will be exploded to get an array, and as we know, the output array key will start from 0 as the key to the first element, 1 for the 2nd and so on. Now how to force that array to start from 1 and not 0? It's very simple for a…
medk
  • 9,233
  • 18
  • 57
  • 79
2
votes
2 answers

array_keys or in_array insensitive doesnt work correctly

i have a problem with insensitive array_keys and in_array ... I developing a translator, and i have something like this: $wordsExample = array("example1","example2","example3","August","example4"); $translateExample =…
Stefan Luv
  • 1,179
  • 3
  • 12
  • 28
2
votes
1 answer

PHP [Array Keys] : Convert "0" Into "A", "1" Into "B"... (and so "89" will be "IJ") Function

Let's say I have an array like this: $array = array ("this","is","me","and","I","am","an","array","hello","beautiful","world"); How can I create a function that converts 0 into "A", 1 into "B"...? Using Foreach or...? So that instead of Array ( …
user1282226
1
vote
3 answers

Is it ok to have numerical keys in a PHP array but not contiguous from 0?

Is it ok to build an array with numerical keys but assign items to arbitrary key numbers? i.e. $test = array(23=>'first thing', 245=>'second thing');
dibs
  • 1,018
  • 2
  • 23
  • 35
1
vote
3 answers

utf8_encode does not produce right result

My problem is the following: I store an array, which has keys like "e", "f", etc. At some point, I have to get the value of the key. This works well. But if I want to store "í", "é", etc. as the keys, it won't produce the right result (results in…
axiomer
  • 2,088
  • 1
  • 17
  • 26
1
vote
1 answer

array size > 0 although no key is set

short question. given the following example: $arr = array(); $arr[0] = false ?: NULL; var_dump($arr[0]); var_dump($arr[1]); var_dump(isset($arr[0])); var_dump(isset($arr[1])); var_dump(count($arr)); the resulting output is: NULL NULL bool(false)…
Florian Kernler
  • 149
  • 1
  • 9
1
vote
1 answer

Undefined array key "REQUEST_URI" php warning - MVC does not return query string

I am creating an MVC framework with PHP. I am not able to get the query string. The following code does not return the query string index.php
1
vote
1 answer

Generating a php array from mysql and want to use ID from db as array keys

The current output is.. Array ( [0] => Array ( [ID] => 20 [name] => Peter [datetime] => Tuesday 26th Oct 21 3:50am ) [1] => Array ( [ID] => 21 [name] =>…
Glen Keybit
  • 296
  • 2
  • 15
1
vote
1 answer

Recursively extract key paths from array - excluding numeric keys

In php, I need a function to return all keys in array of objects. For arrays and objects I need keys like parentkey->key. If the key is again an array, then I need parentkey->key->key1. My code as…
1
vote
4 answers

Give a values for array keys PHP

$columns = array( 'title', 'client', 'date', 'product', 'status' ); This is print: Array ( [0] => title [1] => client [2] => date [3] => product [4] => status ) I want to give values for the $columns array to be like…
John
  • 161
  • 1
  • 13
1
vote
1 answer

Accessing an element in an array by key

Today in a code audit, it was pointed out, that accessing a variable element like $array['keyname'] is not an optimum way to do it and instead a constant should be defined and then used as below. define('KEYNAME', 'keyname'); // Constant defined…
Mohd Abdul Mujib
  • 13,071
  • 8
  • 64
  • 88
1 2
3
10 11