求针对SimpleXMLElement对象的PHP array_column()替代实现方案
Alright, let's figure out how to replicate PHP's array_column() functionality for your specific SimpleXMLElement structure. I'll walk you through two practical implementations—one for pulling attributes directly from the row nodes, and a more flexible version that can also extract values from the nested attribute child nodes.
基础版:提取row节点的顶层属性
If you just need to grab values from the @attributes of each row (like codeonimage, name, or oem), this straightforward function will do the trick:
function xml_array_column(SimpleXMLElement $xml, string $targetAttribute) { $result = []; // Loop through each <row> element in the XML object foreach ($xml->row as $row) { // Get all attributes for the current row $rowAttributes = $row->attributes(); // Check if the target attribute exists, then cast to string (to get the actual value) $result[] = isset($rowAttributes[$targetAttribute]) ? (string)$rowAttributes[$targetAttribute] : null; // Fallback to null if the attribute is missing } return $result; }
使用示例
// Extract all 'name' values from the rows $itemNames = xml_array_column($yourXmlObject, 'name'); // Output: ['Крышка', ... (other row names)] // Extract all 'oem' codes $oemCodes = xml_array_column($yourXmlObject, 'oem'); // Output: ['13711251885', ...]
进阶版:支持提取嵌套attribute节点的值
If you need to dig into the nested attribute child nodes (like grabbing the name or value of an attribute where key="amount"), this extended function handles both top-level attributes and nested ones:
function xml_array_column(SimpleXMLElement $xml, string $target, string $nestedField = null) { $result = []; foreach ($xml->row as $row) { if ($nestedField === null) { // Case 1: Pull top-level row attribute $rowAttributes = $row->attributes(); $result[] = isset($rowAttributes[$target]) ? (string)$rowAttributes[$target] : null; } else { // Case 2: Pull value from nested <attribute> where key matches $target $matchedValue = null; foreach ($row->attribute as $attribute) { $attrAttributes = $attribute->attributes(); if ((string)$attrAttributes['key'] === $target) { $matchedValue = isset($attrAttributes[$nestedField]) ? (string)$attrAttributes[$nestedField] : null; break; // Stop at first match—remove this if you want all matching attributes } } $result[] = $matchedValue; } } return $result; }
使用示例
// Extract the 'name' field from all <attribute> nodes where key="amount" $amountLabels = xml_array_column($yourXmlObject, 'amount', 'name'); // Still works for top-level attributes too! $ssdValues = xml_array_column($yourXmlObject, 'ssd');
关键注意事项
- Casting to string: Always use
(string)when accessing SimpleXMLElement attributes/values—otherwise you'll get a SimpleXMLElement object instead of the raw text. - Single vs multiple rows: The function works even if there's only one
row(SimpleXMLElement handles single nodes in foreach loops seamlessly). - Fallback behavior: The function uses
nullas a fallback for missing values, but you can adjust this to an empty string or another default if needed.
内容的提问来源于stack exchange,提问作者SlowSuperman

