LoginSignup
2
0

More than 1 year has passed since last update.

【 #ゆめみからの挑戦状 ★第5弾】をやってみた

Last updated at Posted at 2022-09-08

お題はこちら

ksort にオプション付けられるの知らんかった^^;

パクったら4行 → 3行になった!

<?php
$in = [
    ['2nd' => 'two', 'four' => '4th'],
    'three' => '3rd',
    ['one' => '1st'],
    '10th' => 'ten',
    ['6th' => 'six'],
    '5th' => 'five',
    'seven' => '7th',
    ['fourteen' => '14th', '11th' => 'eleven'],
    ['8th' => 'eight'],
    'thirteen' => '13th',
    '12th' => 'twelve',
    'nine' => '9th',
    ['15th' => 'fifteen'],
];

$ans = [
    '1st' => 'one',
    '2nd' => 'two',
    '3rd' => 'three',
    '4th' => 'four',
    '5th' => 'five',
    '6th' => 'six',
    '7th' => 'seven',
    '8th' => 'eight',
    '9th' => 'nine',
    '10th' => 'ten',
    '11th' => 'eleven',
    '12th' => 'twelve',
    '13th' => 'thirteen',
    '14th' => 'fourteen',
    '15th' => 'fifteen',
];

preg_match_all('/"(\d{1,2}..)":"([^"]+?)"|"([^"]+?)":"(\d{1,2}..)"/', json_encode($in), $m);
$list = array_merge(array_combine(array_filter($m[1]), array_filter($m[2])), array_combine(array_filter($m[4]), array_filter($m[3])));
ksort($list, SORT_NUMERIC );
var_export($list === $ans);//true

4行が最小じゃね?って事だったんで多分最小w

2
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0