Permalink
Commits on Dec 1, 2018
  1. feature #29127 [DomCrawler] Added return of element name in `extract(…

    nicolas-grekas committed Dec 1, 2018
    …)` method (andrey-helldar)
    
    This PR was squashed before being merged into the 4.3-dev branch (closes #29127).
    
    Discussion
    ----------
    
    [DomCrawler] Added return of element name in `extract()` method
    
    | Q             | A
    | ------------- | ---
    | Branch?       | master
    | Bug fix?      | no
    | New feature?  | yes
    | BC breaks?    | no
    | Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files -->
    | Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
    | License       | MIT
    | Doc PR        | symfony/symfony-docs#10646
    
    Situation: you need to get an array of keys and values.
    The current package code does not allow this to be done easily.
    The changes made to the code will allow you to return the required data set.
    ```php
    use Symfony\Component\DomCrawler\Crawler;
    
    $crawler = new Crawler($content);
    
    $crawler
        ->filter('ItemsList > Item')
        ->each(function (Crawler $element) {
            $data = $element
                ->children()
                ->extract(['_name', '_text']);
    
            var_dump($data);
        });
    
    // Result:
    array:2 [
      0 => array:2 [
        0 => "id",
        1 => "1"
      ],
      1 => array:2 [
        0 => "title",
        1 => "Foo Bar"
      ]
    ]
    ```
    
    Commits
    -------
    
    79162c1eb5 [DomCrawler] Added return of element name in `extract()` method