【问题标题】:PHP - foreach loop like WHERE clausePHP - 类似于 WHERE 子句的 foreach 循环
【发布时间】:2014-08-19 05:36:16
【问题描述】:

我一直在想办法解决这个问题,但就是想不通。

我有这些我正在使用的 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 子句并让其余部分继续进行。但这是我以前没有遇到过的,需要帮助。谢谢。

【问题讨论】:

  • 也许这会对你有所帮助stackoverflow.com/questions/10351713/…
  • 这与 SQL 中的相同。您从 SQL 查询中获取结果,然后解析该数组。唯一的区别是该数组是如何被提取的。
  • 您能否提供该 XML 文档的示例? (不是结果,即SimpleXMLElement)

标签: php foreach where


【解决方案1】:

首先你必须意识到你是在处理一个数组,而不是一个数据库。 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;
}

【讨论】:

  • 不客气。这个东西叫做Adjacency list model,它基本上是用来制作无限的产品类别深度,以及直接从CMS制作下拉菜单。我只是根据您的情况采用它。如果您想了解更多信息,请查看:www.sitepoint.com/hierarchical-data-database/
  • 还在看代码。还是一头雾水。您基本上采用了整个提要并将其重组为自定义数组,然后使用简洁的递归函数对其进行了补充。我正在刻这个:)
  • 相信我,我已经处理这个问题很长时间了。这是解决这个问题的唯一明确方法。您可以改进的是将这些东西变成 OOP,但概念保持不变。至于提高外部资源的加载时间,您可以使用curl,然后通过simplexml_load_string()将其作为字符串加载
  • curl 在这方面如何改善加载? simplexml 函数已经使用原始数据访问页面,因此无需处理太多。就是想。谢谢。
  • simplexml_load_file($url) 等效于 simplexml_load_string(file_get_contents($url))。网上有很多讨论file_get_contents() vs. curl 来获取数据。例如看这个,stackoverflow.com/a/24954327/1208233
猜你喜欢
  • 2014-10-14
  • 2018-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多