"Fossies" - the Fresh Open Source Software Archive

Member "papayacms-core-6.9.4/src/system/Papaya/Utility/ArrayMapper.php" (13 Dec 2019, 1968 Bytes) of package /linux/www/papayacms-core-6.9.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ArrayMapper.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 /**
    3  * papaya CMS
    4  *
    5  * @copyright 2000-2018 by papayaCMS project - All rights reserved.
    6  * @link http://www.papaya-cms.com/
    7  * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License, version 2
    8  *
    9  *  You can redistribute and/or modify this script under the terms of the GNU General Public
   10  *  License (GPL) version 2, provided that the copyright and license notes, including these
   11  *  lines, remain unmodified. papaya is distributed in the hope that it will be useful, but
   12  *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   13  *  FOR A PARTICULAR PURPOSE.
   14  */
   15 namespace Papaya\Utility;
   16 
   17 /**
   18  * Map values of an array into another array.
   19  *
   20  * @package Papaya-Library
   21  * @subpackage Util
   22  */
   23 class ArrayMapper {
   24   /**
   25    * Target array uses teh same keys, the values are array elements, the subelement specified
   26    * by $indexName is used in the result.
   27    *
   28    * If you provide an array to one of the index arguments, it is treated as a list of identifers
   29    * the first found element is used.
   30    *
   31    * If the $elementIndex is NULL the full array element is added to the result.
   32    *
   33    * If the $keyIndex is NULL, the key from the original array is used
   34    *
   35    * @param array|\Traversable $array
   36    * @param string|int|array $elementIndex
   37    * @param string|int|array $keyIndex
   38    *
   39    * @return array
   40    */
   41   public static function byIndex($array, $elementIndex = NULL, $keyIndex = NULL) {
   42     Constraints::assertArrayOrTraversable($array);
   43     $result = [];
   44     foreach ($array as $key => $value) {
   45       if (NULL !== $keyIndex) {
   46         $key = Arrays::get($value, $keyIndex, NULL);
   47       }
   48       if (NULL !== $elementIndex) {
   49         $value = Arrays::get($value, $elementIndex, NULL);
   50       }
   51       if (NULL !== $value) {
   52         if (NULL !== $key) {
   53           $result[$key] = $value;
   54         } else {
   55           $result[] = $value;
   56         }
   57       }
   58     }
   59     return $result;
   60   }
   61 }