Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

array_unique

Posted on 2011-02-13
3
Medium Priority
?
515 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 1000 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

782 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