Using count($array) to retrieve only matching $key

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

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.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I am running a loop of an $array where I am only including results that have a matching user role (from Wordpress users).

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']

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

profilepic.png
manpreet 2 years ago

 

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.

If the key as the same as the value you better use isset (faster - O(1) instead O(n)) as:

$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


0 views   0 shares

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.