Note that there are some explanatory texts on larger screens.

plurals
  1. POphp: trying to find an equivalent of map
    primarykey
    data
    text
    <p>I'm struggling to build an object of objects, instead of an array of objects, (that works but is less easy to use later with json)</p> <p>use <a href="http://writecodeonline.com/php/" rel="nofollow">http://writecodeonline.com/php/</a> for testing it</p> <pre><code>$a = Array(); $obj = new stdClass(); $obj-&gt;key = "name"; $obj-&gt;value = "durant"; array_push($a, $obj); $obj = new stdClass(); $obj-&gt;key = "friend"; $obj-&gt;value = "johns"; array_push($a, $obj); $preds = Array(); foreach( $a as $v ){ $item = Array(); // new stdClass(); $item[$v-&gt;key] = $v-&gt;value; // ^ doesn't work array_push($preds, $item); } $obj = new stdClass(); $obj-&gt;key = "data"; $obj-&gt;value = $preds; array_push($a, $obj); //var_dump($a); echo(json_encode($a[2]-&gt;value)); // returns: [{"name":"durant"},{"friend":"johns"}] </code></pre> <p>I would like to be able to easily perform $arr['name'] or $arr['friend']</p> <p><strong>EDIT</strong>: works like that (thx Dani): I'm totally noob to Php, so If someone can explain (the JSON_FORCE_OBJECT opts did not solved it)</p> <pre><code>$a = Array(); $obj = new stdClass(); $obj-&gt;key = "name"; $obj-&gt;value = "durant"; array_push($a, $obj); $obj = new stdClass(); $obj-&gt;key = "friend"; $obj-&gt;value = "johns"; array_push($a, $obj); $preds = new stdClass(); foreach( $a as $v ){ $k = $v-&gt;key; $preds-&gt;$k = $v-&gt;value; } $obj = new stdClass(); $obj-&gt;key = "data"; $obj-&gt;value = $preds; array_push($a, $obj); echo(json_encode($a[2]-&gt;value)); // returns: {"name":"durant", "friend":"johns"} </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload