PHP Arrays and Data Manipulation Flashcards
(25 cards)
What is an array in PHP?
An array is a data structure that stores multiple values in a single variable, like $arr = [1, 2, 3];.
Arrays are used in WordPress for post data, settings, or menus. Freelancers manipulate arrays for client features like dynamic lists, while enterprise architects use them for structured data in scalable systems.
What is an indexed array in PHP?
An indexed array uses numeric keys, like $fruits = [“apple”, “banana”];.
In WordPress, indexed arrays store sequential data (e.g., post IDs). Freelancers use them for simple lists, while enterprise architects ensure efficient iteration for large datasets.
What is an associative array in PHP?
An associative array uses string keys, like $user = [“name” => “John”, “age” => 30];.
Common in WordPress for settings (e.g., $args in WP_Query). Freelancers use them for key-value data, while enterprise architects leverage them for structured configurations.
What is a multi-dimensional array in PHP?
A multi-dimensional array contains arrays, like $matrix = [[1, 2], [3, 4]];.
In WordPress, they store complex data (e.g., nested menu items). Freelancers use them for hierarchical content, while enterprise architects optimize access for performance.
How do you create an array in PHP?
Use square brackets or array(), like $arr = [1, 2] or $arr = array(1, 2);.
In WordPress, arrays are created for post queries or plugin options. Freelancers build arrays for client needs, while enterprise architects ensure consistent array initialization.
What is the count() function in PHP?
count() returns the number of elements in an array, like count([1, 2, 3]) returns 3.
Used in WordPress to count posts or items. Freelancers apply it in loops, while enterprise architects optimize it for large arrays in high-traffic systems.
What is the array_push() function?
array_push() adds elements to the end of an array, like array_push($arr, 4);.
In WordPress, it appends data (e.g., adding a post ID). Freelancers use it for dynamic lists, while enterprise architects prefer [] assignment for efficiency.
What is the array_pop() function?
array_pop() removes and returns the last element, like $last = array_pop($arr);.
In WordPress, it’s used to extract recent data (e.g., last comment). Freelancers manage arrays dynamically, while enterprise architects ensure safe removal in data pipelines.
What is the array_shift() function?
array_shift() removes and returns the first element, like $first = array_shift($arr);.
In WordPress, it processes queue-like data (e.g., first post). Freelancers use it for sequential tasks, while enterprise architects manage array reindexing.
What is the array_unshift() function?
array_unshift() adds elements to the start of an array, like array_unshift($arr, 0);.
In WordPress, it prepends data (e.g., menu items). Freelancers use it for prioritized content, while enterprise architects ensure efficient array modification.
What is the array_merge() function?
array_merge() combines multiple arrays, like $new = array_merge($arr1, $arr2);.
As you’ve seen in prior decks, it’s used in WordPress for plugin settings. Freelancers merge client options, while enterprise architects avoid key conflicts in large datasets.
What is the array_keys() function?
array_keys() returns an array of keys, like array_keys([“name” => “John”, “age” => 30]) returns [“name”, “age”].
In WordPress, it extracts setting keys. Freelancers use it for data validation, while enterprise architects process metadata efficiently.
What is the array_values() function?
array_values() returns an array of values, like array_values([“name” => “John”]) returns [“John”].
In WordPress, it reindexes arrays (e.g., post data). Freelancers use it for normalized data, while enterprise architects ensure consistent array structures.
What is the array_map() function?
array_map() applies a callback to each element, like array_map(‘strtoupper’, $arr).
In WordPress, it transforms data (e.g., capitalizing titles). Freelancers use it for bulk operations, while enterprise architects optimize callbacks for performance.
What is the array_filter() function?
array_filter() removes elements based on a callback, like array_filter($arr, fn($x) => $x > 0).
In WordPress, it filters posts (e.g., active products). Freelancers use it for selective data, while enterprise architects ensure efficient filtering for large arrays.
What is the array_reduce() function?
array_reduce() reduces an array to a single value, like array_reduce($arr, fn($carry, $x) => $carry + $x, 0).
In WordPress, it sums data (e.g., cart totals). Freelancers use it for calculations, while enterprise architects optimize reductions for complex datasets.
What is the sort() function in PHP?
sort() sorts an array in ascending order, like sort($arr);.
In WordPress, it orders posts or items. Freelancers use it for sorted displays, while enterprise architects ensure stable sorting for large arrays.
What is the rsort() function?
rsort() sorts an array in descending order, like rsort($arr);.
Given your reverseSeq experience, it’s used in WordPress for reverse order (e.g., recent posts). Freelancers apply it for client needs, while enterprise architects manage sorting efficiency.
What is the asort() function?
asort() sorts an associative array by value, preserving keys, like asort($arr);.
In WordPress, it sorts settings. Freelancers use it for ordered data, while enterprise architects maintain key associations in configurations.
What is the ksort() function?
ksort() sorts an associative array by key, like ksort($arr);.
In WordPress, it organizes metadata. Freelancers use it for structured outputs, while enterprise architects ensure consistent key ordering.
What is the array_slice() function?
array_slice() extracts a portion of an array, like array_slice($arr, 1, 3);.
In WordPress, it paginates posts. Freelancers use it for subset data, while enterprise architects optimize slicing for large datasets.
What is the array_splice() function?
array_splice() removes and replaces elements, like array_splice($arr, 1, 2, [“new”]);.
In WordPress, it modifies lists (e.g., menu items). Freelancers use it for dynamic updates, while enterprise architects ensure safe array manipulation.
What is the in_array() function?
in_array() checks if a value exists in an array, like in_array(“apple”, $fruits).
In WordPress, it validates data (e.g., checking post types). Freelancers use it for logic, while enterprise architects ensure efficient searches.
What is the array_key_exists() function?
array_key_exists() checks if a key exists, like array_key_exists(“name”, $user).
In WordPress, it verifies settings keys. Freelancers use it for safe access, while enterprise architects prevent undefined key errors.