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
1
vote
1 answer

array_push to a multidimensional array if array_key_exists

I am creating a new array ($Parts) from an existing array ($newarray), and reordering the array. However if the array key exists in the new array, I want to append to the 'location' and 'qty' arrays. Here is what the new array structure looks…
Matthew Colley
  • 10,816
  • 9
  • 43
  • 63
1
vote
0 answers

When I retrieve my ArrayCollection the keys are missing

I'm working on Symfony2, using Doctrine2. My problem is that when I save an array collection with keys, the moment I retrieve them, the keys are the usual 0 to n array index keys, not the ones I put in. This is my entity: /** * @var array *…
Joseph
  • 11
  • 4
1
vote
3 answers

Make a unique list of values from a particular key existing anywhere in a deep array

I have an array that consists of an undetermined number of arrays, recursively (n levels deep). Each array might contain a name key. I want to create a unique list of those values. Example Suppose the array is: $bigArray = array( 'name'=>'one',…
1
vote
2 answers

Return array key and value from ordered number

I want to return both the key and value of an array item, from only knowing their numerically ordered number. Is there a better method than using these two functions? $num = '3'; $array = [ 'fish' => 'blue', 'monkey' => 'green', …
ditto
  • 5,917
  • 10
  • 51
  • 88
1
vote
3 answers

Does array element has a key

How can I tell if array element, in foreach loop, has a key? Some code: function test($p_arr){ foreach ($p_arr as $key => $value){ // detect here if key 'came with the array' or not } } $arr1['a'] = 10; $arr2[] = 10; $arr3[2] =…
yossi
  • 3,090
  • 7
  • 45
  • 65
1
vote
1 answer

How to create new array key into an existing associative array's internal key?

I've following two arrays as follows: $test_array = array('frequencyCapping','images','sizes'); $soap_arr = array( 'lineItemId' => '', 'creativeData' => array( 'name' => '', 'adType' => '', …
PHPLover
  • 1
  • 51
  • 158
  • 311
1
vote
3 answers

PHP foreach loop with a array_key conditional for filtering array

This might be a painfully obvious question, but here it is anyway... I have a loop to create an filtered results from a multidimensional array, foreach ($myArray as $k => $v) { if (array_keys($v, 'today')) { $todayArray[$k] = $v; …
Jim Doodle
  • 569
  • 1
  • 6
  • 7
1
vote
2 answers

How can I get the keys from a dynamic multidimensional array, in PHP?

How could I get the keys from a dynamic multidimensional arrays those I randomized them using Shuffle function? Assume that I have this script: function customShuffle(array &$array) { $firstElement = array_shift($array); …
MRAN
  • 127
  • 2
  • 12
1
vote
1 answer

Is array_key() the fastest solution for this?

I'm writing an application for my research which deals with very big arrays and lots of iterations on their values. For reducing the calculation time, I'm implementing several improvements in the codes. Right now, after several improvements, the…
SAVAFA
  • 818
  • 8
  • 23
1
vote
2 answers

multidimensional array, transpositional values and merged result

I have and array $aArg with these values array ( 0 => 'reviewed_entity', 1 => 'kiosk', ) array ( 0 => 'tripadvisor', 1 => 'tripadvisor2', ) array ( 0 => 'google', 1 => 'google2', ) array ( 0 => 'yahoo', 1 => 'yahoo2', ) I need to make it appear as…
TimFam
  • 13
  • 2
1
vote
1 answer

php get matching keys between 2 arrays

What is the best way to get the matching keys between two associative arrays: Array ( [array_1] => Array ( [abc] => 111 [def] => 222 ), [array_2] => Array ( [ghi] => 995 [jkl] => 996 [mno] => 997 …
Marty Wallace
  • 34,046
  • 53
  • 137
  • 200
0
votes
0 answers

Warning: Undefined array key 1

It only started giving me problems when i upgraded to php 8.1. The issue is on the line $time = $matches Although, this is a very generic question and probably has been answered quite a few time something similar. I looked through some of the other…
Singleton
  • 85
  • 2
  • 5
  • 16
0
votes
0 answers

How to rename array keys to a string value in JavaScript

I have an array that conatins a list of objects, then am mapping through the object to display a list of data. Right now when user add item to the list , I want to change the index of that newly added Item to a string value. What am getting is this …
Ahmad Salihu
  • 55
  • 1
  • 6
0
votes
0 answers

Unidentified Array Key in $_GET PHP

I'm trying to create a CRUD system in PHP linked with XML that contains all the info that I want to create a CRUD system with. I'm stuck with editing the basic infos because in this code:
0
votes
1 answer

Weird problem with map thru an array - key/id not recognized

I have a small array of data in a separate file. Each item of the array has an id of a unique number. In another file I declared props for the component that I want to rendex and the overall "design" of it with (

{props.title) and a few more. I…

user11924794