gpt4 book ai didi

PHP - 像 WHERE 子句一样的 foreach 循环

转载 作者:搜寻专家 更新时间:2023-10-31 20:39:48 25 4
gpt4 key购买 nike

我一直在努力弄清楚这个问题,但就是想不通。

我有这些正在使用的 xml 对象:

SimpleXMLElement Object
(
[@attributes] => Array
(
[product_category_id] => 13463
[name] => A Athletic Wear
[path] => A Athletic Wear
[thumburl_front] => REPLACE_DOMAIN_WITH/images/publishers/440/ProductCategories/A_Athletic_Wear/80.gif
)

)
SimpleXMLElement Object
(
[@attributes] => Array
(
[product_category_id] => 13475
[name] => A Accessories
[path] => A Accessories
[thumburl_front] => REPLACE_DOMAIN_WITH/images/publishers/440/ProductCategories/A_Accessories/80.gif
)

)
SimpleXMLElement Object
(
[@attributes] => Array
(
[product_category_id] => 13482
[parent_id] => 13463
[name] => Crewneck Sweatshirts
[path] => A Athletic Wear -> Crewneck Sweatshirts
[thumburl_front] => REPLACE_DOMAIN_WITH/images/publishers/440/ProductCategories/A_Athletic_Wear/Crewneck_Sweatshirts/80.gif
)

)
SimpleXMLElement Object
(
[@attributes] => Array
(
[product_category_id] => 13483
[parent_id] => 13475
[name] => Aprons
[path] => A Accessories -> Aprons
[thumburl_front] => REPLACE_DOMAIN_WITH/images/publishers/440/ProductCategories/A_Accessories/Aprons/80.gif
)

)

要使用什么样的循环才能使最终标记看起来像这样:

<ul>
<li>A Athletic Wear</li>
<ul>
<li>Crewneck Sweatshirt</li>
</ul>
<li>A Accessories</li>
<ul>
<li>Aprons</li>
</ul>
</ul>

这是我的功能:

function GetCatalogyList() {
global $url, $get_product_catalog;

$xml = simplexml_load_file($url . $get_product_catalog . "");

foreach ($xml as $items) {
$pcid = $items["product_category_id"];
$pid = $items["parent_id"];
$name = $items["name"];

// Logic to iterate through the xml so that product_category_id elements
// appears under the parent_id element using unordered list.

}

return $return;

}

如果这是 SQL,我会简单地使用 WERE 子句并让其余的继续进行。但这是我以前没有遇到过的,需要帮助。谢谢。

最佳答案

您必须意识到的第一件事是您正在处理数组,而不是数据库。 SQL 只是将结果集作为数组返回,然后您只需按照需要的方式对其进行解析即可。

至于你的功能,它有几个问题:你tightly-couple数据加载、解析和呈现(看起来如此),这只会让函数体更难阅读。

为了解决这样的问题,您需要将任务分解成小任务。以下是需要完成的工作:

  1. 从外部资源(XML、JSON 或其他)加载数据
  2. 解析该数据
  3. 呈现解析后的数据

所以在你的情况下,它看起来像这样:

$url = 'http://stores.inksoft.com/GetProductCategoryList/1210/';

$data = parse_data(simplexml_load_file($url));

echo render_as_dropdown($data);

函数看起来像这样

function parse_data($array) {

// We're going to keep relationships here
$data = array(
'items' => array(),
'parents' => array()
);

foreach ($array as $node) {

// First step - Make those XML attributes easier to read
$attributes = (array) $node->attributes();
$attributes = $attributes['@attributes'];


$data['items'][$attributes['product_category_id']] = $attributes;

// When we have no parents, let's make it null to avoid conflicts
if (!isset($attributes['parent_id'])) {
$attributes['parent_id'] = null;
}

$data['parents'][$attributes['parent_id']][] = $attributes['product_category_id'];
}

return $data;
}


function render_as_dropdown($data, $parentId = null) {
$html = '';

if (isset($data['parents'][$parentId])) {

$html = '<ul>';

foreach ($data['parents'][$parentId] as $itemId) {

$html .= '<li><a href="#">' . $data['items'][$itemId]['name'] . '</a>';

// find childitems recursively
$html .= render_as_dropdown($data, $itemId);
$html .= '</li>';
}

$html .= '</ul>';
}

return $html;
}

关于PHP - 像 WHERE 子句一样的 foreach 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25376295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com