Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

hata http://blog.xole.net/

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Associate array to XML and JSON

PHP Associate array data

$data = array(
    "hoge" => 123,
    "foo" => 456,
    "bar" => 789,
    "aaa" => array(
        "abc" => 111,
        "bcd" => 222,
        "cde" => 333
    ),
    "bbb" => array(
        "def" => array(
            "efg" => "hoge"
        )
    )
);


to XML

$xml = new XmlWriter();
$xml->openMemory();
$xml->startDocument('1.0', 'UTF-8');
$xml->startElement('root');

function write(XMLWriter $xml, $data){
    foreach($data as $key => $value){
        if(is_array($value)){
            $xml->startElement($key);
            write($xml, $value);
            $xml->endElement();
            continue;
        }
        $xml->writeElement($key, $value);
    }
}
write($xml, $data);

$xml->endElement();
echo $xml->outputMemory(true);


output XML
<?xml version="1.0" encoding="UTF-8"?>
<root>
    <hoge>123</hoge>
    <foo>456</foo>
    <bar>789</bar>
    <aaa>
        <abc>111</abc>
        <bcd>222</bcd>
        <cde>333</cde>
    </aaa>
    <bbb>
        <def>
            <efg>hoge</efg>
        </def>
    </bbb>
</root>


to JSON

echo json_encode($data);


output JSON
{
    "hoge":123,
    "foo":456,
    "bar":789,
    "aaa":{
        "abc":111,
        "bcd":222,
        "cde":333
    },
    "bbb":{
        "def":{
            "efg":"hoge"
        }
    }
}


Requires PHP5.2.x or xmlwriter extension, json extension

Constructor overloading Java and PHP5

Java's constructor overload

class Hoge {

    public Hoge(){
        System.out.println("constructor 0");
    }

    public Hoge(int a){
        System.out.println("constructor 1:" + a);
    }

    public Hoge(int a, String[] hoge){
        System.out.println("constructor 2:" + a + ":" + hoge);
    }

    public Hoge(A a, B b, C c){
        System.out.println("constructor 3:" + a + ":" + b + ":" + c);
    }
}


PHP5 code

class Hoge {

    public function __construct(){
        $num = func_num_args();
        $args = func_get_args();
        switch($num){
        case 0:
            $this->__call('__construct0', null);
            break;
        case 1:
            $this->__call('__construct1', $args);
            break;
        case 2:
            $this->__call('__construct2', $args);
            break;
        case 3:
            $this->__call('__construct3', $args);
            break;
        default:
            throw new Exception();
        }
    }

    public function __construct0(){
        echo "constructor 0" . PHP_EOL;
    }

    public function __construct1($a){
        echo "constructor 1: " . $a . PHP_EOL;
    }

    public function __construct2($a, array $hoge){
        echo "constructor 2: " . $a . PHP_EOL;
        var_dump($hoge);
    }

    public function __construct3(A $a, A $b, C $c){
        echo "constructor 3: " . PHP_EOL;
        var_dump($a, $b, $c);
    }

    private function __call($name, $arg){
        return call_user_func_array(array($this, $name), $arg);
    }
}


example(PHP5)

interface C {
    const C = __CLASS__;
}
class A {
    const A = __CLASS__;
    private $a = array(1, 2, 3);
}
class B extends A{
    const B = __CLASS__;
    private $b = array(1, 2, 3);
}
class D extends B implements C {
    const D = __CLASS__;
    private $c = array(1, 2, 3);
}

$a = new Hoge();
$b = new Hoge(1);
$c = new Hoge(777, array(1,2,3));
$d = new Hoge(new A(), new B(), new D());
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS