Home > Software design >  PHP multidimensional array search by value using another value
PHP multidimensional array search by value using another value

Time:10-07

I need to get the the distinct 'uid' and its corresponding 'name' , how can I do this in a multidimensional array?

$userdb = array(
array(
    'uid' => '100',
    'name' => 'Sandra Shush',
    'pic_square' => 'urlof100'
),
array(
    'uid' => '5465',
    'name' => 'Stefanie Mcmohn',
    'pic_square' => 'urlof100'
),
array(
    'uid' => '5465',
    'name' => 'Jane Doe',
    'pic_square' => 'urlof100'
),
array(
    'uid' => '40489',
    'name' => 'Michael',
    'pic_square' => 'urlof40489'
), 
array(
    'uid' => '40489',
    'name' => 'Jane Doe',
    'pic_square' => 'urlof40489'
));

sample output:

$data = [{uid: '100', name: 'Sandra Shush'},{uid: '5465', name: ['Stefanie Mcmohn','Jane Doe']}, {uid: '40489', name: ['Michael','Jane Doe']}]

tried working with array_columns and array_keys but i cant think of the right way to get this.

CodePudding user response:

try this one, hope it will help you.

$userdb = array(
    array(
        'uid' => '100',
        'name' => 'Sandra Shush',
        'pic_square' => 'urlof100'
    ),
    array(
        'uid' => '5465',
        'name' => 'Stefanie Mcmohn',
        'pic_square' => 'urlof100'
    ),
    array(
        'uid' => '40489',
        'name' => 'Michael',
        'pic_square' => 'urlof40489'
    ),
    array(
        'uid' => '40489',
        'name' => 'Jane Doe',
        'pic_square' => 'urlof40489'
        ));
$nameArray = [];
$myUuid = '40489';
foreach ($userdb as $user) {
    if ($myUuid == $user['uid']) {
        array_push($nameArray, $user['name']);
    }
}
print_r($nameArray);

and moreover you can get any parameter or anything you required from the above array from that foreach loop.

CodePudding user response:

Try this solution:

$data = [];

foreach($userdb as $user)
{
    if(array_key_exists($user['uid'], $data))
    {
        array_push($obj->name, $user['name']);
    }
    else
    {
        $obj = new stdClass;

        $obj->uid = $user['uid'];
        $obj->name = [];
        
        array_push($obj->name, $user['name']);
        $data[$user['uid']] = $obj;
    }
}

print_r($data);

CodePudding user response:

I have this function that does very close to what yo need, just modify the return as per your request. Good luck

function array_searchRecursive($needle, $haystack, $strict = false, $path = array()) {
    if (!is_array($haystack)) {
        return false;
    }
    foreach ($haystack as $key => $val) {
        if (is_array($val) && $subPath = array_searchRecursive($needle, $val, $strict, $path)) {
            $path = array_merge($path, array($key), $subPath);
            return $path;
        } elseif ((!$strict && $val == $needle) || ($strict && $val === $needle)) {
            $path[] = $key;
            return $path;
        }
    }
    return false;
}

CodePudding user response:

Try Something like this , here i am storing uid to an array and checking is it already available in the array with in_array function

<?php
$userdb = array(
    array(
        'uid' => '100',
        'name' => 'Sandra Shush',
        'pic_square' => 'urlof100'
    ),
    array(
        'uid' => '5465',
        'name' => 'Stefanie Mcmohn',
        'pic_square' => 'urlof100'
    ),
    array(
        'uid' => '40489',
        'name' => 'Michael',
        'pic_square' => 'urlof40489'
    ),
    array(
        'uid' => '40489',
        'name' => 'Jane Doe',
        'pic_square' => 'urlof40489'
        ));
$newUserDb = array();

$arrayUserId=array();
foreach ($userdb as $user) {
  $newUserDbSingle=array();
  $nameArray=array();
    if (in_array($user['uid'], $arrayUserId))
    {
      $key = array_search ($user['uid'], $arrayUserId);
      $nameArray=$newUserDb[$key]['name'];
      array_push($newUserDb[$key]['name'], $user['name']);
      
    }
    else
    {
      unset($user['pic_square']);
      $arrayUserId[]=$user['uid'];
      $newUserDbSingle=$user;
      $newUserDbSingle['name']=array($user['name']);
      $newUserDb[]=$newUserDbSingle;
    }
}
print_r($newUserDb);
?>
  • Related