Solved

array_unique

Posted on 2011-02-13
3
466 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

920 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

16 Experts available now in Live!

Get 1:1 Help Now