Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to array_merge a dynamic array based on one of it's value similarity
    primarykey
    data
    text
    <p>Good day,</p> <p>I am retrieving information from various websites using cURL and various parsing techniques. I made the code so I can, if desired, add additional websites I scan information from.</p> <p>The information retrieved is as follow : (Please note that the information may be inaccurate and may not reflect real price/name)</p> <pre><code>Array ( [website1.com] =&gt; Array ( [0] =&gt; Array ( [0] =&gt; 60" BRAVIA LX900 Series 3D HDTV [1] =&gt; website1.com [2] =&gt; 5299.99 ) [1] =&gt; Array ( [0] =&gt; 52" BRAVIA LX900 Series 3D HDTV [1] =&gt; website1.com [2] =&gt; 4499.99 ) [2] =&gt; Array ( [0] =&gt; 46" BRAVIA LX900 Series 3D HDTV [1] =&gt; website1.com [2] =&gt; 3699.99 ) [3] =&gt; Array ( [0] =&gt; 40" BRAVIA LX900 Series 3D HDTV [1] =&gt; website1.com [2] =&gt; 2999.99 ) ) [website2.com] =&gt; Array ( [0] =&gt; Array ( [0] =&gt; Sony 3D 60" LX900 HDTV BRAVIA [1] =&gt; website2.com [2] =&gt; 5400.99 ) [1] =&gt; Array ( [0] =&gt; Sony 3D 52" LX900 HDTV BRAVIA [1] =&gt; website2.com [2] =&gt; 4699.99 ) [2] =&gt; Array ( [0] =&gt; Sony 3D 46" LX900 HDTV BRAVIA [1] =&gt; website2.com [2] =&gt; 3899.99 ) ) ) </code></pre> <p>The desired output must be : </p> <pre><code>Array ( [0] =&gt; Array ( [Name] =&gt; 60" BRAVIA LX900 Series 3D HDTV [website1.com] =&gt; 5299.99 [website2.com] =&gt; 5400.99 ) [1] =&gt; Array ( [Name] =&gt; 52" BRAVIA LX900 Series 3D HDTV [website1.com] =&gt; 4499.99 [website2.com] =&gt; 4699.99 ) [2] =&gt; Array ( [Name] =&gt; 46" BRAVIA LX900 Series 3D HDTV [website1.com] =&gt; 3699.99 [website2.com] =&gt; 3899.99 ) [3] =&gt; Array ( [Name] =&gt; 40" BRAVIA LX900 Series 3D HDTV [website1.com] =&gt; 2999.99 ) ) </code></pre> <p>Please note that the name may vary so using similar_text is required. Also, some information may not be shown in all websites. I am aware that only one television name must be choosed, then I will use the one from the most relevent source (website1.com)</p> <p>Here is the codes I'm trying to make work.</p> <pre><code>&lt;?php $_Retreived = array( "website1.com" =&gt; array( array('60" BRAVIA LX900 Series 3D HDTV', 'website1.com', 5299.99), array('52" BRAVIA LX900 Series 3D HDTV', 'website1.com', 4499.99), array('46" BRAVIA LX900 Series 3D HDTV', 'website1.com', 3699.99), array('40" BRAVIA LX900 Series 3D HDTV', 'website1.com', 2999.99) ), "website2.com" =&gt; array( array('Sony 3D 60" LX900 HDTV BRAVIA', 'website2.com', 5400.99), array('Sony 3D 52" LX900 HDTV BRAVIA', 'website2.com', 4699.99), array('Sony 3D 46" LX900 HDTV BRAVIA', 'website2.com', 3899.99), ) ); $_Prices = array(); $_PricesTemp = array(); $_Sites = array("website1.com", "website2.com"); for($i = 0; $i &lt; sizeOf($_Sites); $i++) { $_PricesTemp = array_merge($_PricesTemp, $_Retreived[ $_Sites[$i] ]); } /* print_r($_PricesTemp); Array ( [0] =&gt; Array ( [0] =&gt; 60" BRAVIA LX900 Series 3D HDTV [1] =&gt; website1.com [2] =&gt; 5299.99 ) [1] =&gt; Array ( [0] =&gt; 52" BRAVIA LX900 Series 3D HDTV [1] =&gt; website1.com [2] =&gt; 4499.99 ) [2] =&gt; Array ( [0] =&gt; 46" BRAVIA LX900 Series 3D HDTV [1] =&gt; website1.com [2] =&gt; 3699.99 ) [3] =&gt; Array ( [0] =&gt; 40" BRAVIA LX900 Series 3D HDTV [1] =&gt; website1.com [2] =&gt; 2999.99 ) [4] =&gt; Array ( [0] =&gt; Sony 3D 60" LX900 HDTV BRAVIA [1] =&gt; website2.com [2] =&gt; 5400.99 ) [5] =&gt; Array ( [0] =&gt; Sony 3D 52" LX900 HDTV BRAVIA [1] =&gt; website2.com [2] =&gt; 4699.99 ) [6] =&gt; Array ( [0] =&gt; Sony 3D 46" LX900 HDTV BRAVIA [1] =&gt; website2.com [2] =&gt; 3899.99 ) ) */ foreach($_PricesTemp As $_KeyOne =&gt; $_EntryOne) { foreach(array_reverse($_PricesTemp, true) As $_KeyTwo =&gt; $_EntryTwo) { if ($_KeyOne != $_KeyTwo) { $_Percent = 0; similar_text(strtoupper($_EntryOne[0]), strtoupper($_EntryTwo[0]), $_Percent); if ($_Percent &gt;= 90) //If names matches 90%+ { echo "Similar : &lt;b&gt;" . $_KeyOne . "&lt;/b&gt; " . $_EntryOne[0] . " and &lt;b&gt;" . $_KeyTwo . "&lt;/b&gt; " . $_EntryTwo[0] . " Percent : " . $_Percent . "&lt;br /&gt;"; $_Prices[] = array(); $_Prices[ sizeOf($_Prices)-1 ]['Name'] = $_EntryOne[0]; //Use the product name of the most revelant website (website1.com) foreach($_Sites As $_Site) { if (isset($_EntryOne[ 1 ]) &amp;&amp; $_EntryOne[ 1 ] == $_Site) //Check if it contains price from website1.com { $_Prices[ sizeOf($_Prices)-1 ][ $_Site ] = $_EntryOne[ 2 ]; } if (isset($_EntryTwo[ 1 ]) &amp;&amp; $_EntryTwo[ 1 ] == $_Site) //Check if it contains price from website2.com { $_Prices[ sizeOf($_Prices)-1 ][ $_Site ] = $_EntryTwo[ 2 ]; } } } } } } /* print_r($_Prices); Array ( [0] =&gt; Array ( [Name] =&gt; 60" BRAVIA LX900 Series 3D HDTV [website1.com] =&gt; 2999.99 ) [1] =&gt; Array ( [Name] =&gt; 60" BRAVIA LX900 Series 3D HDTV [website1.com] =&gt; 3699.99 ) [2] =&gt; Array ( [Name] =&gt; 60" BRAVIA LX900 Series 3D HDTV [website1.com] =&gt; 4499.99 ) [3] =&gt; Array ( [Name] =&gt; 52" BRAVIA LX900 Series 3D HDTV [website1.com] =&gt; 2999.99 ) [4] =&gt; Array ( [Name] =&gt; 52" BRAVIA LX900 Series 3D HDTV [website1.com] =&gt; 3699.99 ) [5] =&gt; Array ( [Name] =&gt; 52" BRAVIA LX900 Series 3D HDTV [website1.com] =&gt; 5299.99 ) [6] =&gt; Array ( [Name] =&gt; 46" BRAVIA LX900 Series 3D HDTV [website1.com] =&gt; 2999.99 ) [7] =&gt; Array ( [Name] =&gt; 46" BRAVIA LX900 Series 3D HDTV [website1.com] =&gt; 4499.99 ) [8] =&gt; Array ( [Name] =&gt; 46" BRAVIA LX900 Series 3D HDTV [website1.com] =&gt; 5299.99 ) [9] =&gt; Array ( [Name] =&gt; 40" BRAVIA LX900 Series 3D HDTV [website1.com] =&gt; 3699.99 ) [10] =&gt; Array ( [Name] =&gt; 40" BRAVIA LX900 Series 3D HDTV [website1.com] =&gt; 4499.99 ) [11] =&gt; Array ( [Name] =&gt; 40" BRAVIA LX900 Series 3D HDTV [website1.com] =&gt; 5299.99 ) [12] =&gt; Array ( [Name] =&gt; Sony 3D 60" LX900 HDTV BRAVIA [website2.com] =&gt; 3899.99 ) [13] =&gt; Array ( [Name] =&gt; Sony 3D 60" LX900 HDTV BRAVIA [website2.com] =&gt; 4699.99 ) [14] =&gt; Array ( [Name] =&gt; Sony 3D 52" LX900 HDTV BRAVIA [website2.com] =&gt; 3899.99 ) [15] =&gt; Array ( [Name] =&gt; Sony 3D 52" LX900 HDTV BRAVIA [website2.com] =&gt; 5400.99 ) [16] =&gt; Array ( [Name] =&gt; Sony 3D 46" LX900 HDTV BRAVIA [website2.com] =&gt; 4699.99 ) [17] =&gt; Array ( [Name] =&gt; Sony 3D 46" LX900 HDTV BRAVIA [website2.com] =&gt; 5400.99 ) ) */ ?&gt; </code></pre> <p>First of all, the code above isn't working. There must be a logical error somewhere that I can't put my finger on. Also, I don't believe the code will work in case I add a third website to the listing.</p> <p>Any ideas guys? I have been on this since this morning.</p> <p><strong>Edit 2011-02-16:</strong></p> <p>I added a bounty to this question.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload