<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PHP &#8211; Aimeos</title>
	<atom:link href="https://aimeos.org/tips/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>https://aimeos.org/tips</link>
	<description>ultra fast PHP e-commerce framework</description>
	<lastBuildDate>Tue, 23 Jul 2024 07:31:10 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.7.8</generator>

<image>
	<url>https://aimeos.org/tips/wp-content/uploads/2019/09/Aimeos_e_200-100x104.png</url>
	<title>PHP &#8211; Aimeos</title>
	<link>https://aimeos.org/tips</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Aimeos 2023.10 LTS release</title>
		<link>https://aimeos.org/tips/aimeos-2023-10-lts-release/</link>
					<comments>https://aimeos.org/tips/aimeos-2023-10-lts-release/#comments</comments>
		
		<dc:creator><![CDATA[aimeos]]></dc:creator>
		<pubDate>Wed, 25 Oct 2023 08:23:40 +0000</pubDate>
				<category><![CDATA[e-commerce]]></category>
		<category><![CDATA[Laravel]]></category>
		<category><![CDATA[Releases]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[TYPO3]]></category>
		<category><![CDATA[ecommerce]]></category>
		<category><![CDATA[laravel]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[release]]></category>
		<guid isPermaLink="false">https://aimeos.org/tips/?p=1005</guid>

					<description><![CDATA[The 2023.10 version of the Aimeos e-commerce framework for Laravel and TYPO3 is available now! Especially developers will love the 2023 version because it contains a lot of simplifications and fully supports scaleable cloud setups like Kubernetes natively. The most<span class="ellipsis">&#8230;</span><div class="read-more"><a href="https://aimeos.org/tips/aimeos-2023-10-lts-release/">Read more <span class="screen-reader-text">Aimeos 2023.10 LTS release</span><span class="meta-nav"> &#8250;</span></a></div><!-- end of .read-more -->]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-image size-large"><a href="https://aimeos.org/tips/wp-content/uploads/2021/10/aimeos-release-rect-big.jpg"><img width="1024" height="538" src="https://aimeos.org/tips/wp-content/uploads/2021/10/aimeos-release-rect-big-1024x538.jpg" alt="" class="wp-image-887" srcset="https://aimeos.org/tips/wp-content/uploads/2021/10/aimeos-release-rect-big-1024x538.jpg 1024w, https://aimeos.org/tips/wp-content/uploads/2021/10/aimeos-release-rect-big-300x158.jpg 300w, https://aimeos.org/tips/wp-content/uploads/2021/10/aimeos-release-rect-big-768x403.jpg 768w, https://aimeos.org/tips/wp-content/uploads/2021/10/aimeos-release-rect-big-100x53.jpg 100w, https://aimeos.org/tips/wp-content/uploads/2021/10/aimeos-release-rect-big-150x79.jpg 150w, https://aimeos.org/tips/wp-content/uploads/2021/10/aimeos-release-rect-big-200x105.jpg 200w, https://aimeos.org/tips/wp-content/uploads/2021/10/aimeos-release-rect-big-450x236.jpg 450w, https://aimeos.org/tips/wp-content/uploads/2021/10/aimeos-release-rect-big-600x315.jpg 600w, https://aimeos.org/tips/wp-content/uploads/2021/10/aimeos-release-rect-big-900x473.jpg 900w, https://aimeos.org/tips/wp-content/uploads/2021/10/aimeos-release-rect-big.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></figure>



<p>The 2023.10 version of the Aimeos e-commerce framework for Laravel and TYPO3 is available now! Especially developers will love the 2023 version because it contains a lot of simplifications and fully supports scaleable cloud setups like Kubernetes natively. The most important updates in 2023 are:</p>



<ul><li>Laravel 10 distributions</li><li>TYPO3 12 support</li><li>Kubernetes/Serverless support</li><li>Create managers easily</li><li>Merged order and order base</li><li>Stored basket panel</li><li>DB-based translations for type names</li><li>VueJS and GraphQL in backend</li></ul>



<span id="more-1005"></span>



<h2>Laravel 10 distributions</h2>



<div class="wp-block-image"><figure class="alignright size-large"><a href="https://aimeos.org/tips/wp-content/uploads/2021/10/laravel6.png"><img width="126" height="130" src="https://aimeos.org/tips/wp-content/uploads/2021/10/laravel6.png" alt="" class="wp-image-863" srcset="https://aimeos.org/tips/wp-content/uploads/2021/10/laravel6.png 126w, https://aimeos.org/tips/wp-content/uploads/2021/10/laravel6-100x103.png 100w" sizes="(max-width: 126px) 100vw, 126px" /></a></figure></div>



<p>Since the Laravel 10 release in February 2023, Aimeos fully supports the latest Laravel version and since 2023.04, the Aimeos headless and full stack distribution are based on Laravel 10. You can bootstrap your own Aimeos application in less than five minutes using those distributions and get a fully working Laravel 10 application. This also includes pre-configured authentication setup for both, the full stack and the headless distribution.</p>



<h2>TYPO3 12 support</h2>



<div class="wp-block-image"><figure class="alignleft size-large"><a href="https://aimeos.org/tips/wp-content/uploads/2021/10/typo3.png"><img width="128" height="128" src="https://aimeos.org/tips/wp-content/uploads/2021/10/typo3.png" alt="" class="wp-image-864" srcset="https://aimeos.org/tips/wp-content/uploads/2021/10/typo3.png 128w, https://aimeos.org/tips/wp-content/uploads/2021/10/typo3-100x100.png 100w" sizes="(max-width: 128px) 100vw, 128px" /></a></figure></div>



<p>Also, Aimeos 2023.x fully supports TYPO3 12.4 since it&#8217;s release in April. Many changes has happened in v12 compared to v11, especially a lot of deprecated code has been removed. This made it very hard to make Aimeos 2023.x available for TYPO3 v11 too so we&#8217;ve decided to support v12 only in that release. Therefore, if you need a feature from Aimeos 2023.x or want to upgrade after two years of free support, you must upgrade to the latest TYPO3 v12 too.</p>



<h2>Kubernetes/Serverless support</h2>



<p>We are proud to announce that Aimeos 2023+ is the first cloud-native e-commerce solution for Laravel. Everything including file imports can be distributed across any cloud environment like Kubernetes, AWS, Google Cloud, Azure or so called serverless environments now. Aimeos for Laravel is cloud-ready since the beginning and images/CSS/JS files can be stored in S3 or similar cloud storage services but files were read only from a local file system. This this year, you can store files for importing products, categories, users, etc. from any remote server regardless if they are in CSV or XML format.</p>



<p>We also moved our complete server infrastructure from traditional hosting to a private Kubernetes cloud beginning this year including the web sites, demo setups and private package repository.</p>



<h2>Create managers easily</h2>



<p>Extending existing managers is extremely simply but writing new managers for own data domains or extending existing data domains required a lot of boilerplate code in the past. Since 2023.10, creating new managers is as easy as extending managers now.</p>



<p>First of all, create a setup task for the database migration as before:</p>



<pre class="wp-block-code"><code>namespace Aimeos\Upscheme\Task;

class Test extends Base
{
    public function up()
    {
        $this-&gt;info( 'Creating test schema', 'v' );

        $this-&gt;db( 'db-test' )-&gt;table( $name, function( $table ) {
            $table-&gt;engine = 'InnoDB';

             $table-&gt;id()-&gt;primary( 'pk_mstes_id' );
             $table-&gt;string( 'siteid' );
             $table-&gt;string( 'label' )-&gt;default( '' );
             $table-&gt;int( 'position' )-&gt;default( 0 );
             $table-&gt;smallint( 'status' )-&gt;default( 1 );
             $table-&gt;meta();
         } );
    }
}</code></pre>



<p>Then, create the manager for the new domain named &#8220;test&#8221;:</p>



<pre class="wp-block-code"><code>namespace Aimeos\MShop\Test\Manager;

class Standard
    extends \Aimeos\MShop\Common\Manager\Base
    implements \Aimeos\MShop\Common\Manager\Iface
{
    public function getSaveAttributes() : array
    {
        return $this-&gt;createAttributes( &#91;
            'label' =&gt; &#91;
            ],
            'status' =&gt; &#91;
                'type' =&gt; 'int',
            ],
            'position' =&gt; &#91;
                'type' =&gt; 'int',
                'label' =&gt; 'Position for sorting'
            ],
        ] );
    }
}</code></pre>



<p>That&#8217;s all! You can work with your new manager like with any other manager from the Aimeos core:</p>



<pre class="wp-block-code"><code>$manager = \Aimeos\MShop::create( $this-&gt;context(), 'test' )

$item = $manager-&gt;create()
    -&gt;set( 'label', 'test label' )
    -&gt;set( 'position', 2 )
    -&gt;set( 'status', 1 );

$item = $manager-&gt;save( $item );

$label = $item-&gt;label;
// or using get() with default value
$label = $item-&gt;get( 'label', 'default value' );</code></pre>



<p>For more information, there&#8217;s an article about <a href="https://aimeos.org/docs/latest/models/create-managers/">creating new managers</a> in the Aimeos documentation.</p>



<h2>Merged order and order base</h2>



<p>The biggest architectural change since the beginning is simplifying the order data domain by merging the order and order base tables/managers/items. The initial idea behind was that we can have several invoice/refund entries for each stored order base item (=basket) but that turned out to not work well over the years and complicated handling orders a lot. Thus, the data is all stored in the order record now.</p>



<p>That leads to some consequences, especially for the JSON:API as this change isn&#8217;t backward compatible! An application using the JSON:API doesn&#8217;t need to use the order endpoint any more to create an order item because the basket endpoint did that already. Furthermore, the property names in the basket changed and &#8220;.base&#8221; has to be removed.</p>



<p>The JSON:API has been backward compatible for six years (2017-2022) and we want to keep v2 stable for the next 5-10 years now.</p>



<h2>Stored basket panel</h2>



<div class="wp-block-image"><figure class="alignright size-medium"><a href="https://aimeos.org/tips/wp-content/uploads/2023/10/saved-basket.png"><img width="300" height="180" src="https://aimeos.org/tips/wp-content/uploads/2023/10/saved-basket-300x180.png" alt="" class="wp-image-1009" srcset="https://aimeos.org/tips/wp-content/uploads/2023/10/saved-basket-300x180.png 300w, https://aimeos.org/tips/wp-content/uploads/2023/10/saved-basket-1024x614.png 1024w, https://aimeos.org/tips/wp-content/uploads/2023/10/saved-basket-768x461.png 768w, https://aimeos.org/tips/wp-content/uploads/2023/10/saved-basket-1536x922.png 1536w, https://aimeos.org/tips/wp-content/uploads/2023/10/saved-basket-100x60.png 100w, https://aimeos.org/tips/wp-content/uploads/2023/10/saved-basket-150x90.png 150w, https://aimeos.org/tips/wp-content/uploads/2023/10/saved-basket-200x120.png 200w, https://aimeos.org/tips/wp-content/uploads/2023/10/saved-basket-450x270.png 450w, https://aimeos.org/tips/wp-content/uploads/2023/10/saved-basket-600x360.png 600w, https://aimeos.org/tips/wp-content/uploads/2023/10/saved-basket-900x540.png 900w, https://aimeos.org/tips/wp-content/uploads/2023/10/saved-basket.png 2000w" sizes="(max-width: 300px) 100vw, 300px" /></a></figure></div>



<p>Customer baskets are stored in the database during some time and now, the admin backend contains a new &#8220;Sales &gt; Baskets&#8221; Panel in the admin backend. Administrators and editors can check saved and abandoned baskets of the customers and it&#8217;s also possible to send emails to 3rd party applications for reminding customers about their abandoned carts.</p>



<h2>DB-based translations for type names</h2>



<div class="wp-block-image"><figure class="alignleft size-medium"><a href="https://aimeos.org/tips/wp-content/uploads/2023/10/type-translations.png"><img width="300" height="180" src="https://aimeos.org/tips/wp-content/uploads/2023/10/type-translations-300x180.png" alt="" class="wp-image-1011" srcset="https://aimeos.org/tips/wp-content/uploads/2023/10/type-translations-300x180.png 300w, https://aimeos.org/tips/wp-content/uploads/2023/10/type-translations-1024x614.png 1024w, https://aimeos.org/tips/wp-content/uploads/2023/10/type-translations-768x461.png 768w, https://aimeos.org/tips/wp-content/uploads/2023/10/type-translations-1536x922.png 1536w, https://aimeos.org/tips/wp-content/uploads/2023/10/type-translations-100x60.png 100w, https://aimeos.org/tips/wp-content/uploads/2023/10/type-translations-150x90.png 150w, https://aimeos.org/tips/wp-content/uploads/2023/10/type-translations-200x120.png 200w, https://aimeos.org/tips/wp-content/uploads/2023/10/type-translations-450x270.png 450w, https://aimeos.org/tips/wp-content/uploads/2023/10/type-translations-600x360.png 600w, https://aimeos.org/tips/wp-content/uploads/2023/10/type-translations-900x540.png 900w, https://aimeos.org/tips/wp-content/uploads/2023/10/type-translations.png 2000w" sizes="(max-width: 300px) 100vw, 300px" /></a></figure></div>



<p>Before, translations for attribute types were only possible by translating them statically the Gettext translation files or in the Aimeos configuration. Thus, only developers were able to update these translations and it was difficult to update translations if they have been delivered by ERP systems.</p>



<p>Now, type translations are saved in the database along with the types and can be edited in the admin backend and updated easily by ERP systems. This is available for all types, not only attribute types.</p>



<h2>VueJS and GraphQL in backend</h2>



<p>Several panels have been rewritten in the admin backend to use VueJS components now. This lead to better code quality and is another step forward to remove all jQuery-related code in the next versions. Also, VueJS components for which no VueJS 3 version is available has been replaced by better supported ones, namely the combo box select components which have been replaced by Vue Multiselect now. Finally, this will make the upgrade to VueJS 3 possible in the next versions.</p>



<p>Furthermore, requests to the JSON admin API has been replaced by requests to the new GraphQL API where possible. We will continue to extend the GraphQL API and replace existing JSON admin API requests until we completely phased out the JSON admin API. In 2024.x, the JSON admin API will be also marked as deprecated. The frontend JSON:API will never be replaced and will be the only way for frontends to interact with Aimeos servers as GraphQL is not suited well for read-heavy applications like e-commerce apps.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://aimeos.org/tips/aimeos-2023-10-lts-release/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>PHP: Array &gt; Map 2.0</title>
		<link>https://aimeos.org/tips/php-array-map-2-0/</link>
					<comments>https://aimeos.org/tips/php-array-map-2-0/#respond</comments>
		
		<dc:creator><![CDATA[aimeos]]></dc:creator>
		<pubDate>Wed, 10 Feb 2021 11:23:33 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[collection]]></category>
		<category><![CDATA[map]]></category>
		<guid isPermaLink="false">https://aimeos.org/tips/?p=751</guid>

					<description><![CDATA[The new 2.0 major release of the PHP map package is available which includes new methods and support for working with multi-dimensional arrays in an easy way. Additionally, it comes with the best documentation ever so you find everything you<span class="ellipsis">&#8230;</span><div class="read-more"><a href="https://aimeos.org/tips/php-array-map-2-0/">Read more <span class="screen-reader-text">PHP: Array > Map 2.0</span><span class="meta-nav"> &#8250;</span></a></div><!-- end of .read-more -->]]></description>
										<content:encoded><![CDATA[
<div class="wp-block-image"><figure class="aligncenter size-large"><a href="https://aimeos.org/tips/wp-content/uploads/2021/02/php-map-big.png"><img width="1024" height="542" src="https://aimeos.org/tips/wp-content/uploads/2021/02/php-map-big-1024x542.png" alt="" class="wp-image-752" srcset="https://aimeos.org/tips/wp-content/uploads/2021/02/php-map-big-1024x542.png 1024w, https://aimeos.org/tips/wp-content/uploads/2021/02/php-map-big-300x159.png 300w, https://aimeos.org/tips/wp-content/uploads/2021/02/php-map-big-768x406.png 768w, https://aimeos.org/tips/wp-content/uploads/2021/02/php-map-big-100x53.png 100w, https://aimeos.org/tips/wp-content/uploads/2021/02/php-map-big-150x79.png 150w, https://aimeos.org/tips/wp-content/uploads/2021/02/php-map-big-200x106.png 200w, https://aimeos.org/tips/wp-content/uploads/2021/02/php-map-big-450x238.png 450w, https://aimeos.org/tips/wp-content/uploads/2021/02/php-map-big-600x318.png 600w, https://aimeos.org/tips/wp-content/uploads/2021/02/php-map-big-900x476.png 900w, https://aimeos.org/tips/wp-content/uploads/2021/02/php-map-big.png 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></figure></div>



<p>The new 2.0 major release of the PHP map package is available which includes new methods and support for working with multi-dimensional arrays in an easy way. Additionally, it comes with the best documentation ever so you find everything you need quickly!</p>



<p>Instead of applying independent PHP functions on arrays, use the PHP Map object to stream array data through several methods to transform, order and shorten its elements until the result exactly conforms to your requirements, for example:</p>





<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="re0">$data</span> <span class="sy0">=</span> <span class="br0">&#91;</span>
    <span class="br0">&#91;</span><span class="st_h">'name'</span> <span class="sy0">=&gt;</span> <span class="st_h">'Tom'</span><span class="sy0">,</span> <span class="st_h">'dep'</span> <span class="sy0">=&gt;</span> <span class="st_h">'Dev'</span><span class="sy0">,</span> <span class="st_h">'qty'</span> <span class="sy0">=&gt;</span> <span class="nu0">30</span><span class="br0">&#93;</span><span class="sy0">,</span>
    <span class="br0">&#91;</span><span class="st_h">'name'</span> <span class="sy0">=&gt;</span> <span class="st_h">'Bob'</span><span class="sy0">,</span> <span class="st_h">'dep'</span> <span class="sy0">=&gt;</span> <span class="st_h">'Sales'</span><span class="sy0">,</span> <span class="st_h">'qty'</span> <span class="sy0">=&gt;</span> <span class="nu0">50</span><span class="br0">&#93;</span><span class="sy0">,</span>
    <span class="br0">&#91;</span><span class="st_h">'name'</span> <span class="sy0">=&gt;</span> <span class="st_h">'Joe'</span><span class="sy0">,</span> <span class="st_h">'dep'</span> <span class="sy0">=&gt;</span> <span class="st_h">'Dev'</span><span class="sy0">,</span> <span class="st_h">'qty'</span> <span class="sy0">=&gt;</span> <span class="nu0">10</span><span class="br0">&#93;</span>
<span class="br0">&#93;</span><span class="sy0">;</span>
&nbsp;
<span class="co1">// get the person with the highest quantity in &quot;Dev&quot; department</span>
<span class="re0">$name</span> <span class="sy0">=</span> map<span class="br0">&#40;</span> <span class="re0">$data</span> <span class="br0">&#41;</span>
    <span class="sy0">-&gt;</span><span class="me1">groupBy</span><span class="br0">&#40;</span> <span class="st_h">'dep'</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">only</span><span class="br0">&#40;</span> <span class="st_h">'Dev'</span> <span class="br0">&#41;</span>
    <span class="sy0">-&gt;</span><span class="me1">flat</span><span class="br0">&#40;</span> <span class="nu0">1</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">col</span><span class="br0">&#40;</span> <span class="st_h">'name'</span><span class="sy0">,</span> <span class="st_h">'qty'</span> <span class="br0">&#41;</span>
    <span class="sy0">-&gt;</span><span class="kw3">ksort</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">last</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>






<p>To get the result in one statement is easier and much more elegant than the traditional way in PHP. The PHP Map object is perfectly suited to work on result sets returned by relational databases. You can transform them until you get a collection that exactly fits for your template generating the HTML for the frontend.</p>



<p>The PHP Map package combines the best from Laravel and CakePHP collections, jQuery, Backbone.js, and Lodash in an easy to use PHP package for every PHP application.</p>



<h2>What&#8217;s new in PHP Map 2.0</h2>



<p>One of the biggest new feature is the ability to pass a path of keys as parameter to several PHP Map methods to work on nested arrays directly. Instead of writing own code for diving into multi-dimensional arrays, you can now use the available methods to e.g. get a value by using:</p>





<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1">map<span class="br0">&#40;</span> <span class="br0">&#91;</span><span class="st_h">'key'</span> <span class="sy0">=&gt;</span> <span class="br0">&#91;</span><span class="st_h">'to'</span> <span class="sy0">=&gt;</span> <span class="br0">&#91;</span><span class="st_h">'value'</span> <span class="sy0">=&gt;</span> <span class="st_h">'yes!'</span><span class="br0">&#93;</span><span class="br0">&#93;</span><span class="br0">&#93;</span> <span class="br0">&#41;</span>
    <span class="sy0">-&gt;</span><span class="me1">get</span><span class="br0">&#40;</span> <span class="st_h">'key/to/value'</span> <span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// returns &quot;yes!&quot;</span></pre></div></div></div></div></div></div></div>






<p></p>



<p>This also works for more advanced methods like <code>col()</code> to reindex an array by using a nested key:</p>





<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="re0">$data</span> <span class="sy0">=</span> <span class="br0">&#91;</span>
    <span class="br0">&#91;</span><span class="st_h">'name'</span> <span class="sy0">=&gt;</span> <span class="st_h">'Tom'</span><span class="sy0">,</span> <span class="st_h">'addr'</span> <span class="sy0">=&gt;</span> <span class="br0">&#91;</span><span class="st_h">'zip'</span> <span class="sy0">=&gt;</span> <span class="st_h">'12AB'</span><span class="sy0">,</span> <span class="st_h">'city'</span> <span class="sy0">=&gt;</span> <span class="st_h">'NY'</span><span class="br0">&#93;</span><span class="br0">&#93;</span><span class="sy0">,</span>
    <span class="br0">&#91;</span><span class="st_h">'name'</span> <span class="sy0">=&gt;</span> <span class="st_h">'Bob'</span><span class="sy0">,</span> <span class="st_h">'addr'</span> <span class="sy0">=&gt;</span> <span class="br0">&#91;</span><span class="st_h">'zip'</span> <span class="sy0">=&gt;</span> <span class="st_h">'23WD'</span><span class="sy0">,</span> <span class="st_h">'city'</span> <span class="sy0">=&gt;</span> <span class="st_h">'LA'</span><span class="br0">&#93;</span><span class="br0">&#93;</span>
<span class="br0">&#93;</span><span class="sy0">;</span>
&nbsp;
<span class="re0">$map</span> <span class="sy0">=</span> map<span class="br0">&#40;</span> <span class="re0">$data</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">col</span><span class="br0">&#40;</span> <span class="st_h">'name'</span><span class="sy0">,</span> <span class="st_h">'addr/zip'</span> <span class="br0">&#41;</span><span class="sy0">;</span>
<span class="co1">// ['12AB' =&gt; 'Tom', '23WD' =&gt; 'Bob']</span></pre></div></div></div></div></div></div></div>






<p>By default, a dot (&#8220;/&#8221;) is used as seperator between the array key names but you can change that for the current map using the <code>sep()</code> method:</p>





<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="re0">$map</span> <span class="sy0">=</span> map<span class="br0">&#40;</span> <span class="re0">$data</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">sep</span><span class="br0">&#40;</span> <span class="st_h">'.'</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">col</span><span class="br0">&#40;</span> <span class="st_h">'name'</span><span class="sy0">,</span> <span class="st_h">'addr.zip'</span> <span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>






<p>If you want to use another seperator in all new PHP Map objects (existing ones will keep their separator), you can change the delimiter globally with the <code>delimiter()</code> method:</p>





<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1">Map<span class="sy0">::</span><span class="me2">delimiter</span><span class="br0">&#40;</span> <span class="st_h">','</span> <span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>






<p>It also returns the delimiter used before if you want to revert the used delimiter later in your code.</p>



<h2>Breaking changes in PHP Map 2.0</h2>



<p>The new major version contains a few breaking changes to avoid potential problems that came up in the past. You won&#8217;t be affected by most of them but you should have a look to prevent unexpected behaviour.</p>



<h3>New slash separator</h3>



<p>The most important change affects parameter values including slashes because they are now treated as path separators for nested arrays.</p>





<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="re0">$data</span> <span class="sy0">=</span> <span class="br0">&#91;</span>
    <span class="st_h">'item/id'</span> <span class="sy0">=&gt;</span> <span class="nu0">1</span><span class="sy0">,</span>
    <span class="st_h">'item/code'</span> <span class="sy0">=&gt;</span> <span class="st_h">'test'</span><span class="sy0">,</span>
    <span class="st_h">'item/name'</span> <span class="sy0">=&gt;</span> <span class="st_h">'Test'</span>
<span class="br0">&#93;</span><span class="sy0">;</span>
&nbsp;
map<span class="br0">&#40;</span> <span class="re0">$data</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">col</span><span class="br0">&#40;</span> <span class="st_h">'item/code'</span><span class="sy0">,</span> <span class="st_h">'item/id'</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="co1">// 1.x</span>
<span class="br0">&#91;</span><span class="nu0">1</span> <span class="sy0">=&gt;</span> <span class="st_h">'test'</span><span class="br0">&#93;</span>
&nbsp;
<span class="co1">// 2.x</span>
<span class="br0">&#91;</span><span class="br0">&#93;</span>
<span class="co1">// use instead</span>
map<span class="br0">&#40;</span> <span class="re0">$data</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">sep</span><span class="br0">&#40;</span> <span class="st_h">'.'</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">col</span><span class="br0">&#40;</span> <span class="st_h">'item/code'</span><span class="sy0">,</span> <span class="st_h">'item/id'</span> <span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>






<h3>jQuery style method calls</h3>



<p>You can call methods of objects in a map like this:</p>





<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="co1">// MyClass implements setStatus() (returning $this) and</span>
<span class="co1">// getCode() (initialized by constructor)</span>
&nbsp;
<span class="re0">$map</span> <span class="sy0">=</span> Map<span class="sy0">::</span><span class="me2">from</span><span class="br0">&#40;</span> <span class="br0">&#91;</span>
    <span class="st_h">'a'</span> <span class="sy0">=&gt;</span> <span class="kw2">new</span> MyClass<span class="br0">&#40;</span> <span class="st_h">'x'</span> <span class="br0">&#41;</span><span class="sy0">,</span>
    <span class="st_h">'b'</span> <span class="sy0">=&gt;</span> <span class="kw2">new</span> MyClass<span class="br0">&#40;</span> <span class="st_h">'y'</span> <span class="br0">&#41;</span>
<span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
<span class="re0">$map</span><span class="sy0">-&gt;</span><span class="me1">setStatus</span><span class="br0">&#40;</span> <span class="nu0">1</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">getCode</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">toArray</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>






<p>Before, the objects had been checked if they really implement the <code>setStatus()</code> and <code>getCode()</code> methods.</p>



<p>To avoid returning an empty map if the method name is wrong or the called method is catched by the <code>__call()</code> magic method, this isn’t the case any more. If the method isn’t implemented by all objects, PHP generates a fatal error now for better error tracing.</p>



<h3>Second equals() parameter</h3>



<p>The <code>$assoc</code> parameter of the&nbsp;<a href="https://php-map.org/#equals"><code>equals()</code></a>&nbsp;method to compare keys too has been removed and now, you must use the&nbsp;<a href="https://php-map.org/#is"><code>is()</code></a>&nbsp;method instead:</p>





<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="co1">// 1.x</span>
map<span class="br0">&#40;</span> <span class="br0">&#91;</span><span class="st_h">'one'</span> <span class="sy0">=&gt;</span> <span class="nu0">1</span><span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">equals</span><span class="br0">&#40;</span> <span class="br0">&#91;</span><span class="st_h">'one'</span> <span class="sy0">=&gt;</span> <span class="nu0">1</span><span class="br0">&#93;</span><span class="sy0">,</span> <span class="kw4">true</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="co1">// 2.x</span>
map<span class="br0">&#40;</span> <span class="br0">&#91;</span><span class="st_h">'one'</span> <span class="sy0">=&gt;</span> <span class="nu0">1</span><span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">is</span><span class="br0">&#40;</span> <span class="br0">&#91;</span><span class="st_h">'one'</span> <span class="sy0">=&gt;</span> <span class="nu0">1</span><span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>






<h3>New find() argument</h3>



<p>The&nbsp;<a href="https://php-map.org/#find"><code>find()</code></a>&nbsp;method now accepts a default value or exception object as second argument and the&nbsp;<code>$reverse</code>&nbsp;argument is at the third position now:</p>





<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="co1">// 1.x</span>
Map<span class="sy0">::</span><span class="me2">from</span><span class="br0">&#40;</span> <span class="br0">&#91;</span><span class="st_h">'a'</span><span class="sy0">,</span> <span class="st_h">'c'</span><span class="sy0">,</span> <span class="st_h">'e'</span><span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">find</span><span class="br0">&#40;</span> <span class="kw2">function</span><span class="br0">&#40;</span> <span class="re0">$value</span><span class="sy0">,</span> <span class="re0">$key</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw1">return</span> <span class="re0">$value</span> <span class="sy0">&gt;=</span> <span class="st_h">'b'</span><span class="sy0">;</span>
<span class="br0">&#125;</span><span class="sy0">,</span> <span class="kw4">true</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="co1">// 2.x</span>
Map<span class="sy0">::</span><span class="me2">from</span><span class="br0">&#40;</span> <span class="br0">&#91;</span><span class="st_h">'a'</span><span class="sy0">,</span> <span class="st_h">'c'</span><span class="sy0">,</span> <span class="st_h">'e'</span><span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">find</span><span class="br0">&#40;</span> <span class="kw2">function</span><span class="br0">&#40;</span> <span class="re0">$value</span><span class="sy0">,</span> <span class="re0">$key</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw1">return</span> <span class="re0">$value</span> <span class="sy0">&gt;=</span> <span class="st_h">'b'</span><span class="sy0">;</span>
<span class="br0">&#125;</span><span class="sy0">,</span> <span class="kw4">null</span><span class="sy0">,</span> <span class="kw4">true</span> <span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>






<h3>Semantic change in groupBy()</h3>



<p>Before, items have been grouped by the key string itself if the key passed to&nbsp;<a href="https://php-map.org/#groupby"><code>groupBy()</code></a>&nbsp;didn’t exist. To offer easier checking and sorting of the keys, an empty string is used as key now:</p>





<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1">Map<span class="sy0">::</span><span class="me2">from</span><span class="br0">&#40;</span> <span class="br0">&#91;</span>
    <span class="nu0">10</span> <span class="sy0">=&gt;</span> <span class="br0">&#91;</span><span class="st_h">'aid'</span> <span class="sy0">=&gt;</span> <span class="nu0">123</span><span class="sy0">,</span> <span class="st_h">'code'</span> <span class="sy0">=&gt;</span> <span class="st_h">'x-abc'</span><span class="br0">&#93;</span><span class="sy0">,</span>
<span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">groupBy</span><span class="br0">&#40;</span> <span class="st_h">'xid'</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="co1">// 1.x</span>
<span class="br0">&#91;</span>
    <span class="st_h">'xid'</span> <span class="sy0">=&gt;</span> <span class="br0">&#91;</span>
        <span class="nu0">10</span> <span class="sy0">=&gt;</span> <span class="br0">&#91;</span><span class="st_h">'aid'</span> <span class="sy0">=&gt;</span> <span class="nu0">123</span><span class="sy0">,</span> <span class="st_h">'code'</span> <span class="sy0">=&gt;</span> <span class="st_h">'x-abc'</span><span class="br0">&#93;</span>
    <span class="br0">&#93;</span>
<span class="br0">&#93;</span>
&nbsp;
<span class="co1">// 2.x</span>
<span class="br0">&#91;</span>
    <span class="st_h">''</span> <span class="sy0">=&gt;</span> <span class="br0">&#91;</span>
        <span class="nu0">10</span> <span class="sy0">=&gt;</span> <span class="br0">&#91;</span><span class="st_h">'aid'</span> <span class="sy0">=&gt;</span> <span class="nu0">123</span><span class="sy0">,</span> <span class="st_h">'code'</span> <span class="sy0">=&gt;</span> <span class="st_h">'x-abc'</span><span class="br0">&#93;</span>
    <span class="br0">&#93;</span>
<span class="br0">&#93;</span></pre></div></div></div></div></div></div></div>






<h3>Semantic change in offsetExists()</h3>



<p>The&nbsp;<a href="https://php-map.org/#offsetexists"><code>offsetExists()</code></a>&nbsp;method uses&nbsp;<code>isset()</code>&nbsp;instead of <code>array_key_exists()</code> now to be in line with typical PHP behavior. Thus, keys with NULL values are now treated differently:</p>





<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="re0">$m</span> <span class="sy0">=</span> Map<span class="sy0">::</span><span class="me2">from</span><span class="br0">&#40;</span> <span class="br0">&#91;</span><span class="st_h">'foo'</span> <span class="sy0">=&gt;</span> <span class="kw4">null</span><span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="co1">// 1.x</span>
<span class="kw3">isset</span><span class="br0">&#40;</span> <span class="re0">$m</span><span class="br0">&#91;</span><span class="st_h">'foo'</span><span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// true</span>
&nbsp;
<span class="co1">// 2.x</span>
<span class="kw3">isset</span><span class="br0">&#40;</span> <span class="re0">$m</span><span class="br0">&#91;</span><span class="st_h">'foo'</span><span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// false</span></pre></div></div></div></div></div></div></div>






<h3>Renamed split() to explode()</h3>



<p>To avoid conflicts with the Laravel <code>split()</code> method and to be in line with the PHP&nbsp;<code>explode()</code>&nbsp;method, the static&nbsp;<code>Map::split()</code>&nbsp;method has been renamed to&nbsp;<code><a href="https://php-map.org/#explode">Map::explode()</a></code>.&nbsp;Also, the argument order has changed:</p>





<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="co1">// 1.x</span>
Map<span class="sy0">::</span><span class="kw3">split</span><span class="br0">&#40;</span> <span class="st_h">'a,b,c'</span><span class="sy0">,</span> <span class="st_h">','</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="co1">// 2.x</span>
Map<span class="sy0">::</span><span class="kw3">explode</span><span class="br0">&#40;</span> <span class="st_h">','</span><span class="sy0">,</span> <span class="st_h">'a,b,c'</span> <span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>



]]></content:encoded>
					
					<wfw:commentRss>https://aimeos.org/tips/php-array-map-2-0/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>PHP: Advanced map methods</title>
		<link>https://aimeos.org/tips/php-advanced-map-methods/</link>
					<comments>https://aimeos.org/tips/php-advanced-map-methods/#comments</comments>
		
		<dc:creator><![CDATA[aimeos]]></dc:creator>
		<pubDate>Wed, 11 Mar 2020 10:42:13 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[map]]></category>
		<guid isPermaLink="false">https://aimeos.org/tips/?p=533</guid>

					<description><![CDATA[The&#160;PHP Map package&#160;contains many methods to ease your day to day work. They are&#160;extremely helpful&#160;in a lot of situations where you’ve written custom code up to now.&#160;This tutorial explains some of the&#160;most useful ones&#160;you don&#8217;t want to miss any more!<span class="ellipsis">&#8230;</span><div class="read-more"><a href="https://aimeos.org/tips/php-advanced-map-methods/">Read more <span class="screen-reader-text">PHP: Advanced map methods</span><span class="meta-nav"> &#8250;</span></a></div><!-- end of .read-more -->]]></description>
										<content:encoded><![CDATA[
<div class="wp-block-image"><figure class="aligncenter size-large"><img width="1024" height="538" src="https://aimeos.org/tips/wp-content/uploads/2020/03/php-map-advanced-1-1024x538.png" alt="" class="wp-image-575" srcset="https://aimeos.org/tips/wp-content/uploads/2020/03/php-map-advanced-1-1024x538.png 1024w, https://aimeos.org/tips/wp-content/uploads/2020/03/php-map-advanced-1-300x158.png 300w, https://aimeos.org/tips/wp-content/uploads/2020/03/php-map-advanced-1-768x403.png 768w, https://aimeos.org/tips/wp-content/uploads/2020/03/php-map-advanced-1-100x53.png 100w, https://aimeos.org/tips/wp-content/uploads/2020/03/php-map-advanced-1-150x79.png 150w, https://aimeos.org/tips/wp-content/uploads/2020/03/php-map-advanced-1-200x105.png 200w, https://aimeos.org/tips/wp-content/uploads/2020/03/php-map-advanced-1-450x236.png 450w, https://aimeos.org/tips/wp-content/uploads/2020/03/php-map-advanced-1-600x315.png 600w, https://aimeos.org/tips/wp-content/uploads/2020/03/php-map-advanced-1-900x473.png 900w, https://aimeos.org/tips/wp-content/uploads/2020/03/php-map-advanced-1.png 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure></div>



<p>The&nbsp;<a href="https://github.com/aimeos/map">PHP Map package</a>&nbsp;contains many methods to ease your day to day work. They are&nbsp;<strong>extremely helpful</strong>&nbsp;in a lot of situations where you’ve written custom code up to now.&nbsp;This tutorial explains some of the&nbsp;<strong>most useful ones</strong>&nbsp;you don&#8217;t want to miss any more!</p>



<ul><li><a href="https://github.com/aimeos/map">Documentation at Github</a></li><li><a href="https://packagist.org/packages/aimeos/map">Composer package at Packagist</a></li></ul>



<span id="more-533"></span>



<h2>Installation</h2>



<p>To use the PHP Map package, you have to add it to your composer based application:</p>



<p><code>composer req aimeos/map</code></p>



<p>Afterwards, there will be a map() function available, that creates a Map object which offers the methods explained.</p>



<h2>col(): Map key/value pairs</h2>



<p>There are numerous times when you need to transform an existing array into a list of key/value pairs. Think about a list of database records for example which should be indexed by ID instead of a sequential index. The <code>col()</code> method creates that without the necessity of a loop:</p>





<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="re0">$rows</span> <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span><span class="st_h">'id'</span> <span class="sy0">=&gt;</span> <span class="nu0">1</span><span class="sy0">,</span> <span class="st_h">'code'</span> <span class="sy0">=&gt;</span> <span class="st_h">'a'</span><span class="br0">&#93;</span><span class="sy0">,</span> <span class="br0">&#91;</span><span class="st_h">'id'</span> <span class="sy0">=&gt;</span> <span class="nu0">3</span><span class="sy0">,</span> <span class="st_h">'code'</span> <span class="sy0">=&gt;</span> <span class="st_h">'d'</span><span class="br0">&#93;</span><span class="br0">&#93;</span><span class="sy0">;</span>
<span class="re0">$result</span> <span class="sy0">=</span> map<span class="br0">&#40;</span> <span class="re0">$rows</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">col</span><span class="br0">&#40;</span> <span class="st_h">'code'</span><span class="sy0">,</span> <span class="st_h">'id'</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="co1">// Result equals:</span>
map<span class="br0">&#40;</span> <span class="br0">&#91;</span><span class="nu0">1</span> <span class="sy0">=&gt;</span> <span class="st_h">'a'</span><span class="sy0">,</span> <span class="nu0">3</span> <span class="sy0">=&gt;</span> <span class="st_h">'d'</span><span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>






<p>This does also work with objects if they implement the magic PHP <code>__get()</code> method.</p>



<h2>collapse() and flat(): Transform multi-dimensional arrays</h2>



<p>Do you remember the last time when you had a multi-dimensional array and you needed a flat array of all elements? If you know the maximum depth, you can nest several loops but if not, you need to write a recursive function, which is very cumbersome. The Map object offers the <code>flat()</code> method, that can to do that in one line:</p>





<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="re0">$array</span> <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="sy0">,</span> <span class="nu0">2</span><span class="br0">&#93;</span><span class="sy0">,</span> <span class="br0">&#91;</span><span class="nu0">3</span><span class="sy0">,</span> <span class="br0">&#91;</span><span class="nu0">4</span><span class="sy0">,</span> <span class="nu0">5</span><span class="br0">&#93;</span><span class="br0">&#93;</span><span class="br0">&#93;</span><span class="sy0">;</span>
<span class="re0">$result</span> <span class="sy0">=</span> map<span class="br0">&#40;</span> <span class="re0">$array</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">flat</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="co1">// Result equals:</span>
map<span class="br0">&#40;</span> <span class="br0">&#91;</span><span class="nu0">1</span><span class="sy0">,</span> <span class="nu0">2</span><span class="sy0">,</span> <span class="nu0">3</span><span class="sy0">,</span> <span class="nu0">4</span><span class="sy0">,</span> <span class="nu0">5</span><span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>






<p>There&#8217;s a similar method named collapse(), that does almost the same but keeps the key of each value. Thus, it will overwrite elements with duplicate keys (also numeric keys):</p>





<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="re0">$array</span> <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span><span class="st_h">'a'</span> <span class="sy0">=&gt;</span> <span class="nu0">1</span><span class="sy0">,</span> <span class="st_h">'b'</span> <span class="sy0">=&gt;</span> <span class="nu0">2</span><span class="br0">&#93;</span><span class="sy0">,</span> <span class="br0">&#91;</span><span class="st_h">'c'</span> <span class="sy0">=&gt;</span> <span class="nu0">3</span><span class="sy0">,</span> <span class="br0">&#91;</span><span class="st_h">'a'</span> <span class="sy0">=&gt;</span> <span class="nu0">4</span><span class="sy0">,</span> <span class="st_h">'b'</span> <span class="sy0">=&gt;</span> <span class="nu0">5</span><span class="br0">&#93;</span><span class="br0">&#93;</span><span class="br0">&#93;</span><span class="sy0">;</span>
<span class="re0">$result</span> <span class="sy0">=</span> map<span class="br0">&#40;</span> <span class="re0">$array</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">collapse</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="co1">// Result equals:</span>
map<span class="br0">&#40;</span> <span class="br0">&#91;</span><span class="st_h">'a'</span> <span class="sy0">=&gt;</span> <span class="nu0">4</span><span class="sy0">,</span> <span class="st_h">'b'</span> <span class="sy0">=&gt;</span> <span class="nu0">5</span><span class="sy0">,</span> <span class="st_h">'c'</span> <span class="sy0">=&gt;</span> <span class="nu0">3</span><span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>






<p>Both methods accept the number of levels they should collapse or flatten as parameter. That way it&#8217;s easy to keep e.g. the third level and above unflattened or uncollapsed.</p>



<h2>concat(), merge() and union(): Differences in combining arrays</h2>



<p>In PHP, you can combine arrays using <code>array_merge()</code> or the <code>+</code> operator but their results are different. The PHP map package also offers the <code>concat()</code> method:</p>





<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="re0">$one</span> <span class="sy0">=</span> <span class="br0">&#91;</span><span class="nu0">0</span> <span class="sy0">=&gt;</span> <span class="st_h">'a'</span><span class="sy0">,</span> <span class="st_h">'b'</span> <span class="sy0">=&gt;</span> <span class="nu0">2</span><span class="br0">&#93;</span><span class="sy0">;</span>
<span class="re0">$two</span> <span class="sy0">=</span> <span class="br0">&#91;</span><span class="st_h">'b'</span> <span class="sy0">=&gt;</span> <span class="nu0">3</span><span class="sy0">,</span> <span class="nu0">0</span> <span class="sy0">=&gt;</span> <span class="nu0">4</span><span class="br0">&#93;</span><span class="sy0">;</span>
&nbsp;
<span class="re0">$result</span> <span class="sy0">=</span> map<span class="br0">&#40;</span> <span class="re0">$one</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">concat</span><span class="br0">&#40;</span> <span class="re0">$two</span> <span class="br0">&#41;</span><span class="sy0">;</span>
<span class="co1">//equals map( [0 =&gt; 'a', 'b' =&gt; 2, 1 =&gt; 3, 2 =&gt; 4] );</span>
&nbsp;
<span class="re0">$result</span> <span class="sy0">=</span> map<span class="br0">&#40;</span> <span class="re0">$one</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">merge</span><span class="br0">&#40;</span> <span class="re0">$two</span> <span class="br0">&#41;</span><span class="sy0">;</span>
<span class="co1">//equals map( [0 =&gt; 'a', 'b' =&gt; 3, 1 =&gt; 4] );</span>
&nbsp;
<span class="re0">$result</span> <span class="sy0">=</span> map<span class="br0">&#40;</span> <span class="re0">$one</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">union</span><span class="br0">&#40;</span> <span class="re0">$two</span> <span class="br0">&#41;</span><span class="sy0">;</span>
<span class="co1">//equals map( [0 =&gt; 'a', 'b' =&gt; 2] );</span></pre></div></div></div></div></div></div></div>






<p><code>concat()</code> combines the elements of both arrays but doesn&#8217;t care about their keys and the resulting list will contain all elements of both arrays. <code>merge()</code> which uses the <code>array_merge()</code> function overwrites existing string keys but renumbers numeric keys. <code>union()</code> is the same as using the <code>+</code> operator and doesn&#8217;t add elements whose keys does already exist.</p>



<h2>equals(): Test if arrays contains the same elements</h2>



<p>To compare arrays, PHP only offers the <code>==</code> and <code>===</code> operators. They return only true if both array contains the same elements with the same keys (and the same order in case of <code>===</code>). Contrary, the equals() method of the Map object can also test arrays without checking the keys:</p>





<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="re0">$one</span> <span class="sy0">=</span> <span class="br0">&#91;</span><span class="st_h">'a'</span><span class="sy0">,</span> <span class="st_h">'b'</span><span class="br0">&#93;</span><span class="sy0">;</span>
<span class="re0">$two</span> <span class="sy0">=</span> <span class="br0">&#91;</span><span class="st_h">'b'</span><span class="sy0">,</span> <span class="st_h">'a'</span><span class="br0">&#93;</span><span class="sy0">;</span>
&nbsp;
map<span class="br0">&#40;</span> <span class="re0">$one</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">equals</span><span class="br0">&#40;</span> <span class="re0">$two</span> <span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// true</span>
map<span class="br0">&#40;</span> <span class="re0">$one</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">equals</span><span class="br0">&#40;</span> <span class="re0">$two</span><span class="sy0">,</span> <span class="kw4">true</span> <span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// false</span></pre></div></div></div></div></div></div></div>






<p>When passing <code>true</code> as second parameter, <code>equals()</code> works like the <code>==</code> operator.</p>



<h2>find(): Return the first matching element</h2>



<p>Sometimes, you need the first element from an array that matches your needs. The Map object offers the find() method which allows you to pass a closure function as parameter. This anonymous function can check whose element matches your needs:</p>





<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1">map<span class="br0">&#40;</span> <span class="br0">&#91;</span><span class="st_h">'a'</span><span class="sy0">,</span> <span class="st_h">'c'</span><span class="sy0">,</span> <span class="st_h">'e'</span><span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">find</span><span class="br0">&#40;</span> <span class="kw2">function</span><span class="br0">&#40;</span> <span class="re0">$value</span><span class="sy0">,</span> <span class="re0">$key</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw1">return</span> <span class="re0">$value</span> <span class="sy0">&gt;=</span> <span class="st_h">'b'</span><span class="sy0">;</span>
<span class="br0">&#125;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="co1">// Result equals 'c'</span></pre></div></div></div></div></div></div></div>






<p>If no matching value is found, <code>null</code> is returned. If you pass <code>true</code> as second parameter, the search for the matching element will be reversed and you will get the last matching element.</p>



<h2>first(), last(), get() and pull(): Advanced ways to retrieve an element</h2>



<p>Fetching the first, last or any element of a list of elements is very handy and most useful in many cases. But the PHP Map object offers more than that. What if those methods don&#8217;t return null if no element is found but throw an exception or execute a closure? Instead of:</p>





<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="kw1">if</span><span class="br0">&#40;</span> <span class="br0">&#40;</span> <span class="re0">$item</span> <span class="sy0">=</span> map<span class="br0">&#40;</span> <span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">first</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">===</span> <span class="kw4">null</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw1">throw</span> <span class="kw2">new</span> \Exception<span class="br0">&#40;</span> <span class="st_h">'error'</span> <span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span>
&nbsp;
<span class="kw1">if</span><span class="br0">&#40;</span> <span class="br0">&#40;</span> <span class="re0">$item</span> <span class="sy0">=</span> map<span class="br0">&#40;</span> <span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">last</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">===</span> <span class="kw4">null</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="co1">// fetch and return record from database</span>
<span class="br0">&#125;</span>
<span class="re0">$id</span> <span class="sy0">=</span> <span class="re0">$item</span><span class="sy0">-&gt;</span><span class="me1">getId</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>






<p>you can simply write:</p>





<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="re0">$id</span> <span class="sy0">=</span> map<span class="br0">&#40;</span> <span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">first</span><span class="br0">&#40;</span> <span class="kw2">new</span> \Exception<span class="br0">&#40;</span> <span class="st_h">'error'</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">getId</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="re0">$id</span> <span class="sy0">=</span> map<span class="br0">&#40;</span> <span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">last</span><span class="br0">&#40;</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="co1">// fetch and return record from database</span>
<span class="br0">&#125;</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">getId</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>






<p>All methods (<code>first()</code>, <code>last()</code>, <code>get()</code> and <code>pull()</code>) methods accept either an exception that&#8217;s thrown or a closure that&#8217;s executed if no element is available. Thus, it saves you the annoying checks that  make you code full of if/else statements.</p>



<p>If you pass an exception, the exception object is created whether it&#8217;s used or not. This is no problem as long as you don&#8217;t do that in a loop. Then, create one exception object before the loop and pass only the variable to the methods.</p>



<h2>in(): Check if several elements are in the list</h2>



<p>To test if an value is in a list of elements with <code>in()</code> isn&#8217;t much different than using <code>in_array()</code>. But what if you need to do that several times with different elements? Then, <code>in()</code> takes away the burden of doing that within a loop:</p>





<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1">map<span class="br0">&#40;</span> <span class="br0">&#91;</span><span class="st_h">'a'</span><span class="sy0">,</span> <span class="st_h">'b'</span><span class="sy0">,</span> <span class="st_h">'c'</span><span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">in</span><span class="br0">&#40;</span> <span class="st_h">'b'</span> <span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// true</span>
map<span class="br0">&#40;</span> <span class="br0">&#91;</span><span class="st_h">'a'</span><span class="sy0">,</span> <span class="st_h">'b'</span><span class="sy0">,</span> <span class="st_h">'c'</span><span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">in</span><span class="br0">&#40;</span> <span class="br0">&#91;</span><span class="st_h">'b'</span><span class="sy0">,</span> <span class="st_h">'a'</span><span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// true</span>
map<span class="br0">&#40;</span> <span class="br0">&#91;</span><span class="st_h">'a'</span><span class="sy0">,</span> <span class="st_h">'b'</span><span class="sy0">,</span> <span class="st_h">'c'</span><span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">in</span><span class="br0">&#40;</span> <span class="br0">&#91;</span><span class="st_h">'d'</span><span class="sy0">,</span> <span class="st_h">'a'</span><span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// false</span></pre></div></div></div></div></div></div></div>






<p>The <code>in()</code> method of the Map object checks if all elements of the passed array are available in the list and only then it returns true.</p>



<h2>toJson(): Encode everything into JSON</h2>



<p>When working with APIs, you almost always need to encode the result into its JSON representation. The PHP map object offers a convenient way to transform all elements to JSON:</p>





<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1">map<span class="br0">&#40;</span> <span class="br0">&#91;</span><span class="st_h">'a'</span><span class="sy0">,</span> <span class="st_h">'b'</span><span class="sy0">,</span> <span class="br0">&#91;</span><span class="st_h">'c'</span><span class="sy0">,</span> <span class="st_h">'d'</span><span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">toJson</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>






<p>Like the PHP <code>json_encode()</code> method, it accepts a parameter for passing options that are used during encoding the elements. There are two that you may find extremely useful:</p>





<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1">map<span class="br0">&#40;</span> <span class="br0">&#91;</span><span class="st_h">'a'</span><span class="sy0">,</span> <span class="st_h">'b'</span><span class="sy0">,</span> <span class="br0">&#91;</span><span class="st_h">'c'</span><span class="sy0">,</span> <span class="st_h">'d'</span><span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">toJson</span><span class="br0">&#40;</span> JSON_FORCE_OBJECT<span class="sy0">|</span>JSON_HEX_QUOT <span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>






<p><code>JSON_FORCE_OBJECT</code> always creates <code>{}</code>, even for empty lists to avoid being encoded as <code>[]</code>. If you add JSON output to HTML data attributes for storing something you need for dynamic Javascript features later on, <code>JSON_HEX_QUOT</code> saves you from generating invalid HTML.</p>



<h2>walk(): Visit each value in a multi-dimensional array</h2>



<p>You receive a multi-dimensional array from an HTML form and want to make sure that all values are of a certain type or doesn&#8217;t contain HTML tags? Then, the walk() method of the PHP Map object is your friend:</p>





<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1">map<span class="br0">&#40;</span> <span class="br0">&#91;</span><span class="st_h">'a'</span><span class="sy0">,</span> <span class="st_h">'b'</span><span class="sy0">,</span> <span class="br0">&#91;</span><span class="st_h">'c'</span><span class="sy0">,</span> <span class="st_h">'&lt;b&gt;d&lt;/b&gt;'</span><span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">walk</span><span class="br0">&#40;</span> <span class="kw2">function</span><span class="br0">&#40;</span> <span class="sy0">&amp;</span><span class="re0">$val</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="re0">$val</span> <span class="sy0">=</span> <span class="kw3">strip_tags</span><span class="br0">&#40;</span> <span class="re0">$val</span> <span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span> <span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>






<p>You can also create a mapping based on the list you pass as second parameter:</p>





<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="re0">$mapping</span> <span class="sy0">=</span> <span class="br0">&#91;</span><span class="nu0">1</span> <span class="sy0">=&gt;</span> <span class="st_h">'one'</span><span class="sy0">,</span> <span class="nu0">2</span> <span class="sy0">=&gt;</span> <span class="st_h">'two'</span><span class="sy0">,</span> <span class="nu0">3</span> <span class="sy0">=&gt;</span> <span class="st_h">'three'</span><span class="sy0">,</span> <span class="nu0">4</span> <span class="sy0">=&gt;</span> <span class="st_h">'four'</span><span class="br0">&#93;</span><span class="sy0">;</span>
&nbsp;
map<span class="br0">&#40;</span> <span class="br0">&#91;</span><span class="nu0">1</span><span class="sy0">,</span> <span class="nu0">2</span><span class="sy0">,</span> <span class="br0">&#91;</span><span class="nu0">3</span><span class="sy0">,</span> <span class="nu0">4</span><span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">walk</span><span class="br0">&#40;</span> <span class="kw2">function</span><span class="br0">&#40;</span> <span class="sy0">&amp;</span><span class="re0">$val</span><span class="sy0">,</span> <span class="re0">$key</span><span class="sy0">,</span> <span class="re0">$data</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="re0">$val</span> <span class="sy0">=</span> <span class="re0">$data</span><span class="br0">&#91;</span><span class="re0">$val</span><span class="br0">&#93;</span> ?? <span class="re0">$val</span><span class="sy0">;</span>
<span class="br0">&#125;</span><span class="sy0">,</span> <span class="re0">$mapping</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="co1">// Result equals</span>
map<span class="br0">&#40;</span> <span class="br0">&#91;</span><span class="st_h">'one'</span><span class="sy0">,</span> <span class="st_h">'two'</span><span class="sy0">,</span> <span class="br0">&#91;</span><span class="st_h">'three'</span><span class="sy0">,</span> <span class="st_h">'four'</span><span class="br0">&#93;</span><span class="br0">&#93;</span> <span class="br0">&#41;</span></pre></div></div></div></div></div></div></div>






<h2>method(): Register your own methods</h2>



<p>The best of all comes last: You can extend the PHP Map object by your own methods! This is extremely handy if you need the same code several times and different places. To register your own method, just do this:</p>





<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1">\Aimeos\Map<span class="sy0">::</span><span class="me2">method</span><span class="br0">&#40;</span> <span class="st_h">'strrev'</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span> <span class="re0">$sep</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw1">return</span> <span class="kw3">strrev</span><span class="br0">&#40;</span> <span class="kw3">join</span><span class="br0">&#40;</span> <span class="st_h">'-'</span><span class="sy0">,</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="kw3">list</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span> <span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>






<p>Your closure function has access to the internal array of elements using <code>$this-&gt;list</code> and you can read and/or modify that internal array of the Map object. You can use your new method the same way as any other method of the Map object afterwards:</p>





<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1">map<span class="br0">&#40;</span> <span class="br0">&#91;</span><span class="st_h">'a'</span><span class="sy0">,</span> <span class="st_h">'b'</span><span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="kw3">strrev</span><span class="br0">&#40;</span> <span class="st_h">'-'</span> <span class="br0">&#41;</span><span class="sy0">;</span>
<span class="co1">// returns &quot;b-a&quot;</span></pre></div></div></div></div></div></div></div>






<h2>Conclusion</h2>



<p>The PHP Map object offers some great methods that can simplify your code drastically and you will never want to miss them any more. Especially the possibility to register own methods to extend the Map object dynamically gives your great power in a few lines of code.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://aimeos.org/tips/php-advanced-map-methods/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>PHP: Array to Map</title>
		<link>https://aimeos.org/tips/php-array-to-map/</link>
					<comments>https://aimeos.org/tips/php-array-to-map/#respond</comments>
		
		<dc:creator><![CDATA[aimeos]]></dc:creator>
		<pubDate>Sat, 07 Dec 2019 18:23:33 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[map]]></category>
		<guid isPermaLink="false">https://aimeos.org/tips/?p=490</guid>

					<description><![CDATA[Arrays are the working horse of PHP, the programming language used by the vast majority of web applications. An array seems to be incredible simple to use but things get complicated and ugly when PHP functions for array manipulation come<span class="ellipsis">&#8230;</span><div class="read-more"><a href="https://aimeos.org/tips/php-array-to-map/">Read more <span class="screen-reader-text">PHP: Array to Map</span><span class="meta-nav"> &#8250;</span></a></div><!-- end of .read-more -->]]></description>
										<content:encoded><![CDATA[
<div class="wp-block-image"><figure class="aligncenter"><img width="1198" height="629" src="https://aimeos.org/tips/wp-content/uploads/2019/12/php-array-map-1.png" alt="PHP Array to Map" class="wp-image-493" srcset="https://aimeos.org/tips/wp-content/uploads/2019/12/php-array-map-1.png 1198w, https://aimeos.org/tips/wp-content/uploads/2019/12/php-array-map-1-300x158.png 300w, https://aimeos.org/tips/wp-content/uploads/2019/12/php-array-map-1-768x403.png 768w, https://aimeos.org/tips/wp-content/uploads/2019/12/php-array-map-1-1024x538.png 1024w, https://aimeos.org/tips/wp-content/uploads/2019/12/php-array-map-1-100x53.png 100w, https://aimeos.org/tips/wp-content/uploads/2019/12/php-array-map-1-150x79.png 150w, https://aimeos.org/tips/wp-content/uploads/2019/12/php-array-map-1-200x105.png 200w, https://aimeos.org/tips/wp-content/uploads/2019/12/php-array-map-1-450x236.png 450w, https://aimeos.org/tips/wp-content/uploads/2019/12/php-array-map-1-600x315.png 600w, https://aimeos.org/tips/wp-content/uploads/2019/12/php-array-map-1-900x473.png 900w" sizes="(max-width: 1198px) 100vw, 1198px" /></figure></div>



<p>Arrays are the working horse of PHP, the programming language used by the vast majority of web applications. An array seems to be incredible simple to use but things get <strong>complicated and ugly</strong> when PHP functions for <strong>array manipulation</strong> come into play. Are you tired of hard to read code with excessive error handling too? Then maps can be the answer.</p>



<ul><li><a href="https://github.com/aimeos/map">Documentation at Github</a></li><li><a href="https://packagist.org/packages/aimeos/map">Composer package at Packagist</a></li></ul>



<span id="more-490"></span>



<h2>Why?</h2>



<p>Here&#8217;s a common example of how array manipulation is done in most PHP code:</p>





<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="re0">$list</span> <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span><span class="st_h">'id'</span> <span class="sy0">=&gt;</span> <span class="st_h">'one'</span><span class="sy0">,</span> <span class="st_h">'value'</span> <span class="sy0">=&gt;</span> <span class="st_h">'value1'</span><span class="br0">&#93;</span><span class="sy0">,</span> <span class="br0">&#91;</span><span class="st_h">'id'</span> <span class="sy0">=&gt;</span> <span class="st_h">'two'</span><span class="sy0">,</span> <span class="st_h">'value'</span> <span class="sy0">=&gt;</span> <span class="st_h">'value2'</span><span class="br0">&#93;</span><span class="sy0">,</span> <span class="kw4">null</span><span class="br0">&#93;</span><span class="sy0">;</span>
&nbsp;
<span class="re0">$list</span><span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="br0">&#91;</span><span class="st_h">'id'</span> <span class="sy0">=&gt;</span> <span class="st_h">'three'</span><span class="sy0">,</span> <span class="st_h">'value'</span> <span class="sy0">=&gt;</span> <span class="st_h">'value3'</span><span class="br0">&#93;</span><span class="sy0">;</span>    <span class="co1">// add element</span>
<span class="kw3">unset</span><span class="br0">&#40;</span> <span class="re0">$list</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">;</span>                                   <span class="co1">// remove element</span>
<span class="re0">$list</span> <span class="sy0">=</span> <span class="kw3">array_filter</span><span class="br0">&#40;</span> <span class="re0">$list</span> <span class="br0">&#41;</span><span class="sy0">;</span>                       <span class="co1">// remove empty values</span>
<span class="kw3">sort</span><span class="br0">&#40;</span> <span class="re0">$list</span> <span class="br0">&#41;</span><span class="sy0">;</span>                                       <span class="co1">// sort elements</span>
<span class="re0">$pairs</span> <span class="sy0">=</span> array_column<span class="br0">&#40;</span> <span class="re0">$list</span><span class="sy0">,</span> <span class="st_h">'value'</span><span class="sy0">,</span> <span class="st_h">'id'</span> <span class="br0">&#41;</span><span class="sy0">;</span>       <span class="co1">// create ['three' =&gt; 'value3']</span>
<span class="re0">$value</span> <span class="sy0">=</span> <span class="kw3">reset</span><span class="br0">&#40;</span> <span class="re0">$pairs</span> <span class="br0">&#41;</span> ?<span class="sy0">:</span> <span class="kw4">null</span><span class="sy0">;</span>                    <span class="co1">// return first value</span></pre></div></div></div></div></div></div></div>






<p>It adds/removes elements, sorts and transforms the content and retrieves the first element. You can&#8217;t write that shorter or more elegant and moreover, you need to check almost all array functions for errors.</p>



<h2>Using maps</h2>



<p>The <em>map</em> composer package offers an easy API comparable to jQuery or Laravel collections:</p>



<p><code>composer req aimeos/map</code></p>



<p>When using the Map object, you can do the same as above more elegant with less code:</p>





<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="re0">$list</span> <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span><span class="st_h">'id'</span> <span class="sy0">=&gt;</span> <span class="st_h">'one'</span><span class="sy0">,</span> <span class="st_h">'value'</span> <span class="sy0">=&gt;</span> <span class="st_h">'value1'</span><span class="br0">&#93;</span><span class="sy0">,</span> <span class="br0">&#91;</span><span class="st_h">'id'</span> <span class="sy0">=&gt;</span> <span class="st_h">'two'</span><span class="sy0">,</span> <span class="st_h">'value'</span> <span class="sy0">=&gt;</span> <span class="st_h">'value2'</span><span class="br0">&#93;</span><span class="sy0">,</span> <span class="kw4">null</span><span class="br0">&#93;</span><span class="sy0">;</span>
&nbsp;
<span class="re0">$value</span> <span class="sy0">=</span> map<span class="br0">&#40;</span> <span class="re0">$list</span> <span class="br0">&#41;</span>                                <span class="co1">// create Map</span>
    <span class="sy0">-&gt;</span><span class="me1">push</span><span class="br0">&#40;</span> <span class="br0">&#91;</span><span class="st_h">'id'</span> <span class="sy0">=&gt;</span> <span class="st_h">'three'</span><span class="sy0">,</span> <span class="st_h">'value'</span> <span class="sy0">=&gt;</span> <span class="st_h">'value3'</span><span class="br0">&#93;</span> <span class="br0">&#41;</span> <span class="co1">// add element</span>
    <span class="sy0">-&gt;</span><span class="me1">remove</span><span class="br0">&#40;</span> <span class="nu0">0</span> <span class="br0">&#41;</span>                                    <span class="co1">// remove element</span>
    <span class="sy0">-&gt;</span><span class="me1">filter</span><span class="br0">&#40;</span><span class="br0">&#41;</span>                                       <span class="co1">// remove empty values</span>
    <span class="sy0">-&gt;</span><span class="kw3">sort</span><span class="br0">&#40;</span><span class="br0">&#41;</span>                                         <span class="co1">// sort elements</span>
    <span class="sy0">-&gt;</span><span class="me1">col</span><span class="br0">&#40;</span> <span class="st_h">'value'</span><span class="sy0">,</span> <span class="st_h">'id'</span> <span class="br0">&#41;</span>                           <span class="co1">// create ['three' =&gt; 'value3']</span>
    <span class="sy0">-&gt;</span><span class="me1">first</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>                                       <span class="co1">// return first value</span></pre></div></div></div></div></div></div></div>






<p>And contrary to PHP arrays, you don&#8217;t have to care about error handling because either, there are no errors possible or the Map object will throw an exception if something is really wrong.</p>



<p>Of course, you can use the Map object still like a plain PHP array:</p>





<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="re0">$map</span><span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="br0">&#91;</span><span class="st_h">'id'</span> <span class="sy0">=&gt;</span> <span class="st_h">'three'</span><span class="sy0">,</span> <span class="st_h">'value'</span> <span class="sy0">=&gt;</span> <span class="st_h">'value3'</span><span class="br0">&#93;</span><span class="sy0">;</span>
<span class="re0">$value</span> <span class="sy0">=</span> <span class="re0">$map</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="sy0">;</span>
<span class="kw3">count</span><span class="br0">&#40;</span> <span class="re0">$map</span> <span class="br0">&#41;</span><span class="sy0">;</span>
<span class="kw1">foreach</span><span class="br0">&#40;</span> <span class="re0">$map</span> <span class="kw1">as</span> <span class="re0">$key</span> <span class="sy0">=&gt;</span> value <span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>






<p>In addition, Map objects allow you to pass anonymous functions to a lot of methods. Thus, you can process and transform the elements according to your current needs, e.g. using the <code>each()</code> method:</p>





<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="re0">$map</span><span class="sy0">-&gt;</span><span class="kw3">each</span><span class="br0">&#40;</span> <span class="kw2">function</span><span class="br0">&#40;</span> <span class="re0">$val</span><span class="sy0">,</span> <span class="re0">$key</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw1">echo</span> <span class="re0">$key</span> <span class="sy0">.</span> <span class="st_h">': '</span> <span class="sy0">.</span> <span class="re0">$val</span><span class="sy0">;</span>
<span class="br0">&#125;</span> <span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>






<p>You can even extend Maps dynamically by own methods. This is especially useful if you need a transformation of the Map elements more often and at several places of your code:</p>





<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1">Map<span class="sy0">::</span><span class="me2">method</span><span class="br0">&#40;</span> <span class="st_h">'strrev'</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span> <span class="re0">$sep</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw1">return</span> <span class="kw3">strrev</span><span class="br0">&#40;</span> <span class="kw3">join</span><span class="br0">&#40;</span> <span class="st_h">'-'</span><span class="sy0">,</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="kw3">list</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
Map<span class="sy0">::</span><span class="me2">from</span><span class="br0">&#40;</span> <span class="br0">&#91;</span><span class="st_h">'a'</span><span class="sy0">,</span> <span class="st_h">'b'</span><span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="kw3">strrev</span><span class="br0">&#40;</span> <span class="st_h">'-'</span> <span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>






<p>The best part is using a Map like a jQuery object, where methods are applied to all objects of the Map. Then, you can create expressive code in one line:</p>





<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="kw2">class</span> MyClass <span class="br0">&#123;</span>
    <span class="kw2">private</span> <span class="re0">$code</span><span class="sy0">;</span>
    <span class="kw2">private</span> <span class="re0">$status</span><span class="sy0">;</span>
&nbsp;
    <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span> <span class="re0">$code</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">code</span> <span class="sy0">=</span> <span class="re0">$code</span><span class="sy0">;</span> <span class="br0">&#125;</span>
    <span class="kw2">public</span> <span class="kw2">function</span> setStatus<span class="br0">&#40;</span> <span class="re0">$s</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="kw3">stat</span> <span class="sy0">=</span> <span class="re0">$s</span><span class="sy0">;</span> <span class="kw1">return</span> <span class="re0">$this</span><span class="sy0">;</span> <span class="br0">&#125;</span>
    <span class="kw2">public</span> <span class="kw2">function</span> getCode<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="kw1">return</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">code</span><span class="sy0">;</span> <span class="br0">&#125;</span>
<span class="br0">&#125;</span>
&nbsp;
<span class="re0">$objects</span> <span class="sy0">=</span> Map<span class="sy0">::</span><span class="me2">from</span><span class="br0">&#40;</span> <span class="br0">&#91;</span><span class="st_h">'a'</span> <span class="sy0">=&gt;</span> <span class="kw2">new</span> MyClass<span class="br0">&#40;</span> <span class="st_h">'x'</span> <span class="br0">&#41;</span><span class="sy0">,</span> <span class="st_h">'b'</span> <span class="sy0">=&gt;</span> <span class="kw2">new</span> MyClass<span class="br0">&#40;</span> <span class="st_h">'y'</span> <span class="br0">&#41;</span><span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
<span class="re0">$result</span> <span class="sy0">=</span> <span class="re0">$objects</span><span class="sy0">-&gt;</span><span class="me1">setStatus</span><span class="br0">&#40;</span> <span class="nu0">1</span> <span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">getCode</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">toArray</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>






<p>The example will call <code>setStatus( 1 )</code> on all objects and creates a new Map with the returned values. Because <code>setStatus()</code> returns the same object (<code>$this</code>), we can apply another method on all objects afterwards (<code>getCode()</code>). The results of <code>getCode()</code> is wrapped in a Map object again and <code>toArray()</code> returns a plain PHP array which consists of:</p>





<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="br0">&#91;</span><span class="st_h">'a'</span> <span class="sy0">=&gt;</span> <span class="st_h">'x'</span><span class="sy0">,</span> <span class="st_h">'b'</span> <span class="sy0">=&gt;</span> <span class="st_h">'y'</span><span class="br0">&#93;</span></pre></div></div></div></div></div></div></div>






<h2>Available methods</h2>



<p>The Map object contains methods for almost all array functions available and more like collapse(), flat(), toJson() or methods found in the Javascript Array prototype. Here&#8217;s a list of its methods and for documentation, have a look at the <a href="https://github.com/aimeos/map#methods">Map readme</a> at GitHub where you can also check the code.</p>



<ul><li><strong>function is_map()</strong>&nbsp;: Tests if the variable is a map object</li><li><strong>function map()</strong> : Creates a new map from elements</li><li><strong>arsort()</strong> : Reverse sort elements with keys</li><li><strong>asort()</strong> : Sort elements with keys</li><li><strong>chunk()</strong> : Splits the map into chunks</li><li><strong>clear()</strong> : Removes all elements</li><li><strong>col()</strong> : Creates a key/value mapping</li><li><strong>collapse()</strong> : Collapses multi-dimensional elements</li><li><strong>concat()</strong> : Combines the elements</li><li><strong>copy()</strong> : Creates a new copy</li><li><strong>count()</strong> : Returns the number of elements</li><li><strong>diff()</strong> : Returns the missing elements</li><li><strong>diffAssoc()</strong> : Returns the missing elements and checks keys</li><li><strong>diffKeys()</strong> : Returns the missing elements by keys</li><li><strong>each()</strong> : Applies a callback to each element</li><li><strong>empty()</strong> : Tests if map is empty</li><li><strong>equals()</strong> : Tests if map contents are equal</li><li><strong>filter()</strong> : Applies a filter to the map elements</li><li><strong>find()</strong> : Returns the first matching element</li><li><strong>first()</strong> : Returns the first element</li><li><strong>firstKey()</strong>&nbsp;: Returns the first key</li><li><strong>flip()</strong> : Exchanges keys with their values</li><li><strong>flat()</strong> : Flattens multi-dimensional elements</li><li><strong>from()</strong> : Creates a new map from passed elements</li><li><strong>get()</strong> : Returns an element by key</li><li><strong>has()</strong> : Tests if a key exists</li><li><strong>in()</strong> : Tests if element is included</li><li><strong>includes()</strong> : Tests if element is included</li><li><strong>intersect()</strong> : Returns the shared elements</li><li><strong>intersectAssoc()</strong> : Returns the shared elements and checks keys</li><li><strong>intersectKeys()</strong> : Returns the shared elements by keys</li><li><strong>isEmpty()</strong> : Tests if map is empty</li><li><strong>join()</strong> : Returns concatenated elements as string</li><li><strong>keys()</strong> : Returns the keys</li><li><strong>krsort()</strong> : Reverse sort elements by keys</li><li><strong>ksort()</strong> : Sort elements by keys</li><li><strong>last()</strong> : Returns the last element</li><li><strong>lastKey()</strong>&nbsp;: Returns the last key</li><li><strong>map()</strong> : Applies a callback to each element and returns the results</li><li><strong>merge()</strong> : Combines elements overwriting existing ones</li><li><strong>method()</strong> : Registers a custom method</li><li><strong>pipe()</strong> : Applies a callback to the map</li><li><strong>pop()</strong> : Returns and removes the last element</li><li><strong>pull()</strong> : Returns and removes an element by key</li><li><strong>push()</strong> : Adds an element to the end</li><li><strong>random()</strong> : Returns random elements</li><li><strong>reduce()</strong> : Computes a value for the map content</li><li><strong>remove()</strong> : Removes an element by key</li><li><strong>replace()</strong> : Replaces elements recursively</li><li><strong>reverse()</strong> : Reverses the array order</li><li><strong>rsort()</strong> : Reverse sort elements</li><li><strong>search()</strong> : Find the key of an element</li><li><strong>set()</strong> : Overwrites an element</li><li><strong>shift()</strong> : Returns and removes the first element</li><li><strong>shuffle()</strong> : Randomizes the element order</li><li><strong>slice()</strong> : Returns a slice of the map</li><li><strong>sort()</strong> : Sorts elements</li><li><strong>split()</strong> : Splits a string into map elements</li><li><strong>splice()</strong> : Replaces a slice by new elements</li><li><strong>toArray()</strong> : Returns the plain array</li><li><strong>toJson()</strong> : Returns the elements in JSON format</li><li><strong>uasort()</strong> : Sorts elements with keys using callback</li><li><strong>uksort()</strong> : Sorts elements by keys using callback</li><li><strong>union()</strong> : Combines the element without overwriting</li><li><strong>unique()</strong> : Returns unique elements</li><li><strong>unshift()</strong> : Adds an element at the beginning</li><li><strong>usort()</strong> : Sorts elements using callback</li><li><strong>values()</strong> : Returns all elements with new keys</li></ul>



<p>Again, the <a href="https://github.com/aimeos/map#methods">Map documentation</a> offers descriptions, explains parameters/return values and has examples for each method. Some methods are even better documented than the corresponding PHP array function.</p>



<h2>Conclusion</h2>



<p>Map objects are an easy to use and elegant way to handle PHP arrays. After using it for a while you won&#8217;t want to remember the times before. If you&#8217;ve already worked with jQuery and like it&#8217;s simplicity, you will feel like home when using appropriate lists of objects in your application.</p>



<p>As the Map methods are using the native array functions or are highly optimized, it&#8217;s also save to use them in large scale applications. The documentation contains a section about the <a href="https://github.com/aimeos/map#performance">Map performance</a> and hints to avoid performance bottlenecks.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://aimeos.org/tips/php-array-to-map/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Setup a working Neos CMS environment</title>
		<link>https://aimeos.org/tips/setup-a-working-neos-cms-environment/</link>
					<comments>https://aimeos.org/tips/setup-a-working-neos-cms-environment/#comments</comments>
		
		<dc:creator><![CDATA[aimeos]]></dc:creator>
		<pubDate>Thu, 23 Apr 2015 07:00:08 +0000</pubDate>
				<category><![CDATA[Neos]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[PHP]]></category>
		<guid isPermaLink="false">http://aimeos.org/tips/?p=89</guid>

					<description><![CDATA[This article is for Neos 2.0 and thing might have changed in the meantime. At least the software packages are required in newer versions. Neos is well known for its well designed interface for editors who quickly fall in love<span class="ellipsis">&#8230;</span><div class="read-more"><a href="https://aimeos.org/tips/setup-a-working-neos-cms-environment/">Read more <span class="screen-reader-text">Setup a working Neos CMS environment</span><span class="meta-nav"> &#8250;</span></a></div><!-- end of .read-more -->]]></description>
										<content:encoded><![CDATA[<p><strong>This article is for Neos 2.0 and thing might have changed in the meantime. At least the software packages are required in newer versions.</strong></p>
<p><a title="Neos" href="https://neos.typo3.org/">Neos</a> is well known for its well designed interface for editors who quickly fall in love with the system. On the other hand, it&#8217;s also infamous for being hard to set up by starters due to its high demands towards the hosting environment. Someone also compared it to walking on a slack line: It&#8217;s easy to stumble and to get frustrated.</p>
<p><img width="750" height="275" class="image-inline aligncenter size-full wp-image-97" src="/tips/wp-content/uploads/2015/04/neos-error.png" alt="Neos error page" srcset="https://aimeos.org/tips/wp-content/uploads/2015/04/neos-error.png 750w, https://aimeos.org/tips/wp-content/uploads/2015/04/neos-error-300x110.png 300w, https://aimeos.org/tips/wp-content/uploads/2015/04/neos-error-100x37.png 100w, https://aimeos.org/tips/wp-content/uploads/2015/04/neos-error-150x55.png 150w, https://aimeos.org/tips/wp-content/uploads/2015/04/neos-error-200x73.png 200w, https://aimeos.org/tips/wp-content/uploads/2015/04/neos-error-450x165.png 450w, https://aimeos.org/tips/wp-content/uploads/2015/04/neos-error-600x220.png 600w" sizes="(max-width: 750px) 100vw, 750px" /></p>
<p>This article focuses on creating a hosting environment suited for Neos so you will be able to finish the Neos setup and get a running instance afterwards. I&#8217;m using Ubuntu 14.04 as reference but it also applies to other Linux distributions like Fedora, only the path names might differ a bit (<code>/etc/apache2/</code> vs. <code>/etc/httpd/</code>).</p>
<p><span id="more-89"></span></p>
<h2>Required software</h2>
<p>At first, you need to make sure that the required software is installed. Apache, MySQL and PHP might be obvious but that&#8217;s not enough. Especially the additional Apache and PHP modules are important to get it running at all. You will have a good start with these packages:</p>
<ul>
<li>Apache 2.2+</li>
<li>MySQL 5.1+</li>
<li>PHP 5.3.2+
<ul>
<li>mbstring</li>
<li>tokenizer</li>
<li>pdo_mysql</li>
</ul>
</li>
</ul>
<p>Even if the minimum requirement for PHP is version 5.3.2, it&#8217;s better to use newer versions for performance reasons. Also, the requirements for Neos will be PHP 5.5+ for Neos 2.0.</p>
<p>You can install the required packages via the package manager of your distribution (apt-get for Ubuntu/Debian, rpm for Fedora/SuSE):</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><ol><li class="li1"><pre class="de1"><span class="kw2">sudo</span> <span class="kw2">apt-get install</span> apache2 mysql-server libapache2-mod-php5 php5 php5-mysql</pre></li></ol></div></div></div></div></div></div></div>


<p>To check if all required PHP extensions are available, the easiest way is to place a short PHP file named <code>info.php</code> in your Apache document root directory (usually <code>/var/www/</code>) containing this content:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><ol><li class="li1"><pre class="de1"><span class="sy0">&amp;</span>lt<span class="sy0">;</span>?php <span class="kw3">phpinfo</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></li></ol></div></div></div></div></div></div></div>


<p>If you open <code>http://localhost/info.php</code> in your browser, the output will contain the environment, the configuration settings and the installed modules available in your installation. Search for the strings &#8220;mbstring&#8221;, &#8220;tokenizer&#8221; and &#8220;pdo_mysql&#8221;. There should be sections with this names in the rendered page.</p>
<h2>Apache configuration</h2>
<p>Most often, the first problems occur due to an insufficient Apache configuration. Neos uses .htaccess files and needs some modules that are not activated by default all the times. The &#8220;mod_rewrite&#8221; and &#8220;mod_env&#8221; modules are the best examples for this. To enable them in your Apache configuration, you need to execute these commands on the command line:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><ol><li class="li1"><pre class="de1"><span class="kw2">sudo</span> a2enmod rewrite</pre></li><li class="li1"><pre class="de1"><span class="kw2">sudo</span> a2enmod <span class="kw2">env</span></pre></li></ol></div></div></div></div></div></div></div>


<p>If you got the message that one or both modules had been already enabled then this was set up correctly before.</p>
<p>The VHost configuration is another common source of problems and you have to adapt it for sure. The default configuration is located in <code>/etc/apache2/sites-enabled/000-default.conf</code> (in Fedora or other Linux distributions the files might be located somewhere else). In this file, change the <code>DocumentRoot</code> to the Neos <code>./Web/</code> directory, e.g.</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="apache"><ol><li class="li1"><pre class="de1"><span class="kw1">DocumentRoot</span> /var/www/Neos/Web</pre></li></ol></div></div></div></div></div></div></div>


<p>Furthermore, you need to add a &#8220;Directory&#8221; directive below the &#8220;DocumentRoot&#8221; line to allow the Neos .htaccess file to add rewrite rules and some settings:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="apache"><ol><li class="li1"><pre class="de1">	<span class="kw1">AllowOverride</span> <span class="kw2">All</span></pre></li></ol></div></div></div></div></div></div></div>


<p>If there&#8217;s already a &#8220;Directory&#8221; section available, replace it with the lines above.</p>
<div class="caution">Keep care that the named directory in the &#8220;Directory&#8221; directive is exactly the same as in the &#8220;DocumentRoot&#8221; AND contains a trailing slash (/).</div>
<p>The last task is to restart Apache:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><ol><li class="li1"><pre class="de1"><span class="kw2">sudo</span> service apache2 restart</pre></li></ol></div></div></div></div></div></div></div>


<p>Now you should be at able to open at least <code>http://localhost/setup</code> in your browser but beware: <code>http://localhost/index.php/setup</code> <strong>still won&#8217;t work!</strong></p>
<h2>PHP configuration</h2>
<p>There are various PHP settings that can cause your Neos installation to fail. First of all you should have a look into the output of the <code>info.php</code> file again and check if one of the required PHP functions is disabled. This is often the case for security reasons in shared hosting environments and this leads to errors afterwards. Especially check the <code>disable_functions</code> line in the core section for:</p>
<ul>
<li>system</li>
<li>shell_exec</li>
<li>escapeshellcmd</li>
<li>escapeshellarg</li>
</ul>
<p>If non of these function is listed there, then you are on the safe side.</p>
<p>The next problems are lurking in your PHP configuration but that can be fixed either by adding a few lines to your <code>/etc/php5/apache2/php.ini</code> or to the <code>.htaccess</code> file in the Neos <code>./Web/</code> directory. When using the php.ini file you should add these lines at the end of the file:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="apache"><ol><li class="li1"><pre class="de1">memory_limit = 256M</pre></li><li class="li1"><pre class="de1">magic_quotes_gpc = <span class="kw2">off</span> <span class="co1"># only for PHP 5.3 to be sure</span></pre></li><li class="li1"><pre class="de1">date.timezone = Europe/Berlin <span class="co1"># or any other valid timezone</span></pre></li><li class="li1"><pre class="de1">xdebug.max_nesting_level = <span class="nu0">500</span> <span class="co1"># only if XDebug extension is installed</span></pre></li></ol></div></div></div></div></div></div></div>


<p>In case you are allowed to overwrite settings also via the <code>.htaccess</code> file, you can add these lines to the <code>./Web/.htaccess</code> of your Neos installation:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="apache"><ol><li class="li1"><pre class="de1"><span class="kw1">php_value</span> memory_limit = 256M</pre></li><li class="li1"><pre class="de1"><span class="kw1">php_flag</span> magic_quotes_gpc <span class="kw2">off</span> <span class="co1"># only for PHP 5.3 to be sure</span></pre></li><li class="li1"><pre class="de1"><span class="kw1">php_value</span> date.timezone Europe/Berlin <span class="co1"># or any other valid timezone</span></pre></li><li class="li1"><pre class="de1"><span class="kw1">php_value</span> xdebug.max_nesting_level <span class="nu0">500</span> <span class="co1"># only if XDebug extension is installed</span></pre></li></ol></div></div></div></div></div></div></div>


<p>Neos can be very memory consuming so even the standard memory limit of 128MB can be to low. Errors due to an insufficient memory limit are written to the Apache error log file (usually in <code>/var/log/apache2/error.log</code>) so it&#8217;s a good idea to keep an eye on the content of that file.</p>
<p>The <code>magic_quotes_gpc</code> directive has been removed in PHP 5.4 and was already deprecated before. To disable it for PHP 5.3 is only a safety measure for installations that have been constantly updated during the last years.</p>
<p>For the timezone value you can use any valid timezone if it fits better for your location. A list of valid timezones is available at the <a title="PHP valid time zones" href="http://php.net/manual/en/timezones.php">PHP web site</a>.</p>
<p>When you&#8217;ve installed the PHP XDebug extension you might run into error messages like <code>Maximum function nesting level of '...' reached</code>. Only in this case you need to increase the XDebug nesting level for functions.</p>
<h2>Done!</h2>
<p>Finally, you should be able to open the Neos setup page in your browser, that is available at <code>http://localhost/setup</code>. The rest of the installation is usually a non-brainer due to the nice setup routine provided by the Neos installer.</p>
<p>Afterwards you can enjoy Neos. Happy editing! <img src="https://s.w.org/images/core/emoji/13.0.1/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://aimeos.org/tips/setup-a-working-neos-cms-environment/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
