Symfony\Component\Config\Definition\PrototypedArrayNode::setKeyAttribute PHP Method

setKeyAttribute() public method

This is useful when you have an indexed array that should be an associative array. You can select an item from within the array to be the key of the particular item. For example, if "id" is the "key", then: array( array('id' => 'my_name', 'foo' => 'bar'), ); becomes array( 'my_name' => array('foo' => 'bar'), ); If you'd like "'id' => 'my_name'" to still be present in the resulting array, then you can set the second argument of this method to false.
public setKeyAttribute ( string $attribute, boolean $remove = true )
$attribute string The name of the attribute which value is to be used as a key
$remove boolean Whether or not to remove the key
    public function setKeyAttribute($attribute, $remove = true)
    {
        $this->keyAttribute = $attribute;
        $this->removeKeyAttribute = $remove;
    }

Usage Example

 /**
  * Tests the opposite of the testMappedAttributeKeyIsRemoved because
  * the removal can be toggled with an option.
  */
 public function testMappedAttributeKeyNotRemoved()
 {
     $node = new PrototypedArrayNode('root');
     $node->setKeyAttribute('id', false);
     // each item under the root is an array, with two scalar items
     $prototype = new ArrayNode(null, $node);
     $prototype->addChild(new ScalarNode('foo'));
     $prototype->addChild(new ScalarNode('id'));
     // the key attribute will remain
     $node->setPrototype($prototype);
     $children = array();
     $children[] = array('id' => 'item_name', 'foo' => 'bar');
     $normalized = $node->normalize($children);
     $expected = array();
     $expected['item_name'] = array('id' => 'item_name', 'foo' => 'bar');
     $this->assertEquals($expected, $normalized);
 }
All Usage Examples Of Symfony\Component\Config\Definition\PrototypedArrayNode::setKeyAttribute