Quantcast
Channel: Recent Gists from assertchris
Viewing all articles
Browse latest Browse all 30

marshal.php

$
0
0
marshal.php
<?php
function marshal(array $rows) {
foreach ($rows as $row) {
assert(is_object($row));
assert(property_exists($row, "id"));
assert(property_exists($row, "type"));
$row->relatives = array_filter($rows, function ($next) use ($row) {
$key = "{$row->type}_id";
if (property_exists($next, $key)) {
return $next->$key === $row->id;
}
});
yield $row;
}
}
$rows = json_decode(json_encode([
["type" => "blog", "id" => 1, "taxonomy_id" => 1],
["type" => "blog", "id" => 2, "taxonomy_id" => 2],
["type" => "post", "id" => 1, "blog_id" => 1, "taxonomy_id" => 1],
["type" => "post", "id" => 2, "blog_id" => 1, "taxonomy_id" => 2],
["type" => "post", "id" => 3, "blog_id" => 2, "taxonomy_id" => 1],
["type" => "post", "id" => 4, "blog_id" => 2, "taxonomy_id" => 2],
["type" => "taxonomy", "id" => 1],
["type" => "taxonomy", "id" => 2],
]));
print_r(
iterator_to_array(
marshal($rows)
)
);

Viewing all articles
Browse latest Browse all 30

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>