これが可能なアプローチのようになります。
<?php
$input = [
['name' => 'Birthday'],
['name' => 'Marriage Anniversary']
];
$output = [];
array_walk($input, function($set) use (&$output) {
$entry = $set['name'];
$value = preg_replace('/\s+/', '_', strtolower($entry));
$label = $entry;
$output[] = [
'value' => $value,
'label' => $label
];
});
print_r($output);
上記のコードの出力は明らかである:
Array
(
[0] => Array
(
[value] => birthday
[label] => Birthday
)
[1] => Array
(
[value] => marriage_anniversary
[label] => Marriage Anniversary
)
)
あなたの質問は、その番号あなたについて少しは不明です値のうちの1つ(「birthday1」)を後で示唆してください... 本当ににsomが必要な場合
<?php
$input = [
['name' => 'Birthday'],
['name' => 'Marriage Anniversary'],
['name' => 'Birthday']
];
$output = [];
$catalog = [];
array_walk($input, function($set) use (&$catalog, &$output) {
$entry = $set['name'];
$value = preg_replace('/\s+/', '_', strtolower($entry));
$catalog[$value] = isset($catalog[$value]) ? ++$catalog[$value] : 1;
$label = $entry;
$output[] = [
'value' => $value . $catalog[$value],
'label' => $label
];
});
print_r($output);
変更された出力は明らかに次のようになります:
Array
(
[0] => Array
(
[value] => birthday1
[label] => Birthday
)
[1] => Array
(
[value] => marriage_anniversary1
[label] => Marriage Anniversary
)
[2] => Array
(
[value] => birthday2
[label] => Birthday
)
)
はどのように我々は '動的な値を知ることになっている値の出現のためのカウンターの電子ソート、そしてここで上記のコードの修正版です誕生日1、結婚記念日?これらはループされた配列内に存在しますか?また、ループコードを表示してください。 –