PHP按键的第三个字符开始对数组键进行排序。

huangapple go评论69阅读模式
英文:

PHP sort array keys starting at the 3rd character of the key

问题

以下是您要求的部分翻译:

array(4) {
    [D.Fletcher] = array(22) {
        [games] = int() 2
        [ab] = int() 6
        [runs] = int() 2
        [hits] = int() 2
    }
    [A.Slegers] = array(22) {
        [games] = int() 3
        [ab] = int() 0
        [runs] = int() 0
        [hits] = int() 0
    }
    [A.Eaton] = array(22) {
        [games] = int() 1
        [ab] = int() 2
        [runs] = int() 0
        [hits] = int() 0
    }
    [S.Ohtani] = array(22) {
        [games] = int() 3
        [ab] = int() 6
        [runs] = int() 2
        [hits] = int() 3
    }
}

如果要从键的第三个字符开始排序数组,您可以使用以下 PHP 代码:

$arr = [
    'D.Fletcher' => ['games' => 2, 'ab' => 6, 'runs' => 2, 'hits' => 2],
    'A.Slegers' => ['games' => 3, 'ab' => 0, 'runs' => 0, 'hits' => 0],
    'A.Eaton' => ['games' => 1, 'ab' => 2, 'runs' => 0, 'hits' => 0],
    'S.Ohtani' => ['games' => 3, 'ab' => 6, 'runs' => 2, 'hits' => 3],
];

// 获取所有键
$keys = array_keys($arr);

// 使用 usort 函数按键的第三个字符进行排序
usort($keys, function($a, $b) {
    $a = substr($a, 2);
    $b = substr($b, 2);
    return strcmp($a, $b);
});

// 根据排序后的键重组数组
$sortedArr = [];
foreach ($keys as $key) {
    $sortedArr[$key] = $arr[$key];
}

// 输出排序后的数组
print_r($sortedArr);

这段代码将按键的第三个字符进行排序,得到您期望的结果。

英文:
array(4) {
    [D.Fletcher] = array(22) {
        [games] = int() 2
        [ab] = int() 6
        [runs] = int() 2
        [hits] = int() 2
    }
    [A.Slegers] = array(22) {
        [games] = int() 3
        [ab] = int() 0
        [runs] = int() 0
        [hits] = int() 0
    }
    [A.Eaton] = array(22) {
        [games] = int() 1
        [ab] = int() 2
        [runs] = int() 0
        [hits] = int() 0
    }
    [S.Ohtani] = array(22) {
        [games] = int() 3
        [ab] = int() 6
        [runs] = int() 2
        [hits] = int() 3
    }

I want to be able to sort the array starting at the third character of the key.

If I use ksort, I get:

A.Eaton
A.Slegers
D.Fletcher
S.Ohtani

But I want:

A.Eaton
D.Fletcher
S.Ohtani
A.Slegers

I can do this SQL as follows:
SELECT * FROM batters order by SUBSTRING(name, 3)

But I cannot figure out how to do it with a PHP sort.
I know I can get the keys with

$keys=array_keys($arr);

or in a loop:

foreach ($arr as $key => $value) {
   echo $key;
}

Then possibly do a substr($key, 3) but I can't figure out how to put it all together for a sort.

答案1

得分: 3

以下是代码的翻译部分:

如我在注释中所述

	// 在这里输入你的代码,享受吧!
	$array = [
		"D.Fletcher" => [],
		"A.Slegers" => [],
		"A.Eaton" => []
	];


	uksort($array, function($a,$b){
		return substr($a,2) <=> substr($b,2);
	});

	print_r($array);

输出

    Array
    (
       [A.Eaton] => Array()
       [D.Fletcher] => Array()
       [A.Slegers] => Array()
    )

[test][1]

个人认为,如果我是你,我会这样做:

应该选择在SQL中使用substr的额外字段选择数据

    选择 SUBSTRING(name, 2) as sort_name....


然后,不再在数据库中进行排序,因为性能原因,你现在可以将这些作为数据的键。如果可能的话,最好不要在排序中进行substr,因为这将被多次调用。虽然它非常快,所以这不是什么大问题。但是我避免在数组键中使用`.`,还有其他原因。如果你删除它,可能会有重复的名称,所以很难说。

基本上,我会构建数据,以便不需要这样做,你仍然可以在PHP中进行排序。

也可以按每个嵌套数组中的特定键对此进行多维数组排序,例如,你可以在子数组中包含名称并创建一个按照该名称进行排序的搜索函数。我只是尽量避免频繁调用`substr`。性能方面可能并不是真正的大问题,这只是我的直觉。

[1]: https://onlinephp.io?s=Zc3BCoJAEIDhcwv7DpN0aGFR6KoWQfYCHUVCbUpLdmWcPUj07q1lp3Zu3wz_Jru-6aWIIsgMI8FoHUFtLwgNEmpAc7fjUopVSVSOkEIuBfgXHMJjh1z7qwDSLeSFnhf78NThDWn486xka2aVooilmMY9Bku8_n6g4epMza01HvSqUk8pFoTsyMDgqoFp8o2CxFd-UHnwsZf6FHtqDZ9pDqr4DQ%2C%2C&amp;v=8.2.1
英文:

As I said in the comments

// Enter your code here, enjoy!
$array = [
	&quot;D.Fletcher&quot; =&gt; [],
	&quot;A.Slegers&quot; =&gt; [],
	&quot;A.Eaton&quot; =&gt; []
];


uksort($array, function($a,$b){
	return substr($a,2) &lt;=&gt; substr($b,2);
});

print_r($array);

Output

Array
(
   [A.Eaton] =&gt; Array()
   [D.Fletcher] =&gt; Array()
   [A.Slegers] =&gt; Array()
)

test

Personally what I would do if I was you:

Would be to select the data with an extra field that you substr in SQL

Select SUBSTRING(name, 2) as sort_name ....

Then instead of sorting in the DB, because of performance, you can now just use those as the keys for the data. If it's possible it's better to not do the substr in the sorting because this will be called many times. It's very fast so it's not a big deal. But I avoid using the . in array keys for other reasons. You may have duplicate names if you remove that so it's hard for me to say.

Basically I would build the data so I don't need to do this, you can still sort in PHP.

It's also possible to sort this as a multidimensional array by a specific key in each nested array for example you could include the name in the sub-array and make a search function to sort by that as well.. I would just try to avoid calling substr so much. Performance wise it's probably not really a big deal, that's just my instinct.

答案2

得分: 0

uksort示例起作用了。谢谢你。

FYI. 我在一个不支持PHP 7的代码提示的平台上工作,因此使用太空船操作符<=>会导致代码提示错误。使用strcmp也可以工作。

return strcmp( substr($a,2), substr($b,2) );
英文:

uksort example worked. Thank you.

FYI. I work on a platform that doesn't support code hinting for PHP 7, hence a code hint error using spaceship operator <=>. Using strcmp works as well too.

return strcmp( substr($a,2), substr($b,2) );

huangapple
  • 本文由 发表于 2023年2月8日 23:05:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387706.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定