snipt

Ctrl+h for KB shortcuts
#128079

PHP

php array()

1
2
3
4
5
6
7
<?php
	$identity = array(
        		"name" => "Sam",
        		"age"  => "47",
        		"country" => "USA"
                );
?>
https://snipt.net/embed/d69135fcd66db9de96634f01a2c378e4/
https://snipt.net/raw/d69135fcd66db9de96634f01a2c378e4/
d69135fcd66db9de96634f01a2c378e4
php
PHP
7
2014-04-14T23:02:44
True
False
/api/public/snipt/128079/
php-array
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> </span><span id="L-2"><a name="L-2"></a> <span class="nv">$identity</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span> </span><span id="L-3"><a name="L-3"></a> <span class="s2">&quot;name&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;Sam&quot;</span><span class="p">,</span> </span><span id="L-4"><a name="L-4"></a> <span class="s2">&quot;age&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;47&quot;</span><span class="p">,</span> </span><span id="L-5"><a name="L-5"></a> <span class="s2">&quot;country&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;USA&quot;</span> </span><span id="L-6"><a name="L-6"></a> <span class="p">);</span> </span><span id="L-7"><a name="L-7"></a><span class="cp">?&gt;</span><span class="x"></span> </span></pre></div> </td></tr></table>
array, php
#108547

PHP

Convert Array to Object PHP

1
2
3
<?php

$object = json_decode(json_encode($array), FALSE);
https://snipt.net/embed/e501d688106c3f72b6487363b6e3d0b3/
https://snipt.net/raw/e501d688106c3f72b6487363b6e3d0b3/
e501d688106c3f72b6487363b6e3d0b3
php
PHP
3
2014-04-17T13:30:47
True
True
Jan 26, 2014 at 02:40 AM
/api/public/snipt/108547/
convert-array-to-object-php
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a><span class="nv">$object</span> <span class="o">=</span> <span class="nb">json_decode</span><span class="p">(</span><span class="nb">json_encode</span><span class="p">(</span><span class="nv">$array</span><span class="p">),</span> <span class="k">FALSE</span><span class="p">);</span> </span></pre></div> </td></tr></table>
array, object, php
#101477

PHP

Simple XML to associative array (PHP)

1
2
3
4
5
6
// XML to associative array

$xml_string = file_get_contents("php://input");
$xml  = simplexml_load_string($xml_string);
$json = json_encode($xml);
$arr  = json_decode($json, true);
https://snipt.net/embed/722c731c88b4e580fae7f3ebf2c563ba/
https://snipt.net/raw/722c731c88b4e580fae7f3ebf2c563ba/
722c731c88b4e580fae7f3ebf2c563ba
php
PHP
6
2014-04-17T07:23:39
True
False
/api/public/snipt/101477/
xml-to-associative-array-php
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="x">// XML to associative array</span> </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a><span class="x">$xml_string = file_get_contents(&quot;php://input&quot;);</span> </span><span id="L-4"><a name="L-4"></a><span class="x">$xml = simplexml_load_string($xml_string);</span> </span><span id="L-5"><a name="L-5"></a><span class="x">$json = json_encode($xml);</span> </span><span id="L-6"><a name="L-6"></a><span class="x">$arr = json_decode($json, true);</span> </span></pre></div> </td></tr></table>
array, php, xml