1

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:

  1. If there’s an id in the $list_order that doesn’t exist in the $list of books, the function should just ignore it.
  2. 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'))
SnatchIT
  • 43
  • 5
  • Please provide your exact desired output from your sample input. Your sample data does not contain `$list_order`. – mickmackusa Oct 03 '20 at 22:23
  • @ikiK this question is poorly tabbed. When you do a post edit, you should endeavor to make the "final" edit so that the number of edits in the history is minimized. Please be more thorough. – mickmackusa Oct 03 '20 at 22:26
  • Your [mcve] does a poor job of representing the fringe cases to accommodate. – mickmackusa Oct 03 '20 at 22:29

0 Answers0