Finding an array using php

This is my code with an array of samples. If the user is looking for 523465, then the index should return the main array userdb..Have tried in several ways, but could not get the correct output. Can anybody help me?

   <?php
   $userdb = Array (
        (0) => Array (
            (uid) => Array (
                    (0) => Array (
                            '10770' 
                    ),

                    (1) => Array (
                            '523465' 
                    ),

                    (2) => Array (
                            '4042389' 
                    ) 
            ),
            (name) => 'Sandra Shush',
            (url) => 'urlof100' 
    ),

    (1) => Array (
            (uid) => Array (
                    (0) => Array (
                            '102320' 
                    ),

                    (1) => Array (
                            '532465' 
                    ),

                    (2) => Array (
                            '40432389' 
                    ) 
            ),
            (name) => 'Stefanie Mcmohn',
            (pic_square) => 'urlof100' 
    ));
    echo '<pre>';
    print_r(array_column ( $userdb, 'uid' ));
    $key = array_search ( 5432365, array_column ( $userdb, 'uid' ) );

    echo ("The key is: " . $key);
+4
source share
4 answers

It takes a couple of foreach loops. Here is the function I wrote for you. The first parameter is your array, and the second parameter is the value you want to execute.

function searchArrayKey($userdb,$given_value){

foreach ($userdb as $firstkey => $val) {

    foreach ($val as $secondkey => $nestedarray){

        foreach ($nestedarray as $thirdkey => $thirdnestedarray){
            if ($thirdnestedarray['0'] == $given_value){
                return $firstkey;
            }
        }
    }
}

}

Then use to get the key of the main array

print_r (searchArrayKey($userdb, 523465));
+3
source

Several foreach loops would accomplish this task:

function user_from_uid(array $usersdb, $uid){

    foreach($usersdb as $user){
        foreach($user['uid'] as $id){
            if($id==$uid){
                return $user;
            }
        }
    }
    //handle non existent user somehow
    return false; //or throw exception etc

}

, -, uid :

class UserList
{

    private $usersArray, $uidMap;

    function __construct($usersArray)
    {
        $this->usersArray = $usersArray;
        $this->uidMap=[];
        $this->createMap();
    }

    private function createMap()
    {
        foreach($this->usersArray as $key => $user){
            foreach($user['uid'] as $uid){
                $this->uidMap[$uid]=$key;
            }
        }
    }

    function getUserByUid($uid)
    {
        if(isset($this->uidMap[$uid])){
            return $this->usersArray[$this->uidMap[$uid]];
        }
        //handle non existent user somehow
        return false; //or throw exception etc        
    }   

} 

$userList = new UserList($usersdb);

$someUser = $userList->getUserByUid('523465');
$another  = $userList->getUserByUid('40432389');
+2
 foreach ($userdb as $key=> $db) {
    if (in_array($key, $db["uid"])) {
       $parent_key = $key;
       break;
    } 
 }

echo $parent_key;
0

,

$key = '102320';
foreach($userdb as $user){
    foreach($user['uid'] as $id){
        if($id[0] == $key)      
        {
            echo "Match found--->".$key;
        }       
    }   
}

DEMO

, .

-1

Source: https://habr.com/ru/post/1651526/


All Articles