Solved

array_unique

Posted on 2011-02-13
3
486 Views
Last Modified: 2012-05-11
Hi guys,

That is Jquery Autocomplete:

<script>
      $(function() {
            var availableTags = [
                                    <? foreach ($product_autocomplete as $key => $value){?>
                                    "<?=$product_autocomplete[$key]['header'][$_lang]?>",
                                    <?}?>
            ];

            $( "#header_<?=$_lang?>" ).autocomplete({

                  source: availableTags
            });
      });
      </script>
My problem is that $product_autocomplete[$key]['header'][$_lang] may have identical(duplicated value). It would be great if autocomplete could be uniqe.

I know that in PHP, there is array_unique function, but if I use foreach (array_unique($product_autocomplete) as $key => $value, I can see only 1 autocompletion offering.
0
Comment
Question by:NiconPhantom
  • 2
3 Comments
 
LVL 8

Expert Comment

by:rationalboss
ID: 34883896
<?php
$items = array();
foreach ($product_autocomplete as $key => $value){
   $items[] = "\"".$product_autocomplete[$key]['header'][$_lang]."\"";
}
$items = array_unique($items);
?>
?>
<script>
      $(function() {
            var availableTags = [<?php echo implode(',',$items); ?>];

            $( "#header_<?=$_lang?>" ).autocomplete({

                  source: availableTags
            });
      });
      </script>
0
 
LVL 8

Accepted Solution

by:
rationalboss earned 250 total points
ID: 34883902
I corrected indentions, and some conventions :)

<?php
$items = array();
foreach ($product_autocomplete as $key => $value){
   $items[] = "\"".$product_autocomplete[$key]['header'][$_lang]."\"";
}
$items = array_unique($items);
?>
<script type="text/javascript">
      $(function() {
            var availableTags = [<?php echo implode(',',$items); ?>];

            $( "#header_<?php echo $_lang; ?>" ).autocomplete({

                  source: availableTags
            });
      });
</script>

Open in new window

0
 

Author Closing Comment

by:NiconPhantom
ID: 34883930
Thank you very much Rationalboss! It was very fast, and provided by you solution just ideal! Thank you Expert-exchange is really nice place, perfect and fast solution as usually!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question