Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts

Thursday 4 June 2020

How to convert a multidimensional Array To XML in PHP

function createxmlele($data, $name='root', &$doc=null, &$node=null){
        if ($doc==null){
            $doc = new DOMDocument('1.0','UTF-8');
            $doc->formatOutput = TRUE;
            $node = $doc;
        }

        if (is_array($data)){
            foreach($data as $var=>$val){
                if (is_numeric($var)){
                    $this->createxmlele($val, $name, $doc, $node);
                }else{
                    if (!isset($child)){
                        $child = $doc->createElement($name);
                        $node->appendChild($child);
                    }

                    $this->createxmlele($val, $var, $doc, $child);
                }
            }
        }else{
            $child = $doc->createElement($name);
            $node->appendChild($child);
            $textNode = $doc->createTextNode($data);
            $child->appendChild($textNode);
        }

        if ($doc==$node){
            return $doc;
        }
    }

Friday 5 October 2018

How to get list of all categories array tree view in Magento 1

Call Category function

$categories = $this->getTreeCategories(2, false);
print_r($categories);
Get All categories array tree view

function getTreeCategories($parentId, $isChild) {
     
        $allCats = Mage::getModel('catalog/category')->getCollection()
                ->addAttributeToSelect('*')
                ->addAttributeToFilter('is_active', '1')
                ->addAttributeToFilter('include_in_menu', '1')
                ->addAttributeToFilter('parent_id', array('eq' => $parentId));
        $subcat = ($isChild) ? "  -- " : "";
        foreach ($allCats as $category) {
            $options[] = array(
                'label' => Mage::helper('jaydip_attrfiltercount')->__($subcat.iconv(mb_detect_encoding($category->getName(), mb_detect_order(), true), "UTF-8", $category->getName())),
                'value' => $category->getId()
            );
            $subcats = $category->getChildren();
            if ($subcats != '') {
                $options[] = $this->getTreeCategories($category->getId(), true);
            }
        }
        $optiondata = array();
        foreach ($options as $option):
            if(isset($option[0])){
             
                foreach ($option as $value):
                    $optiondata[] = array(
                        'label' => $value['label'],
                        'value' => $value['value']
                    );
                endforeach;
             
            }else{
                $optiondata[] = array(
                    'label' => $option['label'],
                    'value' => $option['value']
                );
            }
        endforeach;
     
        return $optiondata;
    }

Wednesday 4 May 2016

Convert PHP, Magento, Joomla, Codeigniter, Cake PHP, Wordpress XML to JSON, XML to Array, JSON to Array

Convert PHP, Magento, Joomla, Codeigniter, Cake PHP, Wordpress XML to JSON, XML to Array, JSON to Array is very difficult task for some people. But, in PHP, believe me it’s very easy. One line of code each! Don’t believe? Just check out the code below and test it yourself!


$xml = simplexml_load_string($xmldata);
$json = json_encode($xml);
$array = json_decode($json,true);