0
0
PHPprogramming~10 mins

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

Choose your learning style9 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.