{"success":true,"name":"use-php-array-built-ins-before-writing-manual-foreach-loops","description":"About to write a PHP loop that transforms, filters, or searches an array","category":"ai-brimind","path":"ai-brimind/use-php-array-built-ins-before-writing-manual-foreach-loops/SKILL.md","file":"SKILL.md","content":"---\nname: use-php-array-built-ins-before-writing-manual-foreach-loops\ndescription: \"About to write a PHP loop that transforms, filters, or searches an array\"\n---\n\n# Use PHP Array Built-ins Before Writing Manual foreach Loops\n\n## When to use\nProject is PHP, and you are about to hand-write a foreach that maps, filters, sums, searches, or reindexes array data.\n\n## Example request\nLoop building a new array of transformed items → array_map is the name — mapping. Loop with if pushing matches → array_filter owns it — filtering. Loop summing a field → array_sum + array_column in one line — aggregation. Loop hunting one item → array_search / in_array / array_find are built — searching.\n\n## Instructions\nYou are a PHP developer who names the array operation before writing the loop. When facing array work, classify it first: transform each item (array_map), keep some (array_filter), reduce to one value (array_sum, array_reduce), extract a field (array_column), search (in_array, array_search, array_key_exists), reindex (array_values, array_combine), or sort (usort, uasort — knowing sort mutates in place). Reach for the built-in matching the name; a manual foreach is justified only when the logic genuinely mixes concerns, needs early exit the built-ins lack, or readability truly suffers — and you must say which. Watch the known edges: array_filter without a callback drops 0 and '' too, array_map on associative arrays keeps keys but array_filter needs array_values after if sequential keys matter. Only write a manual loop once you've confirmed no built-in fits, and explain why the chosen construct states the intent that a raw loop hides.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #949\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}