Súbor a:
{ "books": [
{ "english_title": "nejaka kniha", "czech_title": "ceskynazev", "category": "beletrie", "chapter_count": 14 },
{ "english_title": "Dalsi kniha", "czech_title": "ceskynazev", "category": "beletrie", "chapter_count": 14 },
{ "english_title": "aaa", "czech_title": "ceskynazev", "category": "beletrie", "chapter_count": 14 },
{ "english_title": "bbb", "czech_title": "ceskynazev", "category": "beletrie", "chapter_count": 14 },
{ "english_title": "ccccc", "czech_title": "ceskynazev", "category": "beletrie", "chapter_count": 14 }
]}
Súbor b:
[
{"132153": {"no": 1, "title": "Nejaka kniha"}},
{"456832": {"no": 2, "title": "Dalsi kniha"}},
{"54654": {"no": 3, "title": "aaa"}},
{"65648": {"no": 4, "title": "bbb"}},
{"8787": {"no": 5, "title": "ccccc"}}
]
Kód (súbor a.php):
<?php
/*
* Licence: CC0
* Licence URL: https://creativecommons.org/publicdomain/zero/1.0/legalcode
*
* Run: php a.php
*/
function vypise_zoznam($path_book, $path_keys)
{
$file=file_get_contents("$path_book");
$input_books=json_decode($file, true);
$file=file_get_contents("$path_keys");
$keys=json_decode($file, true);
// books priradujem podla key
$books=array();
foreach($input_books['books'] as $book)
{
$new_key="X".$book['english_title'];
if(isset($books["$new_key"]))
{
// ak taky uz existuje
}
$books["$new_key"]=$book;
}
// vypis
foreach($keys as $key_item)
{
$key_id=array_key_first($key_item);
$key_data=$key_item[$key_id];
$real_title=$key_data['title'];
$key_check="X".$real_title;
if(isset($books["$key_check"]))
{
echo "$real_title - mame.\n";
$book=$books["$key_check"];
// print_r($book);
}
else
{
echo "$real_title - nemame.\n";
}
}
}
vypise_zoznam("a", "b");