PHP-foreach类似WHERE子句的循环
我一直在想这个问题,但就是想不起来 我正在处理这些xml对象:PHP-foreach类似WHERE子句的循环,php,foreach,where,Php,Foreach,Where,我一直在想这个问题,但就是想不起来 我正在处理这些xml对象: SimpleXMLElement Object ( [@attributes] => Array ( [product_category_id] => 13463 [name] => A Athletic Wear [path] => A Athletic Wear [thumburl_front] => REPLACE_DOMAI
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>
$url = 'http://stores.inksoft.com/GetProductCategoryList/1210/';
$data = parse_data(simplexml_load_file($url));
echo render_as_dropdown($data);
如果这是SQL,我只需要使用一个ARE子句,并让其余的继续。但这是我以前从未遇到过的,需要帮助。谢谢。您必须意识到的第一件事是处理的是数组,而不是数据库。SQL只是将结果集作为数组返回,然后您只需按需要的方式对其进行解析 至于您的函数,它有几个问题:您的数据加载、解析和呈现(似乎如此),这只会使读取它的函数体变得更加困难 为了解决这样的问题,您需要将任务分解为几个小任务。以下是需要做的事情:
<ul>
<li>A Athletic Wear</li>
<ul>
<li>Crewneck Sweatshirt</li>
</ul>
<li>A Accessories</li>
<ul>
<li>Aprons</li>
</ul>
</ul>
$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;
}
函数解析_数据($array){
//我们要在这里保持关系
$data=数组(
'items'=>array(),
“父项”=>array()
);
foreach($array作为$node){
//第一步-使这些XML属性更易于阅读
$attributes=(数组)$node->attributes();
$attributes=$attributes['@attributes'];
$data['items'][$attributes['product\u category\u id']]=$attributes;
//当我们没有父母时,让我们将其设为空以避免冲突
如果(!isset($attributes['parent_id'])){
$attributes['parent_id']=null;
}
$data['parents'][$attributes['parent_id'][]=$attributes['product_category_id'];
}
返回$data;
}
函数render_as_下拉菜单($data,$parentId=null){
$html='';
if(isset($data['parents'][$parentId])){
$html=“”;
foreach($data['parents'][$parentId]作为$itemId){
$html.='- ';
//递归查找子项
$html.=呈现为下拉菜单($data,$itemId);
$html.='
';
}
$html.='
';
}
返回$html;
}
这可能会对您有所帮助,这与SQL中的相同。从SQL查询中获取结果,然后解析该数组。唯一的区别是提取数组的方式。您能提供一个XML文档的示例吗?(不是结果,即simplexmlement
)不客气。这个东西被称为邻接列表模型,它基本上用于制作无限的产品类别深度,以及直接从CMS制作下拉菜单。我只是根据你的情况采用它。如果您想了解更多信息,请查看:www.sitepoint.com/hierarchy-data-database/仍在查看代码。仍然困惑不解。您基本上获取了整个提要并将其重新构造为自定义数组,然后使用一个简洁的递归函数将其结束。我在上面刻了一个字:)相信我,我已经处理这个问题很长时间了。这是解决这个问题的唯一明确办法。您可以改进的是将这些内容转换为OOP,但概念保持不变。至于改进从外部资源加载的时间,您可以使用curl
,然后通过simplexml\u load\u string()
将其作为字符串加载,curl在这方面如何改进加载?simplexml函数已经在使用原始数据访问页面,因此没有太多需要处理的内容。只是想知道。谢谢。simplexml\u load\u file($url)
相当于simplexml\u load\u字符串(file\u get\u contents($url))
。关于获取数据的webfile\u get\u contents()与curl
有很多讨论。看看这个例子,