Lewa Bammy Stephen
Lewa Bammy Stephen

Reputation: 399

I want to extract grades from this array containing subject and grades into an array

I am trying extract the grades e.g C5,C6 into another array from a string. Sample given below

String:

$str = "English Language-C5,Mathematics-C6,Economics-B3,Biology-C5,Geography-B2,Yoruba-B3,Agricultural Science-C6";

I created the following function to explode the string above into an array

function multiexplode($delimiters,$string) {
    $replace = str_replace(' ', '', $string); // remove spaces between subjects
    $ready = str_replace($delimiters, $delimiters[0], $replace);
    $launch = explode($delimiters[0], $ready);
    return  $launch;
}
print_r(multiexplode(array(" ",",","-"),$str));
// This was the result
Array
(
    [0] => EnglishLanguage
    [1] => C5
    [2] => Mathematics
    [3] => C6
    [4] => Economics
    [5] => B3
    [6] => Biology
    [7] => C5
    [8] => Geography
    [9] => B2
    [10] => Yoruba
    [11] => B3
    [12] => AgriculturalScience
    [13] => C6
)

How do I extract the grades(C6,B2...) uniquely into an array. I would appreciate another approach to solve this or what to be done on the code to arrive at the best solution. Thanks

Upvotes: 1

Views: 130

Answers (4)

mickmackusa
mickmackusa

Reputation: 48071

OP says: "How do I extract the grades(C6,B2...) uniquely into an array"

Assuming the substrings to be extracted always begin with Bor C (which are preceded by a hyphen AND there is always and only one number after the letter, use the following pattern. The \K means restart the fullstring match -- this is done to simplify the output array and avoid the use of a capture group.

Code: (Demo)

$csv = "English Language-C5,Mathematics-C6,Economics-B3,Biology-C5,Geography-B2,Yoruba-B3,Agricultural Science-C6";

var_export(preg_match_all('~-\K[BC]\d~', $csv, $out) ? array_unique($out[0]) : []);

Output:

array (
  0 => 'C5',
  1 => 'C6',
  2 => 'B3',
  4 => 'B2',
)

I don't know if alphabetizing or re-indexing is required, so I'll leave it as it is.

Upvotes: 2

user3783243
user3783243

Reputation: 5223

I would parse the string as a CSV then explode on the -s.

<?php
$str = "English Language-C5,Mathematics-C6,Economics-B3,Biology-C5,Geography-B2,Yoruba-B3,Agricultural Science-C6";
foreach(str_getcsv($str) as $row){
    $values = explode('-', $row);
    $class[] = $values[0];
    $grades[] = $values[1];
}

If you really only want grades use the $grades array. You also can match to the class if needed. Additionally a preg_match could be used on $values[1] if you want to verify some pattern.

Upvotes: 4

Nick
Nick

Reputation: 147266

You can use preg_match_all to extract the grade values, linking them to their subjects in an array:

$str = "English Language-C5,Mathematics-C6,Economics-B3,Biology-C5,Geography-B2,Yoruba-B3,Agricultural Science-C6";

preg_match_all('/([^-,]+)-([^,]+)/', $str, $matches);
$grades = array_combine($matches[1], $matches[2]);
print_r($grades);

Output:

Array
(
    [English Language] => C5
    [Mathematics] => C6
    [Economics] => B3
    [Biology] => C5
    [Geography] => B2
    [Yoruba] => B3
    [Agricultural Science] => C6
)

If you really only want the values, use $grades = $matches[2] instead:

$grades = $matches[2];
print_r($grades);

Output:

Array
(
    [0] => C5
    [1] => C6
    [2] => B3
    [3] => C5
    [4] => B2
    [5] => B3
    [6] => C6
)

Demo on 3v4l.org

Upvotes: 2

smarber
smarber

Reputation: 5084

You can use preg_match_all

$a = "English Language-C5,Mathematics-C6,Economics-B3,Biology-C5,Geography-B2,Yoruba-B3,Agricultural Science-C6";

preg_match_all('#-([BC]\d)#', $a, $res);

if (0 < count($res)) {
    unset($res[0]);
    $res = current($res);
}

print_r($res);

The output :

array ( 0 => 'C5', 1 => 'C6', 2 => 'B3', 3 => 'C5', 4 => 'B2', 5 => 'B3', 6 => 'C6', )

Upvotes: 1

Related Questions