Bird
Raised Fist0
PHPprogramming~10 mins

Array key and value extraction in PHP - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Array key and value extraction
Start with array
↓
Extract keys
→Store keys in new array
↓
Extract values
→Store values in new array
↓
Use keys and values separately
We start with an array, then extract its keys and values into separate arrays to use them individually.
Execution Sample
PHP
<?php
$array = ['a' => 1, 'b' => 2, 'c' => 3];
$keys = array_keys($array);
$values = array_values($array);
print_r($keys);
print_r($values);
?>
This code extracts keys and values from an associative array and prints them separately.
Execution Table
StepActionArray StateResultOutput
1Define array['a' => 1, 'b' => 2, 'c' => 3]Array created
2Extract keys['a' => 1, 'b' => 2, 'c' => 3]['a', 'b', 'c']
3Extract values['a' => 1, 'b' => 2, 'c' => 3][1, 2, 3]
4Print keyskeys = ['a', 'b', 'c']keys arrayArray ( [0] => a [1] => b [2] => c )
5Print valuesvalues = [1, 2, 3]values arrayArray ( [0] => 1 [1] => 2 [2] => 3 )
💡 All keys and values extracted and printed.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
$arrayundefined['a' => 1, 'b' => 2, 'c' => 3]['a' => 1, 'b' => 2, 'c' => 3]['a' => 1, 'b' => 2, 'c' => 3]
$keysundefined['a', 'b', 'c']['a', 'b', 'c']['a', 'b', 'c']
$valuesundefinedundefined[1, 2, 3][1, 2, 3]
Key Moments - 2 Insights
Why do keys become a simple list of strings instead of keeping their original association?
Because array_keys() returns only the keys as a plain indexed array, losing the original key-value association, as shown in step 2 of the execution_table.
Why do values become a zero-based indexed array instead of keeping their original keys?
Because array_values() returns only the values as a plain indexed array starting at 0, ignoring original keys, as seen in step 3 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the content of $keys?
A['a' => 1, 'b' => 2, 'c' => 3]
B['1', '2', '3']
C['a', 'b', 'c']
D[1, 2, 3]
💡 Hint
Check the 'Result' column at step 2 in execution_table.
At which step does $values get assigned its array?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at variable_tracker for $values and see when it changes from undefined.
If the original array had numeric keys, how would array_keys() output look?
AAn array of values
BAn array of those numeric keys
CAn associative array with keys and values swapped
DAn empty array
💡 Hint
array_keys() always returns the keys as a simple indexed array, regardless of key type.
Concept Snapshot
PHP arrays have keys and values.
Use array_keys() to get all keys as a simple list.
Use array_values() to get all values as a simple list.
Both functions return indexed arrays starting at 0.
Useful to separate keys and values for processing.
Full Transcript
We start with an associative array with keys 'a', 'b', 'c' and values 1, 2, 3. Using array_keys(), we extract the keys into a new indexed array ['a', 'b', 'c']. Using array_values(), we extract the values into a new indexed array [1, 2, 3]. We print both arrays to see the separated keys and values. This helps when you want to work with keys and values separately in PHP.

Practice

(1/5)
1. Which PHP function is used to extract all the keys from an array?
easy
A. array_map()
B. array_values()
C. array_keys()
D. array_filter()

Solution

  1. Step 1: Understand the function purpose

    array_keys() returns all keys from an array.
  2. Step 2: Compare with other functions

    array_values() returns values, not keys; others do different tasks.
  3. Final Answer:

    array_keys() -> Option C
  4. Quick Check:

    Keys extraction = array_keys() [OK]
Hint: Keys come from array_keys(), values from array_values() [OK]
Common Mistakes:
  • Confusing array_keys() with array_values()
  • Using array_map() to get keys
  • Trying to get keys with array_filter()
2. Which of the following is the correct syntax to get all values from an array $arr in PHP?
easy
A. array_values($arr);
B. array_keys($arr);
C. values_array($arr);
D. get_values($arr);

Solution

  1. Step 1: Identify the correct function name

    The function to get values is array_values().
  2. Step 2: Check syntax correctness

    Correct syntax is array_values($arr); with parentheses and variable inside.
  3. Final Answer:

    array_values($arr); -> Option A
  4. Quick Check:

    Values extraction = array_values() [OK]
Hint: Values come from array_values(), keys from array_keys() [OK]
Common Mistakes:
  • Using array_keys() to get values
  • Using non-existent functions like values_array()
  • Missing parentheses in function call
3. What will be the output of the following PHP code?
$arr = ['a' => 1, 'b' => 2, 'c' => 3];
print_r(array_keys($arr));
medium
A. Array ( [0] => 1 [1] => 2 [2] => 3 )
B. Array ( [0] => 'a' [1] => 'b' [2] => 'c' )
C. Array ( [a] => 1 [b] => 2 [c] => 3 )
D. Array ( [0] => a [1] => b [2] => c )

Solution

  1. Step 1: Understand array_keys() output

    It returns an indexed array of all keys from the input array.
  2. Step 2: Analyze given array keys

    Keys are strings 'a', 'b', 'c', so output is an indexed array with these strings.
  3. Final Answer:

    Array ( [0] => a [1] => b [2] => c ) -> Option D
  4. Quick Check:

    array_keys(['a'=>1,...]) = ['a','b','c'] [OK]
Hint: array_keys() returns keys as indexed array [OK]
Common Mistakes:
  • Confusing keys with values in output
  • Expecting associative array output
  • Including quotes in output keys
4. Identify the error in this PHP code snippet:
$arr = ['x' => 10, 'y' => 20];
$keys = array_values($arr);
print_r($keys);
medium
A. array_values() should be array_keys() to get keys
B. Missing semicolon after array declaration
C. print_r() cannot print arrays
D. Variable $keys is not defined

Solution

  1. Step 1: Understand the goal

    The code tries to get keys but uses array_values() which returns values.
  2. Step 2: Correct function usage

    To get keys, use array_keys() instead of array_values().
  3. Final Answer:

    array_values() should be array_keys() to get keys -> Option A
  4. Quick Check:

    Keys need array_keys(), not array_values() [OK]
Hint: Use array_keys() for keys, array_values() for values [OK]
Common Mistakes:
  • Mixing up array_keys() and array_values()
  • Assuming print_r() can't print arrays
  • Ignoring correct variable definitions
5. Given the array $data = ['apple' => 3, 'banana' => 0, 'cherry' => 5, 'date' => 0];, which PHP code correctly extracts only the keys with non-zero values?
hard
A. $keys = array_keys($data);
B. $keys = array_keys(array_filter($data));
C. $keys = array_values(array_filter($data));
D. $keys = array_filter(array_keys($data));

Solution

  1. Step 1: Filter array to keep non-zero values

    array_filter($data) removes entries with zero values.
  2. Step 2: Extract keys from filtered array

    array_keys() gets keys from the filtered array, giving only keys with non-zero values.
  3. Final Answer:

    $keys = array_keys(array_filter($data)); -> Option B
  4. Quick Check:

    Filter then keys = array_keys(array_filter()) [OK]
Hint: Filter first, then get keys with array_keys(array_filter()) [OK]
Common Mistakes:
  • Using array_values() instead of array_keys() after filtering
  • Not filtering before extracting keys
  • Filtering keys instead of values
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard