Alexandrw
Alexandrw

Reputation: 1329

How to create a function in PHP that sorts an array based on one of its keys

I have the following array:

$class = array(
        'e1' => array('nume' => 'Nitu', 'prenume' => 'Andrei', 'sex' => 'm', 'varsta' => 23),
        'e2' => array('nume' => 'Nae', 'prenume' => 'Ionel', 'sex' => 'm', 'varsta' => 27),
        'e3' => array('nume' => 'Noman', 'prenume' => 'Alice', 'sex' => 'f', 'varsta' => 22),
        'e4' => array('nume' => 'Geangos', 'prenume' => 'Bogdan', 'sex' => 'm', 'varsta' => 23),
        'e5' => array('nume' => 'Vasile', 'prenume' => 'Mihai', 'sex' => 'm', 'varsta' => 25)
);

What I would like to do is to create a function that can sort through this array based on one of its keys, for example I want the function to sort and output all the males ('sex' => 'm') from the array.

I have managed to do this with:

foreach ($class as $val) {
    if ($val['sex'] == 'm')
    echo $val['nume'].' '.$val['prenume'].'<br/>';
}

But I want to create a function of my own that can do just this, which would help me a lot and give me more insight on how functions work and how they should be done.

Upvotes: 2

Views: 190

Answers (6)

Degar007
Degar007

Reputation: 127

   $class = array(
          array('nume' => 'Nitu', 'prenume' => 'Andrei', 'sex' => 'm', 'varsta' => 23),
          array('nume' => 'Nae', 'prenume' => 'Ionel', 'sex' => 'm', 'varsta' => 27),
          array('nume' => 'Noman', 'prenume' => 'Alice', 'sex' => 'f', 'varsta' => 22),
          array('nume' => 'Geangos', 'prenume' => 'Bogdan', 'sex' => 'm', 'varsta' => 23),
          array('nume' => 'Vasile', 'prenume' => 'Mihai', 'sex' => 'm', 'varsta' => 25) 
);

$object = json_decode(json_encode($class), FALSE);

after that you can do your foreach compare like so:

foreach ($object as $val) 
{
if ($object['sex'] == 'm')
echo $object['nume'].'  '.$object['prenume'].' SEX is : '.$object['sex'].'<br/>';
}

oops just saw that this post was over a year old...perhaps someone may find it useful.

Upvotes: 1

Fabian
Fabian

Reputation: 318

If you want to write the function yourself and don't use the existing array_filter function, you could do the following:

findAll($array, $key, $value)
{
    $result = array()
    foreach ($array as $val) 
    {
        if ($val[$key] == $value)
        {
            $result[] = $val;
        }
    }
}

and call it like findAll($class, "sex", "m"). It will return an array of results that you can display as you already did:

$filteredArray = findAll($class, "sex", "m")
foreach ($filteredArray as $item) 
{
    echo $item['nume'].' '.$item['prenume'].'<br/>';
}

Upvotes: 2

devang jogiya
devang jogiya

Reputation: 234

Try this:

<?php
function array_sort($array,$search_key,$search_val){
    $re_array = array();
    foreach($array as $value){
        if($value[$search_key] == $search_val)
            $re_array[] = $value;
    }
    return $re_array;
}
$class = array(
        'e1' => array('nume' => 'Nitu', 'prenume' => 'Andrei', 'sex' => 'm', 'varsta' => 23),
        'e2' => array('nume' => 'Nae', 'prenume' => 'Ionel', 'sex' => 'm', 'varsta' => 27),
        'e3' => array('nume' => 'Noman', 'prenume' => 'Alice', 'sex' => 'f', 'varsta' => 22),
        'e4' => array('nume' => 'Geangos', 'prenume' => 'Bogdan', 'sex' => 'm', 'varsta' => 23),
        'e5' => array('nume' => 'Vasile', 'prenume' => 'Mihai', 'sex' => 'm', 'varsta' => 25)
);
$data = array_sort($class,'sex','m');
echo "<pre>";
print_r($data);
echo "</pre>";
exit;
?>

Upvotes: 2

david
david

Reputation: 3218

function output($key, $criteria){
    foreach ($class as $val){
        if ($val[$key] == $criteria)
            echo $val['nume'].' '.$val['prenume'].'<br/>';
    }
}

You can call this function by passing the parameter. For example: output('sex', 'm'). Assume that $class is global variable.

Upvotes: 1

Alma Do
Alma Do

Reputation: 37365

You're looking not for sorting your array, but for filtering it - feel the difference.

In PHP, there's array_filter() to do this. If you want to create flexible function, you can act like this:

function filterArray($data, $key, $value)
{
   return array_filter($data, function($row) use ($key, $value)
   {
      return $row[$key]==$value;
   });
}

$class = array(
        'e1' => array('nume' => 'Nitu', 'prenume' => 'Andrei', 'sex' => 'm', 'varsta' => 23),
        'e2' => array('nume' => 'Nae', 'prenume' => 'Ionel', 'sex' => 'm', 'varsta' => 27),
        'e3' => array('nume' => 'Noman', 'prenume' => 'Alice', 'sex' => 'f', 'varsta' => 22),
        'e4' => array('nume' => 'Geangos', 'prenume' => 'Bogdan', 'sex' => 'm', 'varsta' => 23),
        'e5' => array('nume' => 'Vasile', 'prenume' => 'Mihai', 'sex' => 'm', 'varsta' => 25)
);

$result = filterArray($class, 'sex', 'm');

Upvotes: 9

Jaroslav
Jaroslav

Reputation: 1389

1.You can user array_filter function.

function filterSex($row){
  return $row['sex'] == 'm';
}

$newArray = array_filter($class, "filterSex");

2.Or you can iterate through array by yourself:

function filterSex($array){
  $result = array();
  foreach ($array in $row){
    if ($row['sex'] == 'm'){
      $result[] = $row;
    }
  }
  return $result;
}
$newArray = filterSex($class);

Upvotes: 1

Related Questions