PHP-foreach循环,例如WHERE子句 [英] PHP - foreach loop like WHERE clause

查看:52
本文介绍了PHP-foreach循环,例如WHERE子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图弄清楚这一点,但无法将我的头缠住它.

I've been trying to figure this one out but just can't wrap my head around it.

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

I have these xml objects that I'm working with:

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
    )

)

使用哪种正确的循环,以便最终标记如下所示:

What would be the right loop to use so that the final markup looks like this:

<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子句,然后进行其余操作.但这是我以前从未遇到过的,需要帮助.谢谢.

If this was SQL i'd simply be using a WERE clause and get the rest going. But this one's something I haven't encountered before and need help. Thanks.

推荐答案

您首先要意识到的是,您正在处理数组,而不是数据库. SQL只是将结果集作为数组返回,然后只需按照需要的方式对其进行解析.

The very first thing you have to realize is that you're dealing with an array, not with a database. A SQL simply returns a result-set as array, and then you just parse it the way you need.

关于您的功能,它有几个问题:您数据紧密耦合加载,解析和渲染(似乎如此),这只会使它的函数体难以阅读.

As for you function, it has several problems: You tightly-couple data loading, parsing and rendering (seems so), that merely makes harder to read its function body.

为了解决这样的问题,您需要将任务分解为小任务.这是需要做的事情:

In order to solve a problem like this, you need to break the task into small ones. Here's what needs to be done:

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

因此,在您的情况下,将如下所示:

So in your case, that would look like this:

$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-foreach循环,例如WHERE子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆