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

标签 php foreach where

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

我有这些正在使用的 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/

相关文章:

javascript - 带你的href视频

php - 在 html : returned data object empty 中显示来自 SQL 数据库的数据

php - 使用foreach循环将数据插入mysql数据库

sql - 如何检查SQL中的组中的所有字段是否相等

php - 无法从 XAMPP 控制面板启动 MySQL

php - 使用 whereHas 方法的 Laravel Eloquent 查询的奇怪行为

c# - 将 foreach 替换为 LINQ 表达式

r - 如何防止嵌套 foreach 循环使用 R 中所有核心的 100% CPU?

filter - 在WHERE过滤器上使用OR条件的StrongLoop回送模型查找

sqlite - 如何在 SQLite 中添加总列