<?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 - WP2X</title> <atom:link href="https://wp2x.com/tag/php/feed/" rel="self" type="application/rss+xml" /><link>https://wp2x.com</link> <description>We can help your Wordpress faster</description> <lastBuildDate>Fri, 29 May 2015 15:14:41 +0000</lastBuildDate> <language>en-US</language> <sy:updatePeriod> hourly </sy:updatePeriod> <sy:updateFrequency> 1 </sy:updateFrequency> <generator>https://wordpress.org/?v=5.6.16</generator> <item><title>Randomise elements in PHP array with shuffle</title><link>https://wp2x.com/randomise-elements-in-php-array-with-shuffle/</link> <dc:creator><![CDATA[david]]></dc:creator> <pubDate>Fri, 29 May 2015 15:14:41 +0000</pubDate> <category><![CDATA[Blog]]></category> <category><![CDATA[Wordpress Tips]]></category> <category><![CDATA[php]]></category> <category><![CDATA[wordpress development]]></category> <category><![CDATA[wordpress ebook]]></category> <category><![CDATA[wordpress html]]></category> <category><![CDATA[wordpress programming]]></category> <category><![CDATA[wordpress theme]]></category> <category><![CDATA[wordpress tips]]></category> <category><![CDATA[wordpress tutorial]]></category> <guid
isPermaLink="false">http://wp2x.com/?p=370</guid><description><![CDATA[<p>Randomise elements in PHP array with shuffle is quite easy. I have to spend at least an hour to optimise this task until I found shuffle function what already done this task :). The function reference you can check more detail here: http://php.net/manual/en/function.shuffle.php Here is an example for how to randomise elements in PHP array with ...</p><p>The post <a
rel="nofollow" href="https://wp2x.com/randomise-elements-in-php-array-with-shuffle/">Randomise elements in PHP array with shuffle</a> appeared first on <a
rel="nofollow" href="https://wp2x.com">WP2X</a>.</p> ]]></description> <content:encoded><![CDATA[<p>Randomise elements in PHP array with shuffle is quite easy. I have to spend at least an hour to optimise this task until I found shuffle function what already done this task :).<span
id="more-370"></span></p><p>The function reference you can check more detail here: <a
href="http://php.net/manual/en/function.shuffle.php" target="_blank">http://php.net/manual/en/function.shuffle.php</a></p><p><a
href="http://wp2x.com/wp-content/uploads/2015/05/PHP__shuffle_-_Manual.jpg?4b2a2c&amp;4b2a2c"><img
loading="lazy" class="aligncenter size-full wp-image-371" src="http://wp2x.com/wp-content/uploads/2015/05/PHP__shuffle_-_Manual.jpg?4b2a2c&amp;4b2a2c" alt="PHP__shuffle_-_Manual" width="564" height="326" srcset="https://wp2x.com/wp-content/uploads/2015/05/PHP__shuffle_-_Manual.jpg 564w, https://wp2x.com/wp-content/uploads/2015/05/PHP__shuffle_-_Manual-300x173.jpg 300w" sizes="(max-width: 564px) 100vw, 564px" /></a></p><p>Here is an example for how to randomise elements in PHP array with <strong>shuffle</strong></p><p>Let &#8216;s run this code</p><pre class="lang:php decode:true ">&lt;?php
    $natural_born_killers = array("lions", "tigers", "bears", "kittens");
    shuffle($natural_born_killers);
    var_dump($natural_born_killers);
?&gt;</pre><p>Run in many times and try to see the result array, it will show you different result every time you run it.<br
/> One major drawback to using shuffle() is that it mangles your array keys. This is unavoidable sadly, and you need to live with it when you use shuffle(). Note that shuffle uses its parameter by reference &#8211; it returns true, but shuffles up the parameter you pass to it.</p><p>If you want to pick out just one random value from an array, you can use array_rand() &#8211; it takes an array to read from, then returns one random key from inside there. The advantage to array_rand() is that it leaves the original array intact &#8211; you can use the random key returned to grab the related value from the array, but other than that the original array is left untouched.</p><p><strong>Array_rand</strong>() has an optional second parameter that allows you to specify the number of elements you would like returned. These are each chosen randomly from the array, and are not necessarily returned in any particular order.</p><p>Before I show you an example, you need to be aware of the following attributes of <strong>array_rand</strong>():</p><p>It returns the keys in your array. If these aren&#8217;t specified, the default integer indexes are used. To get the value out of the array, just look up the value at the key.</p><p>If you ask for one random element, or do not specify parameter two, you will get a standard variable back.</p><p>If you ask for more than one random element, you will receive an array of variables back.</p><p>If you ask for more random elements than there are in the array you will get an error</p><p><strong>Array_rand</strong>() will not return duplicate elements if you request more than one random element</p><p>If you want to read most or all of the elements from your array in a random order, use a mass-randomiser like shuffle() &#8211; it is faster.</p><p>With that in mind, here&#8217;s an example of array_rand() in action:</p><pre class="lang:php decode:true">&lt;?php
    $natural_born_killers = array("lions", "tigers", "bears", "kittens");
    var_dump(array_rand($natural_born_killers, 2));
?&gt;</pre><p>&nbsp;</p><p>The post <a
rel="nofollow" href="https://wp2x.com/randomise-elements-in-php-array-with-shuffle/">Randomise elements in PHP array with shuffle</a> appeared first on <a
rel="nofollow" href="https://wp2x.com">WP2X</a>.</p> ]]></content:encoded> </item> <item><title>Delete all Duplicate Rows in MySQL</title><link>https://wp2x.com/delete-duplicate-rows-in-mysql/</link> <comments>https://wp2x.com/delete-duplicate-rows-in-mysql/#comments</comments> <dc:creator><![CDATA[david]]></dc:creator> <pubDate>Mon, 01 Sep 2014 02:24:23 +0000</pubDate> <category><![CDATA[Blog]]></category> <category><![CDATA[Wordpress Plugins]]></category> <category><![CDATA[Wordpress Tips]]></category> <category><![CDATA[hosting]]></category> <category><![CDATA[mysql]]></category> <category><![CDATA[php]]></category> <category><![CDATA[programming]]></category> <guid
isPermaLink="false">http://wp2x.com/?p=255</guid><description><![CDATA[<p>Doing PHP project with MySQL database, I usually meet the problem with duplicate rows value. In the optimization process, I just want to delete all of rows except the first one. I see we have 2 options here: The first way is easy and not a good way to do in big data. We will look ...</p><p>The post <a
rel="nofollow" href="https://wp2x.com/delete-duplicate-rows-in-mysql/">Delete all Duplicate Rows in MySQL</a> appeared first on <a
rel="nofollow" href="https://wp2x.com">WP2X</a>.</p> ]]></description> <content:encoded><![CDATA[<p>Doing PHP project with MySQL database, I usually meet the problem with duplicate rows value. In the optimization process, I just want to delete all of rows except the first one. I see we have 2 options here:<span
id="more-255"></span></p><p><a
href="http://wp2x.com/wp-content/uploads/2014/08/mysql.png?4b2a2c&amp;4b2a2c"><img
loading="lazy" class="aligncenter size-medium wp-image-256" src="http://wp2x.com/wp-content/uploads/2014/08/mysql-300x300.png?4b2a2c&amp;4b2a2c" alt="mysql" width="300" height="300" srcset="https://wp2x.com/wp-content/uploads/2014/08/mysql-300x300.png 300w, https://wp2x.com/wp-content/uploads/2014/08/mysql-150x150.png 150w, https://wp2x.com/wp-content/uploads/2014/08/mysql-1024x1024.png 1024w, https://wp2x.com/wp-content/uploads/2014/08/mysql-55x55.png 55w" sizes="(max-width: 300px) 100vw, 300px" /></a></p><div
class="su-list" style="margin-left:0px"><ul><li><i
class="sui sui-check" style="color:#40dd1b"></i> Fetch all of records to PHP and delete one by one if it is not the first one with the same value</li><li><i
class="sui sui-check" style="color:#40dd1b"></i> Execute one MySQL query to delete</li></ul></div><p>The first way is easy and not a good way to do in big data. We will look at the second one what save time and server resource. We just need to execute these sql query</p><h2>If you want to keep the row with the lowest <strong>id</strong> value</h2><p>Let &#8216;s use one of these SQL</p><pre class="lang:mysql decode:true ">DELETE n1 FROM names n1, names n2 WHERE n1.id &gt; n2.id AND n1.name = n2.name</pre><p>or</p><pre class="lang:tsql decode:true">DELETE FROM NAMES
WHERE id NOT IN (SELECT *
FROM (SELECT MIN(n.id)
FROM NAMES n
GROUP BY n.name) x)</pre><h2>If you want the <strong>id</strong> value that is the highest</h2><pre class="lang:mysql decode:true ">DELETE n1 FROM names n1, names n2 WHERE n1.id &lt; n2.id AND n1.name = n2.name</pre><p>or</p><pre class="lang:mysql decode:true ">DELETE FROM NAMES
WHERE id NOT IN (SELECT *
FROM (SELECT MIN(n.id)
FROM NAMES n
GROUP BY n.name) x)</pre><p>Now time to enjoy the result. Check it out and tell us if you meet any problem.</p><p>The post <a
rel="nofollow" href="https://wp2x.com/delete-duplicate-rows-in-mysql/">Delete all Duplicate Rows in MySQL</a> appeared first on <a
rel="nofollow" href="https://wp2x.com">WP2X</a>.</p> ]]></content:encoded> <wfw:commentRss>https://wp2x.com/delete-duplicate-rows-in-mysql/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> </channel> </rss>