❮ PHP Array Reference Show ExampleGet column of last names from a recordset: // An array that represents a possible record set returned from a database $last_names = array_column($a, 'last_name'); Output: Array Definition and UsageThe array_column() function returns the values from a single column in the input array. Syntaxarray_column(array, column_key, index_key) Parameter ValuesParameterDescriptionarrayRequired. Specifies the multi-dimensional array (record-set) to use. As of PHP 7.0, this can also be an array of objects.column_keyRequired. An integer key or a string key name of the column of values to return. This parameter can also be NULL to return complete arrays (useful together with index_key to re-index the array)index_keyOptional. The column to use as the index/keys for the returned arrayTechnical DetailsReturn Value:Returns an array of values that represents a single column from the input arrayPHP Version:5.5+More ExamplesExampleGet column of last names from a recordset, indexed by the "id" column: // An array that represents a possible record set returned from a database $last_names = array_column($a, 'last_name', 'id'); Output: Array ❮ PHP Array Reference Unlike some databases, MongoDB documents sometimes contain fields that have values as arrays. Sometimes a document that contains specific information inside these arrays needs to be checked. This tutorial will explain how to filter MongoDB array elements to extract and project only the necessary data, and not the entire array. Prerequisites
Creating Sample DatasetThis section will explain how to create a sample dataset that will consist of a collection named 1 db.icafe.insert([ The output should resemble the following: BulkWriteResult({ Using $elemMatchThis Here is the basic form of the : { $elemMatch : {,, … } } Using $elemMatch with find() methodExecute the following query in the Mongo shell to use the $elemMatch operator with the find() function: db.icafe.find( The results should resemble the following: 1 { Using $filterSometimes extracting or projecting all of the fields in the target document isn’t required, but only those records that match a defined criteria. Here the $filter operator can be used to refine and obtain the desired results. The $filter operator has three variables:
Execute the following code to effect the $filter operator: 1 db.icafe.aggregate([ Note that the above code is designed to filter the projected results by excluding the ratings field in the document. Its output should resemble the following: { Now note that the above results were successfully filtered as defined by the ConclusionThis tutorial explained how to filter MongoDB array elements to extract and project only the needed data contained in an array. The tutorial explained how to create a sample dataset and then use the $elemMatch and $elemMatch with the find() method to match documents that have an array field that contains at least one of the specified elements. The article also explained how to use $filter with the three variables to pull up only those records that match a defined criteria. Remember that all three of the variables must be used in the $filter operator to obtain the desired results. |