如果数组

时间:2015-12-15 15:04:34

标签: php arrays

我有以下多维数组,我需要迭代它,只要有重复的名称,就会在名称中添加一个计数器。

[1] => Array
    (
        [0] => Array
            (
                [clientName] => John Smith
                [clientType] => 0
                [clientDOB] => 1980-10-14
            )

     )

[2] => Array
    (
        [0] => Array
            (
                [clientName] => John Smith
                [clientType] => 0
                [clientDOB] => 1970-01-01
            )

        [1] => Array
            (
                [clientName] => Jeremy White
                [clientType] => 2
                [clientDOB] => 2015-08-19
            )

    )

我使用的代码是:

$finalNames = array_map(function ($item) use (&$namesCount) {
    if (!isset($namesCount[$item['clientName']])) {
        $namesCount[$item['clientName']] = 0;
    }

    $namesCount[$item['clientName']]++;

    $item['clientName'] = $item['clientName'] . ' ' . $namesCount[$item['clientName']];

    return $item;
}, $arrayOfTravellers);

array_map(function($item, $key) use ($namesCount, &$finalNames) {
    $finalNames[$key]['clientName'] = $namesCount[$item['clientName']] == 1
        ? str_replace(' 1', '', $finalNames[$key]['clientName'])
        : $finalNames[$key]['clientName'];
}, $arrayOfNames, array_keys($arrayOfTravellers));

这会返回一堆错误,例如: 注意:未定义的索引:第330行的/Applications/MAMP/htdocs/europatours/functions/reportsFunctions.php中的clientName

我的假设是代码不适合多维数组。有人可以帮忙吗?我只需要在名称重复的地方保留数组的完整结构,添加一个计数器,如John Smith 1,John Smith 2,而Jeremy White仍然没有计数器。

2 个答案:

答案 0 :(得分:0)

$arrayOfNames = array(
    array('clientName' => 'John'),
    array('clientName' => 'John'),
    array('clientName' => 'Mary'),
    array('clientName' => 'Mary'),
    array('clientName' => 'Mary'),
    array('clientName' => 'Tony'),
    array('clientName' => 'Alex')
);

$namesCount = array();

$finalNames = array_map(function ($item) use (&$namesCount) {
    if (!isset($namesCount[$item['clientName']])) {
        $namesCount[$item['clientName']] = 0;
    }

    $namesCount[$item['clientName']]++;

    $item['clientName'] = $item['clientName'] . ' ' . $namesCount[$item['clientName']];

    return $item;
}, $arrayOfNames);

array_map(function($item, $key) use ($namesCount, &$finalNames) {
    $finalNames[$key]['clientName'] = $namesCount[$item['clientName']] == 1
        ? str_replace(' 1', '', $finalNames[$key]['clientName'])
        : $finalNames[$key]['clientName'];
}, $arrayOfNames, array_keys($arrayOfNames));

echo '<pre>';
    var_dump($finalNames);
echo '</pre>';

输出结果为:

array(7) {
  [0]=>
  array(1) {
    ["clientName"]=>
    string(6) "John 1"
  }
  [1]=>
  array(1) {
    ["clientName"]=>
    string(6) "John 2"
  }
  [2]=>
  array(1) {
    ["clientName"]=>
    string(6) "Mary 1"
  }
  [3]=>
  array(1) {
    ["clientName"]=>
    string(6) "Mary 2"
  }
  [4]=>
  array(1) {
    ["clientName"]=>
    string(6) "Mary 3"
  }
  [5]=>
  array(1) {
    ["clientName"]=>
    string(4) "Tony"
  }
  [6]=>
  array(1) {
    ["clientName"]=>
    string(4) "Alex"
  }
}

答案 1 :(得分:0)

添加新值时,array_columnin_array的组合怎么样?

$records = array(); // your data
$name = 'John';

$i = 0;
do
{
    $newName = $name . ( ($i == 0)? '' : ' '.$i );
    $i++;
}
while (in_array($newName, array_column($records, 'clientName')));

$records[] = array('clientName' => $newName);

由于性能问题,您可能只想在循环之前调用array_columns一次,并将结果存储在变量中。