Solved

array_unique

Posted on 2011-02-13
3
506 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 …

623 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