I have this array
$books = [ [
'id' => 23,
'title' => 'The divine Comedy',
'description' => "Lorem ipsum dolor sit amet..."
], [
'id' => 44,
'title' => 'The woman from Zagreb',
'description' => "Lorem ipsum dolor sit amet, consectetur..."
], [
'id' => 9,
'title' => 'Blindness',
'description' => "Consectetur adipiscing elit..."
], [
'id' => 1973,
'title' => 'Faust',
'description' => "Tempor incididunt ut labore et dolore magna aliqua."
]
];
$books_order = [44, 1973, 23, 9];
$ordered_books = ordered_list($books, $books_order);
I want to to add if statements into the following function:
- If there’s an id in the $list_order that doesn’t exist in the $list of books, the function should just ignore it.
- If some books are in the $list but their id is not in the $list_order, this book must be appended at the end of the list.
function ordered_list($list, $books_order) {
$ordered = [];
foreach ($books_order as $ordered_id) { array_push(
$ordered,
...array_filter($list, function($item) use ($ordered_id) { return ($item['id'] == $ordered_id ? true : false);
})
);
}
return $ordered;
}
I tried something like this
if(property_exists($books, 'title'))