Skip to content

Instantly share code, notes, and snippets.

@kmkmjhyiiiu
Last active April 30, 2018 21:36
Show Gist options
  • Save kmkmjhyiiiu/83d598d316a0c4576d9bc6b4c804ee4a to your computer and use it in GitHub Desktop.
Save kmkmjhyiiiu/83d598d316a0c4576d9bc6b4c804ee4a to your computer and use it in GitHub Desktop.
<?php
/**
* ------------------------------------------------------------------------------------------
* Pratice array_columns, array_map, array_filter
* ------------------------------------------------------------------------------------------
*
* Simple and stupid but in OOP way to clear my concepts about some useful array functions.
*/
/**
* display subject
*/
function display($sub) {
foreach ($sub as $sub):
echo 'i hope, i can pass: <b>'.$sub.'</b>.<br />';
endforeach;
}
/**
* var dump function
*/
function dd() {
var_dump(func_get_args());
die;
}
/**
* class name
*/
class ExamsPreparation
{
/**
* subjects list
* @var Array
*/
public $subjects;
/**
* do i have any hope that i can clear those given subjects?
* @var bool
*/
public $canClear;
public function __construct(Array $subjects, Bool $canClear)
{
$this->subjects = $subjects;
$this->canClear = $canClear;
}
}
/**
* Exams Preparation object list
*
* @var array
*/
$list = [
new ExamsPreparation(['Urdu', 'Computer'], true),
new ExamsPreparation(['Physics', 'English'], true),
new ExamsPreparation(['Computer', 'Islamiyat'], true),
new ExamsPreparation(['Math'], false)
];
/**
* Array filter to return only "True" conditions
* @var array
*/
$chanceToClear = array_filter($list, function ($d) {
return $d->canClear;
});
/**
* Use Array columns to get only those subjects list :p which can be clear.
* @var array
*/
$getSubjects = array_column($chanceToClear, 'subjects');
/**
* Array Map to call function to echo out subjects.
*/
array_map('display', $getSubjects);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment