Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech Bugs & Fixes 2 years ago
Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
Turn Your Knowledge into Earnings.
I am running a loop of an $array where I am only including results that have a matching user role (from Wordpress users).
$array
I have the loop:
// custom function that returns the user's role (administrator, reader, etc) $userRole = get_current_user_role(); foreach( $location_array as $key => $location ) { if( in_array( $userRole, $location['c'] ) ) { // do the things echo 'style="width: calc( 100vw / ' . count($array) . ');">'; echo $location['a']; echo ''; } }
With the $array composing of:
Array ( [1] => Array ( [a] => Location 1 [b] => loc04 [c] => Array ( [administrator] => administrator ) ) [2] => Array ( [a] => Location 2 [b] => loc23 [c] => Array ( [administrator] => administrator, [publisher] => publisher ) ) [3] => Array ( [a] => Location 3 [b] => loc12 [c] => Array ( [publisher] => publisher, [viewer] => viewer ) ) )
However, when I try to get the count of the $array, it is returning the number of all the $key, and I dont know how to narrow it down to just the ones from the in_array( $userRole, $location['c']
$key
in_array( $userRole, $location['c']
I have tried putting the in_array( $userRole, $location['c'] from the loop into a variable, and then outputting that but I wasnt having any success with it other than returning the value of 1, but there being two matches for administrator
You cannot use the count on the array on-going. You need to filter it first!
You can use array-filter as:
$arr = [["b" => "loc04", "c" => ["com/tag/administrator">administrator" => "com/tag/administrator">administrator"]]]; $arr[] = ["b" => "loc23", "c" => ["com/tag/administrator">administrator" => "com/tag/administrator">administrator", "publisher" => "publisher"]]; $arr[] = ["b" => "loccom/tag/1">12", "c" => ["publisher" => "publisher", "viewer" => "viewer"]]; $role = "com/tag/administrator">administrator"; $a = array_filter($arr, function ($e) use ($role) {return in_array($role, $e["c"]);});
Now $a will contain the 2 element.
$a
If the key as the same as the value you better use isset (faster - O(1) instead O(n)) as:
isset
$a = array_filter($arr, function ($e) use ($role) {return isset($e["c"][$role]);});
Then you can just loop over $a and do:
foreach($a as $location) { echo 'calc( com/tag/1">100vw / ' . count($a) . ');">'; echo $location['a']; echo ''; }
Live example: 3v4l
No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
General Tech 10 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 9 Answers
General Tech 2 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.