Preface
Goal: Learning PHP without HTML burden.
Preface
Goal: Continue Part One
4: Data Structure Using Class
Simply tidier.
There is other alternative data structure as well.
Instead of Associative Array
, we can use Class
.
Class
The declaration is simple.
<?php
class MySong {
public function __construct(
public String $title = "",
public Array $tags = []
) {}
}
$song = new MySong(
'Cantaloupe Island',
['60s', 'jazz']
);
var_export($song);
__
WWe can examine the object shown in output result below:
❯ php 12-record-a.php
MySong::__set_state(array(
'title' => 'Cantaloupe Island',
'tags' =>
array (
0 => '60s',
1 => 'jazz',
),
))
If you prefer shorter output you can utilize this code below:
<?php
printf("%s: [%s]\n",
$song->title,
join(', ', $song->tags));
With the result as below:
❯ php 12-record-b.php
Cantaloupe Island: [60s, jazz]
The Songs Structure
We can continue our journey to records using Class
.
A little bit complex, but worth it.
<?php
class MySong {
public function __construct(
public String $title = "",
public Array $tags = []
) {}
}
$songs = [
new MySong(
'Cantaloupe Island', ['60s', 'jazz']),
new MySong('Let It Be', ['60s', 'rock']),
new MySong(
'Knockin\' on Heaven\'s Door',
['70s', 'rock']),
new MySong('Emotion', ['70s', 'pop']),
new MySong('The River'),
];
foreach($songs as $song) {
printf("%s: [%s]\n",
$song->title,
join(', ', $song->tags));
}
__
With the result similar as below record:
❯ php 13-songs.php
Cantaloupe Island: [60s, jazz]
Let It Be: [60s, rock]
Knockin' on Heaven's Door: [70s, rock]
Emotion: [70s, pop]
The River: []
[]
5: Separating Module
Again, we need to reuse the songs record multiple times, so we separate the record structure from logic.
Songs Module
The code can be shown as below:
<?php
class MySong {
public function __construct(
public String $title = "",
public Array $tags = []
) {}
}
$songs = [
new MySong(
'Cantaloupe Island', ['60s', 'jazz']),
new MySong('Let It Be', ['60s', 'rock']),
new MySong(
'Knockin\' on Heaven\'s Door',
['70s', 'rock']),
new MySong('Emotion', ['70s', 'pop']),
new MySong('The River'),
];
__
Using Songs Module
Now we can have a very short code.
<?php
require_once(__DIR__.'/MyClassSongs.php');
foreach($songs as $song) {
printf("%s: [%s]\n",
$song->title,
join(', ', $song->tags));
}
With the result, as below. Exactly the same as previous output result.
❯ php 14-module.php
Cantaloupe Island: [60s, jazz]
Let It Be: [60s, rock]
Knockin' on Heaven's Door: [70s, rock]
Emotion: [70s, pop]
The River: []
[]
3: Finishing The Task
Extract, Flatten, Unique
Extracting Class
Using arrow to access property
<?php
require_once(__DIR__.'/MyClassSongs.php');
$tagss = array_map(
function ($song) { return $song->tags; },
$songs);
printf("%s\n", json_encode($tagss));
With the result is,
still array
of array
as shown below.
❯ php 15-extract.php
[["60s","jazz"],["60s","rock"],["70s","rock"],["70s","pop"],[]]
You can see thw we are uing []
instead of nil
.
Flatten
We still have to flatten, the multi dimensional array,
with this ...
feature.
<?php
require_once(__DIR__.'/MyClassSongs.php');
$tagss = array_map(
function ($song) { return $song->tags; },
$songs);
$tags = array_merge(...$tagss);
printf("%s\n", json_encode($tags));
With the result is,
still array
of array
as shown below.
❯ php 16-flatten.php
["60s","jazz","60s","rock","70s","rock","70s","pop"]
Unique
Advance List Comprehension
Finally we solve unique
list,
after flattening.
<?php
require_once(__DIR__.'/MyClassSongs.php');
$tags =
array_values(
array_unique(
array_merge(
... array_map(
function ($song) {
return $song->tags; },
$songs)
)));
printf("%s\n", json_encode($tags));
With the result similar as below array:
❯ php 17-unique.php
["60s","jazz","rock","70s","pop"]
Good enough.
What is Next 🤔?
We have alternative way to do get unique list. And we will also discuss about concurrency.
Consider continue reading [ PHP - Playing with Records - Part Three ].