Comments (2)
The where
method returns a sequence of elements. If you need only one element, you should use first
(if you're absolutely sure at least one element exists) or firstOrDefault
(it returns default null
if the searched sequence is empty). It also returns the whole row, not just the value, so you need to get meta_value
. So your code becomes:
$usermetas = $wpdb->get_results("SELECT * FROM {$wpdb->usermeta}");
foreach ($users as $user) {
$dateOfBirth = from($usermetas)
->firstOrDefault(function($um) {
return $um['user_id'] === $user->ID && $um['meta_key'] === 'co_dateofbirth' ;});
if ($dateOfBirth != null)
echo "co_dateofbirth value is: {$dateOfBirth->meta_value}<br/>";
}
Or, if all users are guaranteed to have a date of birth:
$usermetas = $wpdb->get_results("SELECT * FROM {$wpdb->usermeta}");
foreach ($users as $user) {
$dateOfBirth = from($usermetas)
->first(function($um) {
return $um['user_id'] === $user->ID && $um['meta_key'] === 'co_dateofbirth' ;});
echo "co_dateofbirth value is: {$dateOfBirth->meta_value}<br/>";
}
Note that your code is inefficient because it puts the whole UserMeta table into memory while you only need dates of birth. It would be more optimal modify your SQL query to get all your data at once and only it.
The following query would return only the data you actually need:
SELECT u.id, um.meta_value
FROM user AS u
INNER JOIN usermeta AS um ON um.user_id = u.id
WHERE um.meta_key = 'co_dateofbirth'
In general, if a query is possible within SQL, you should perform it within SQL. You should only use YaLinqo (and any filtering and transformation using array functions in general) only in cases SQL is unavailable.
(I haven't tested any of the code above.)
from yalinqo.
The code seems returning wrong field which is the 1st on table, is it a problem on the first function conditions!
from yalinqo.
Related Issues (20)
- Undefined foreach variable HOT 2
- Switch from VersionEye.com to Libraries.io badge
- Version 3 doesn't exist HOT 5
- Library should throw own exception types HOT 1
- Distinct exception cases should have distinct exception types HOT 2
- Add reverse()
- how to 'sum my data' HOT 1
- Remove autoload files
- Array of arrays - firstOrDefault by reference? HOT 2
- Distinct returns incorrect indexes HOT 1
- Iterating through two arrays of objects with same ids HOT 6
- Replace Functions::compareStrict and compareLoose with spaceship operator
- Examples? HOT 4
- ReadMe: update syntax, sensible intro for newbies, FAQ
- Enumerable::getIterator fails silently HOT 2
- not PHP8 compatible -> obsolete function create_function HOT 1
- Abandoned? HOT 1
- how to add them to my project if I have no composer? HOT 1
- Custom comparer in orderBy is ignored if key selector is cached
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from yalinqo.