nutools/lib/repoctl/json_each.php

29 lines
724 B
PHP
Raw Normal View History

#!/usr/bin/php
<?php # -*- coding: utf-8 mode: php -*- vim:sw=2:sts=2:et:ai:si:sta:fenc=utf-8
if ($argc <= 1) die("ERROR: Vous devez spécifier le nom de la clé\n");
$data = json_decode(stream_get_contents(STDIN), true);
foreach ($data as $datum) {
$first = true;
for ($i = 1; $i < $argc; $i++) {
$result = $datum;
$keys = explode(".", trim($argv[$i]));
foreach ($keys as $key) {
if ($result === null) break;
if (!$key) continue;
if (isset($result[$key])) {
$result = $result[$key];
} else {
$result = null;
}
}
if ($first) $first = false;
else echo "\t";
if (is_array($result)) var_export($result);
else echo $result;
}
echo "\n";
}