1.2.276. Could Be array_combine()

This rule suggests using the native function array_combine() to merge two arrays into a hash. array_combine() takes the keys and the values from two distinct arrays, and merge them into one.

<?php

$keys = [1, 2, 3];
$values = ['a', 'b', 'c'];
$destination = [];
foreach($keys as $k => $v) {
     $destination[$v] = $values[$k];
}

$destination = [1 => 'a', 2 => 'b', 3 => 'c'];

$destination = array_combine($keys, $values);

?>

See also How to use array_merge() and array_combine() in PHP ?.

1.2.276.1. Suggestions

  • Use array_combine().

1.2.276.2. Specs

Short name

Structures/CouldBeArrayCombine

Rulesets

All, Changed Behavior, Suggestions

Exakat since

2.5.3

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Medium

Available in

Entreprise Edition, Exakat Cloud