PHP - foreach loop like WHERE clause

时光总嘲笑我的痴心妄想 提交于 2019-12-22 09:22:08

问题


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

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>

Here's my function:

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;

}

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.


回答1:


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. Loading data from external resource (be it XML, JSON or whatetver)
  2. Parsing that data
  3. Rendering parsed data

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);

And the functions, would look like this,

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;
}


来源:https://stackoverflow.com/questions/25376295/php-foreach-loop-like-where-clause

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!