Solved

array_unique

Posted on 2011-02-13
3
445 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
Comment Utility
<?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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now