2

I have an XML string resulting from an SQL query that looks like the following (shortened). My website is set up to fetch this from SQL and $objRev->reviews->countries would give me the content of the countries part for each of the reviews in the XML.

Countries only contains text (country names) separated by comma and space.

In the below example my input variables would be:

var1 = "Austria, Belgium, Bulgaria, Croatia, Cyprus";
var2 = "Brazil, Sweden, United Kingdom";

Expected output: "Austria, Belgium, Brazil, Bulgaria, Croatia, Cyprus, Sweden, United Kingdom"

Can someone tell me how I can use PHP to combine the content of these variables in an array and sort them alphabetically ? My challenge here is that there are not always two variables, it can also be one or more as this depends on whether the review date matches a given input, here: 2014-04-21 - the indicator for this is dateMatch = 'Yes'.

<root>
  <reviews>
    <review>2014-04-21</review>
    <revLong>21 Apr 2014</revLong>
    <revShort>2014-04-21</revShort>
    <dayDiff>0</dayDiff>
    <revStatus>past</revStatus>
    <countries>Austria, Belgium, Bulgaria, Croatia, Cyprus</countries>
    <dateMatch>Yes</dateMatch>
    <countryMatch>Yes</countryMatch>
  </reviews>
  <reviews>
    <review>2014-04-21</review>
    <revLong>21 Apr 2014</revLong>
    <revShort>2014-04-21</revShort>
    <dayDiff>0</dayDiff>
    <revStatus>past</revStatus>
    <countries>Brazil, Sweden, United Kingdom</countries>
    <dateMatch>Yes</dateMatch>
    <countryMatch>No</countryMatch>
  </reviews>
</root>

Many thanks for any help with this, Tim.

user2571510
  • 11,167
  • 39
  • 92
  • 138

2 Answers2

1

Something simple like this.. Use simplexml_load_string()

$xml = simplexml_load_string($xml);
foreach ($xml->reviews as $child)
{
        $cnt[]=explode(',',$child->countries);
}
$arr = array_map('trim',call_user_func_array('array_merge',$cnt));
sort($arr);
echo implode(', ',$arr);

OUTPUT :

Austria, Belgium, Brazil, Bulgaria, Croatia, Cyprus, Sweden, United Kingdom

Demonstration

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
  • 1
    Thanks a lot, this works great - I just had to replace the foreach in your answer as follows to limit this to the dates I am interested in: if($child->dateMatch == "Yes") { $cnt[] = explode(",",$child->countries); } - can you explain what "call_user_func_array" does here ? – user2571510 May 01 '14 at 10:03
  • 1
    @user2571510, The results are generated under nested arrays, so the `call_user_func_array` uses an `array_merge` to merge those arrays. – Shankar Narayana Damodaran May 01 '14 at 10:06
  • Thanks again ! One more question on this: If I need to add something to each of the countries (e.g. an img tag before each country name), how would I do this using your solution ? – user2571510 May 01 '14 at 10:15
  • 1
    You can use an `array_walk` for that.. Add this `array_walk($arr,function (&$v){ $v = ''.$v;});` after the `sort($arr);` – Shankar Narayana Damodaran May 01 '14 at 10:20
  • Thanks. I would need to do this before the sort, maybe even in the foreach as each image will have the same name as its country - sorry, I didn't mention that before. – user2571510 May 01 '14 at 10:22
  • Australia, Belgium, Brazil ... – user2571510 May 01 '14 at 10:40
  • 1
    You need to use this after the `sort()` call ... `array_walk($arr,function (&$v){ $v = "".$v;});` – Shankar Narayana Damodaran May 01 '14 at 10:43
0

Try with array_merge, sort and array_unique functions:

$all = array_unique(sort(array_merge($var1, $var2)));
R.Sicart
  • 671
  • 3
  • 10