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
0
votes
0 answers

Update jsonb array one element without key using jsonb_set in postgresql

I have table audits with jsonb field data and it has content: "products": [ {"id": 405, "color": null, "price": 850, "title": "test", "value": 52, "real_value": 105 }, {"id": 347, "color": null, "price": 195, "title": "test2", "value": 69,…
0
votes
5 answers

Finding the next array row with PHP

I'm working in a code that find the next array value based on the current value but still always returning 1 as result. $users_emails = array('Spence', 'Matt', 'Marc', 'Adam', 'Paul'); $current = 'Spence'; $keys =…
John Wiky
  • 19
  • 5
0
votes
1 answer

Match two array associates keys and replace reference array key with 1st array

Match 1st and 2nd array associates keys. And replace 1st array associates keys with 2nd array value. Information like below. 1st Array [ [ ], [ ], { 585: { firsthalf: "0", secondhalf:…
Amila Priyankara
  • 118
  • 1
  • 12
0
votes
1 answer

Why I'm getting parse error in accessing an array value present under the key of string type?

I have tried following code : "purple"); // For below line of code I get tis error : Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier…
user9009323
0
votes
1 answer

PHP Array only get those elements/values whose keys match a particular pattern

I have a multi-dimensional & nested array like below: Array ( [_edit_lock] => Array ( [0] => 1504299434:6 ) [_edit_last] => Array ( [0] => 6 ) [additional_photos_0_gallery_image_1]…
VST
  • 131
  • 1
  • 10
0
votes
4 answers

Get array key to remove an array based on that array object

New question, so this is the array [ 0: { "id" : "3" "name": "David", "age": "20" }, 1: { "id" : "6" "name": "", "age": "18" }, 2: { "id" : "8" "name": "Micheal", …
Alan Yong
  • 993
  • 2
  • 12
  • 25
0
votes
0 answers

Why the output is getting changed of an array resulted from API calling?

Suppose I have an API which is resulting an array of objects. like [{ "cipcode": "03.0104", "major": "Environmental Science" }, { "cipcode": "03.0201", "major": "Natural Resources Management and Policy" }, { "cipcode":…
SNishant
  • 103
  • 11
0
votes
2 answers

How to remove the unwanted nested keys from JSON

This is my json: { "all_counts_reports":{ "26":{ "name":"kumar", "date":"2017-04-27", "trips_per_day":"2", "cash_trips":"0", "credit_trips":"1", "compliment_trips":"1" }, …
0
votes
1 answer

Nested foreach loop on supposed copy of array changes internal pointer of original array

When using PHP 5.5.9 and given this example test class: class Test implements Iterator { private $ar = [ 1, 2, 3 ]; public function toArray() { return $this->ar; } public function rewind() { reset( $this->ar ); } public…
Decent Dabbler
  • 22,532
  • 8
  • 74
  • 106
0
votes
1 answer

php perform isset in if statement without grouped statement

I have this if statement if ($something !== $array[$key]) { // do something } But I can't be 100% sure that $array[$key] exists. So I'd like to do something like this: if ($something !== $array[$key] ?? null) { // do something } The if…
user936965
0
votes
1 answer

PHP filling the associative array with the right key

$niz = array( 'fruit1' => 'apple', 'fruit2' => 'orange', 'fruit3' => 'grape', 'fruit4' => 'watermelon', 'fruit5' => 'grapefruit' ); $max = 'yellow'; $niz2 = array(); $niz3 = array(); …
lukpar
  • 27
  • 5
0
votes
0 answers

How to count similar array keys using PHP?

foreach ($data as $key => $value) { $count = count(preg_grep("/^options(\d)+$/", array_keys(get_object_vars($value)))); echo $count; exit; echo $count is giving 0. If I do print_r($value);exit;, the $value variable is giving this…
Ramkishan Suthar
  • 403
  • 1
  • 7
  • 26
0
votes
2 answers

how to use in foreach for array_keys

i need to get data from array_keys the script i use in the server side: PHP: $friends = json_decode(file_get_contents( 'https://graph.facebook.com/me/friends?access_token=' . $facebook->getAccessToken() ), true); $friend_ids =…
ROI
  • 103
  • 3
  • 15
0
votes
1 answer

is it safe to use a email id as a key for array in php

I am trying to do the below in php array( 'email@domain.com' => 'something', ... ); I did read that php accepts and number or valid string as key. is it safe to have email id as key in the array as above ? by safe I mean is possible that this…
0
votes
1 answer

How to find parent array key in multidimensional array php

I've tried to adjust many similar solutions that I've found here on stack by none worked for me. Could someone please help me? This multidimensional array is dynamically generated (contains 55 keys total). There is variable $age which users enter,…
cron15
  • 1
  • 1