array_unique doesn't seem to be working - php

After I use array_unique on an array, when I var_dump the array, it still has the same content, with duplicates:
array(21) {
[0]=> string(10) "tricou_CRS"
[1]=> string(10) "tricou_CRM"
[2]=> string(11) "tricou_CRXL"
[3]=> string(10) "tricou_CBM"
[4]=> string(10) "tricou_CBL"
[5]=> string(10) "tricou_CWS"
[6]=> string(11) "tricou_CWXL"
[7]=> string(10) "tricou_CRS"
[8]=> string(10) "tricou_CRM"
[9]=> string(11) "tricou_CRXL"
[10]=> string(10) "tricou_CBM"
[11]=> string(10) "tricou_CBL"
[12]=> string(10) "tricou_CWS"
[13]=> string(11) "tricou_CWXL"
[14]=> string(10) "tricou_CRS"
[15]=> string(10) "tricou_CRM"
[16]=> string(11) "tricou_CRXL"
[17]=> string(10) "tricou_CBM"
[18]=> string(10) "tricou_CBL"
[19]=> string(10) "tricou_CWS"
[20]=> string(11) "tricou_CWXL" }
Which obviously has some duplicates. Now, there's not much code that could help, really, because it's just array_unique($myarr);var_dump($myarr);.
So, what am I missing there? Shouldn't array_unique remove the duplicates? Both the type and the content is the same in many of the array's positions.

$uniquearr = array_unique($myarr);
var_dump($uniquearr);
Always read docs first
http://www.php.net/array_unique
array_unique
Return Values
Returns the filtered array.

Related

Wordpress admin submenu items array returns skipped value's

i returned a array off all my admin submenu items. but this returns like this:
array(2) { [0]=> string(4) "read" [10]=> string(11) "update_core" } array(0) { } array(4) { [5]=> string(10) "edit_posts" [10]=> string(10) "edit_posts" [15]=> string(17) "manage_categories" [16]=> string(16) "manage_post_tags" } array(3) { [5]=> string(12) "upload_files" [10]=> string(12) "upload_files" [15]=> string(17) "manage_categories" } array(2) { [5]=> string(10) "edit_pages" [10]=> string(10) "edit_pages" } array(0) { } array(4) { [5]=> string(10) "edit_posts" [10]=> string(10) "edit_posts" [15]=> string(17) "manage_categories" [16]=> string(17) "manage_categories" } array(3) { [5]=> string(10) "edit_posts" [10]=> string(10) "edit_posts" [15]=> string(17) "manage_categories" } array(0) { } array(8) { [0]=> string(18) "edit_theme_options" [1]=> string(19) "edit_us_page_blocks" [2]=> string(19) "edit_us_page_blocks" [3]=> string(19) "edit_us_page_blocks" [4]=> string(19) "edit_us_page_blocks" [5]=> string(14) "manage_options" [6]=> string(14) "manage_options" [7]=> string(14) "manage_options" } array(4) { [5]=> string(13) "switch_themes" [6]=> string(9) "customize" [7]=> string(18) "edit_theme_options" [10]=> string(18) "edit_theme_options" } array(2) { [5]=> string(16) "activate_plugins" [10]=> string(15) "install_plugins" } array(3) { [5]=> string(10) "list_users" [10]=> string(12) "create_users" [15]=> string(4) "read" } array(7) { [5]=> string(10) "edit_posts" [10]=> string(6) "import" [15]=> string(6) "export" [20]=> string(23) "view_site_health_checks" [25]=> string(27) "export_others_personal_data" [30]=> string(26) "erase_others_personal_data" [50]=> string(13) "setup_network" } array(5) { [0]=> string(14) "manage_options" [1]=> string(14) "manage_options" [2]=> string(14) "manage_options" [3]=> string(14) "manage_options" [5]=> string(10) "edit_posts" } array(7) { [10]=> string(14) "manage_options" [15]=> string(14) "manage_options" [20]=> string(14) "manage_options" [25]=> string(14) "manage_options" [30]=> string(14) "manage_options" [40]=> string(14) "manage_options" [45]=> string(22) "manage_privacy_options" } array(3) { [0]=> string(14) "manage_options" [1]=> string(14) "manage_options" [2]=> string(14) "manage_options" } array(0) { } array(0) { } array(2) { [0]=> string(14) "manage_options" [1]=> string(4) "read" }
php code:
$subMenuItems = wp_list_pluck($submenu_list, 1);
var_dump($subMenuItems);
i know this array is long, is it me or is this broken
i hope that someone can explain why the array items are not like [0] => .... [1] => .... but random
According to the documentation:
wp_list_pluck( array $list, int|string $field, int|string $index_key = null ): array
Array of found values. If $index_key is set, an array of found values with keys corresponding to $index_key. If $index_key is null, array keys from the original $list will be preserved in the results.
You need to add a third parameter, if you want to conserve indexed keys order.
Regards,

Can't sort array items by value ASC

I get all keywords separated by commas from database and create arrays from them then I merge all arrays. From the resulted array I keep only unique items array_unique. The problem is that I can't sort resulted array by value.
Here is my code:
$select_all_keywords = mysqli_query($db_connect, "SELECT `keywords` FROM `bookmarks`") or die(mysqli_error());
$keywords_array = array();
while($keywords = mysqli_fetch_assoc($select_all_keywords))
{
$explode_keywords = explode(", ", $keywords['keywords']);
$keywords_array = array_merge($keywords_array, $explode_keywords);
}
$unique_keywords = array_unique($keywords_array);
sort($unique_keywords);
$unique_keywords = array_values($unique_keywords);
print_array($unique_keywords);
The printed array:
array(23) {
[0]=>
string(10) "Awolnation"
[1]=>
string(7) "Belgium"
[2]=>
string(7) "Gravity"
[3]=>
string(21) "Nervo (Musical Group)"
[4]=>
string(5) "R3..."
[5]=>
string(22) "R3hab (Musical Artist)"
[6]=>
string(5) "Remix"
[7]=>
string(4) "Sail"
[8]=>
string(30) "Tomorrowland (Recurring Event)"
[9]=>
string(21) "Tomorrowland Festival"
[10]=>
string(9) "Unlimited"
[11]=>
string(6) "dasdas"
[12]=>
string(10) "freshbooks"
[13]=>
string(11) "gdsfgdsfgds"
[14]=>
string(6) "mockup"
[15]=>
string(3) "php"
[16]=>
string(11) "programming"
[17]=>
string(10) "revolution"
[18]=>
string(17) "tomorrowland 2013"
[19]=>
string(11) "ummet ozcan"
[20]=>
string(10) "web design"
[21]=>
string(7) "wikihow"
[22]=>
string(3) "xml"
}
I tryed almost all sorting array functions. None of them helped me sort the array by value ASC.
I believe you might be looking for asort(), it will sort the array by value. But still keep the keys the same.
Look at the documentation here.
http://php.net/manual/en/function.asort.php

How to read var_dump in order to know how to load its variable contents in to an array with a foreach?

Here is a sample var_dump. Now, how do I know how to construct a foreach from it to load any particular word or fragment in to an array?
object(stdClass)#1 (2)
{
["noun"]=>
object(stdClass)#2 (1)
{
["syn"]=> array(24)
{
[0]=> string(12) "domestic dog"
[1]=> string(16) "Canis familiaris"
[2]=> string(5) "frump"
[3]=> string(3) "cad"
[4]=> string(7) "bounder"
[5]=> string(10) "blackguard"
[6]=> string(5) "hound"
[7]=> string(4) "heel"
[8]=> string(5) "frank"
[9]=> string(11) "frankfurter"
[10]=> string(6) "hotdog"
[11]=> string(7) "hot dog"
[12]=> string(6) "wiener"
[13]=> string(11) "wienerwurst"
[14]=> string(6) "weenie"
[15]=> string(4) "pawl"
[16]=> string(6) "detent"
[17]=> string(5) "click"
[18]=> string(7) "andiron"
[19]=> string(7) "firedog"
[20]=> string(8) "dog-iron"
[21]=> string(8) "blighter"
[22]=> string(5) "canid"
[23]=> string(6) "canine"
[24]=> string(5) "catch"
}
}
}
Before we can decipher it, we have to format it.
Tobias Kun's answer shows a very good way to format the var_dump output so you can read it.
object(stdClass)#1 (2)
{
["noun"]=>
object(stdClass)#2 (1)
{
["syn"]=> array(24)
{
[0]=> string(12) "domestic dog"
[1]=> string(16) "Canis familiaris"
[2]=> string(5) "frump"
[3]=> string(3) "cad"
[4]=> string(7) "bounder"
[5]=> string(10) "blackguard"
[6]=> string(5) "hound"
[7]=> string(4) "heel"
[8]=> string(5) "frank"
[9]=> string(11) "frankfurter"
[10]=> string(6) "hotdog"
[11]=> string(7) "hot dog"
[12]=> string(6) "wiener"
[13]=> string(11) "wienerwurst"
[14]=> string(6) "weenie"
[15]=> string(4) "pawl"
[16]=> string(6) "detent"
[17]=> string(5) "click"
[18]=> string(7) "andiron"
[19]=> string(7) "firedog"
[20]=> string(8) "dog-iron"
[21]=> string(8) "blighter"
[22]=> string(5) "canid"
[23]=> string(6) "canine"
[24]=> string(5) "catch"
}
}
}
You have a stdClass object with a property called ,"noun". noun is an abject with a property called "syn", which is an array of strings.
Suppose we call the object $object. Then we can access the array like:
echo $object->noun->syn[23];
which gives us canine. So a loop might look like:
foreach($data->noun->syn as $value) {
echo $value . "<br>\n";
}
First of all you should really increase the quality of your questions. The code is not formatted at all.
If you use echo "<pre>" . print_r($your_data_object_or_array,1) . "</pre>" your data will be formatted fine.
If i understand you right this should help you:
foreach($data['noun']['syn'] as $value) {
//with this you loop through all your words in "syn" e.g. domestic, "Canis familiaris etc."
echo $value . "<br>";
}
//Ouput:
domestic
Canis familiaris
frump
cad
etc ...

Fixing multidimensional array

I'm trying to make a multidimensional array that should print out like this (without formatting):
array(3) {
[0]=> array(5) {
[0]=> int(0)
[1]=> string(5) "Arena"
[2]=> string(18) "2012-05-3017:00:00"
[3]=> string(18) "2012-05-3000:00:00"
[4]=> string(33) "Masquerade Checkin (Participants)"
},
[1]=> array(5) {
[0]=> int(0)
[1]=> string(10) "Workshop 1"
[2]=> string(18) "2012-05-3017:00:00"
[3]=> string(18) "2012-05-3000:00:00"
[4]=> string(15) "Death Note (Live)"
},
[2]=> array(5) {
[0]=> int(0)
[1]=> string(7) "Video 6"
[2]=> string(18) "2012-05-3017:00:00"
[3]=> string(18) "2012-05-3000:00:00"
[4]=> string(26) "Takeuchi Fan Panel"
}
}
Notice from the above code that the inner array() length is always 5.
Here is my code below:
$loopsArray = array();
$data=array();
// graphing info come in here.
foreach ($events as $key => $event) {
$el=$event['event_location'] ;
$eln=$event['event_locationName'];
$ed=$event['event_date'];
$es=$event['event_start'];
$ee=$event['event_end'];
$en=$event['event_name'];
array_push($loopsArray,$el,$eln, $ed.$es,$ed.$ee,$en);
array_push($data,$loopsArray);
}
var_dump($data);
Here the print out
array(27) {
[0]=> array(5) {
[0]=> int(0)
[1]=> string(5) "Arena"
[2]=> string(18) "2012-05-3017:00:00"
[3]=> string(18) "2012-05-3000:00:00"
[4]=> string(33) "Masquerade Checkin (Participants)"
}
[1]=> array(10) {
[0]=> int(0)
[1]=> string(5) "Arena"
[2]=> string(18) "2012-05-3017:00:00"
[3]=> string(18) "2012-05-3000:00:00"
[4]=> string(33) "Masquerade Checkin (Participants)"
[5]=> int(13)
[6]=> string(11) "Autograph 1"
[7]=> string(18) "2012-06-2419:00:00"
[8]=> string(18) "2012-06-2422:00:00"
[9]=> string(17) "Parents and Anime"
}
//... continues
}
Notice that the inner arrays length double each iteration. array(5) array(10) array(15)array(20).
It doubles up to 60 elements in the last inner array. Each inner array should only have 5 elements in them. I don't understand why it is doubling or how to fix it.
Can you look over my loop and let me know how to fix it?
I have to use this multidimensional array for this code to work in JpGraph.
TIP : write $loopsArray = array(); inside foreach
better approach
instead of
array_push($loopsArray,$el,$eln, $ed.$es,$ed.$ee,$en);
array_push($data,$loopsArray);
try this
$temp = array ($el,$eln, $ed.$es,$ed.$ee,$en);
$data[] = $temp;

Google Analytics doesnt track any data

i am working on a custom Google Analytics implementation making use of direct utm.gif requests.
The problem:
Google Analytics doesnt track any data. Maybe because sth. is wrong with the parameters?
Thank's for helping
Sample: trackPage parameters
array(16) {
[0]=>
string(53) "http://www.google-analytics.com/__utm.gif?utmwv=4.5.7"
[1]=>
string(14) "utmn=474288553"
[2]=>
string(34) "utmhn=labs.domain.de%2Fphp%2Fga%2F"
[3]=>
string(16) "utmcs=ISO-8859-1"
[4]=>
string(11) "utmul=de-de"
[5]=>
string(7) "utmsr=-"
[6]=>
string(7) "utmsc=-"
[7]=>
string(7) "utmje=-"
[8]=>
string(7) "utmfl=-"
[9]=>
string(6) "utmr=-"
[10]=>
string(70) "utme=8%28userstatus%2Amyvariable%299%28nofan%2Amyvalue%2911%281%2A1%29"
[11]=>
string(14) "utmdt=Homepage"
[12]=>
string(16) "utmhid=441842675"
[13]=>
string(12) "utmp=home%2F"
[14]=>
string(17) "utmac=UA-123456-1"
[15]=>
string(191) "utmcc=__utma%253D186215409.1789216404.1265552708.1288611723.1288619999.26%253B%252B__utmz%253D186215409.1288619999.1.1.utmcsr%3D%28direct%29%7Cutmccn%3D%28direct%29%7Cutmcmd%3D%28none%29%253B"
}
Sample: trackEvent parameters
array(15) {
[0]=>
string(53) "http://www.google-analytics.com/__utm.gif?utmwv=4.5.7"
[1]=>
string(14) "utmn=240340321"
[2]=>
string(34) "utmhn=labs.domain.de%2Fphp%2Fga%2F"
[3]=>
string(10) "utmt=event"
[4]=>
string(16) "utmcs=ISO-8859-1"
[5]=>
string(11) "utmul=de-de"
[6]=>
string(7) "utmsr=-"
[7]=>
string(7) "utmsc=-"
[8]=>
string(7) "utmje=-"
[9]=>
string(7) "utmfl=-"
[10]=>
string(6) "utmr=-"
[11]=>
string(116) "utme=5%28permission%2Apublish_stream%2Agranted%29%281%298%28userstatus%2Amyvariable%299%28%2Amyvalue%2911%281%2A1%29"
[12]=>
string(16) "utmhid=364249414"
[13]=>
string(17) "utmac=UA-123456-1"
[14]=>
string(191) "utmcc=__utma%253D186215409.1789216404.1265552708.1288611723.1288620331.26%253B%252B__utmz%253D186215409.1288620331.1.1.utmcsr%3D%28direct%29%7Cutmccn%3D%28direct%29%7Cutmcmd%3D%28none%29%253B"
}
This one is generated with another lib and it's working. But i can't find my fault?
array(14) {
[0]=>
string(53) "http://www.google-analytics.com/__utm.gif?utmwv=4.5.7"
[1]=>
string(15) "utmn=8893029624"
[2]=>
string(18) "utmhn=labs.domain.de%2Fphp%2Fga%2F"
[3]=>
string(7) "utmcs=-"
[4]=>
string(7) "utmsr=-"
[5]=>
string(7) "utmsc=-"
[6]=>
string(11) "utmul=de-de"
[7]=>
string(7) "utmje=-"
[8]=>
string(7) "utmfl=-"
[9]=>
string(14) "utmdt=Homepage"
[10]=>
string(6) "utmr=-"
[11]=>
string(10) "utmp=/home"
[12]=>
string(17) "utmac=UA-123456-1"
[13]=>
string(199) "utmcc=__utma%3D155417661.238248693.1267644354.1279870281.1279921084.8%3B%2B__utmz%3D155417661.1279824753.5.6.utmccn=(organic)%7Cutmcsr=google%7Cutmctr=facebook%20goole%20analytics%7Cutmcmd=organic%3B"
}

Categories