1. <?php
  2. /**
  3. * Strips metadata from a list of items.
  4. *
  5. * @param array $data The input array of data.
  6. * @return array The array with metadata stripped.
  7. */
  8. function stripMetadata(array $data): array
  9. {
  10. $strippedData = [];
  11. foreach ($data as $item) {
  12. if (is_array($item)) {
  13. $strippedItem = [];
  14. foreach ($item as $key => $value) {
  15. if ($key !== '_meta' && $key !== '_id') { //Exclude meta and id keys
  16. $strippedItem[$key] = $value;
  17. }
  18. }
  19. $strippedData[] = $strippedItem;
  20. } else {
  21. $strippedData[] = $item; //If not an array, just keep the item as is.
  22. }
  23. }
  24. return $strippedData;
  25. }
  26. //Example Usage:
  27. /*
  28. $data = [
  29. ['id' => 1, '_meta' => ['author' => 'John Doe', 'date' => '2023-10-27'], 'title' => 'My Article'],
  30. ['id' => 2, '_meta' => ['author' => 'Jane Smith', 'date' => '2023-10-28'], 'title' => 'Another Article'],
  31. 'plain text'
  32. ];
  33. $strippedData = stripMetadata($data);
  34. print_r($strippedData);
  35. */
  36. ?>

Add your comment