Solved

in_array not doing what I expect it to... where am I going wrong?

Posted on 2015-02-18
2
61 Views
Last Modified: 2015-02-19
I've got an array named $links that I am working with. It is displayed below.

I need to search the array to see if the string "Unaccept this Answer" is in it (which it is, in this case).

I doing this:

<?php
if (in_array("Unaccept this Answer", $links)) {
    echo "Testing This";
}
?>

Even though that string is in there, this is apparently evaluating to false - I don't get anything echo'd.

Where am I going wrong with this... and is there a more efficient way for me to perform this check?

Thanks -   Chris

Array ( [voting_flag] => dexrol_Link Object ( [_url:dexrol_Link:private] => dexrolFramework_Application_Url Object ( [_data:dexrolFramework_Application_Url:private] => Array ( [script_url] => http://www.mySite.com/forum/answers/8/vote/flag/form [params] => Array ( [update_target_id] => dexrol-entity-content-8 ) [fragment] => [separator] => & ) ) [_label:dexrol_Link:private] => [_options:dexrol_Link:private] => Array ( [width] => 470 [icon] => flag [active] => [no_escape] => ) [_attributes:dexrol_Link:private] => Array ( [class] => dexrol-btn dexrol-btn-default dexrol-btn-sm dexrol-voting-btn-flag [title] => Flag this Answer [onclick] => dexrol.ajax({modalWidth:470,trigger:jQuery(this), container:'#dexrol-modal'}); event.stopImmediatePropagation(); return false; [data-dexrol-remote-url] => http://www.mySite.com/forum/answers/8/vote/flag/form?update_target_id=dexrol-entity-content-8 [rel] => dexroltooltip ) ) [edit] => dexrol_Link Object ( [_url:dexrol_Link:private] => dexrolFramework_Application_Url Object ( [_data:dexrolFramework_Application_Url:private] => Array ( [script_url] => http://www.mySite.com/forum/answers/8/edit [params] => Array ( ) [fragment] => [separator] => & ) ) [_label:dexrol_Link:private] => [_options:dexrol_Link:private] => Array ( [icon] => edit [no_escape] => ) [_attributes:dexrol_Link:private] => Array ( [title] => Edit this Answer [class] => dexrol-btn dexrol-btn-default dexrol-btn-sm [rel] => dexroltooltip ) ) [delete] => dexrol_Link Object ( [_url:dexrol_Link:private] => dexrolFramework_Application_Url Object ( [_data:dexrolFramework_Application_Url:private] => Array ( [script_url] => http://www.mySite.com/forum/answers/8/delete [params] => Array ( [delete_target_id] => dexrol-entity-content-8 ) [fragment] => [separator] => & ) ) [_label:dexrol_Link:private] => [_options:dexrol_Link:private] => Array ( [width] => 470 [icon] => trash-o [no_escape] => ) [_attributes:dexrol_Link:private] => Array ( [title] => Delete this Answer [onclick] => dexrol.ajax({modalWidth:470,trigger:jQuery(this), container:'#dexrol-modal'}); event.stopImmediatePropagation(); return false; [data-dexrol-remote-url] => http://www.mySite.com/forum/answers/8/delete?delete_target_id=dexrol-entity-content-8 [class] => dexrol-btn dexrol-btn-default dexrol-btn-sm [rel] => dexroltooltip ) ) [accept] => dexrol_Link Object ( [_url:dexrol_Link:private] => dexrolFramework_Application_Url Object ( [_data:dexrolFramework_Application_Url:private] => Array ( [script_url] => http://www.mySite.com/forum/answers/8/accept [params] => Array ( [update_target_id] => dexrol-entity-content-8 ) [fragment] => [separator] => & ) ) [_label:dexrol_Link:private] => [_options:dexrol_Link:private] => Array ( [width] => 470 [icon] => check-circle [active] => 1 [no_escape] => ) [_attributes:dexrol_Link:private] => Array ( [class] => dexrol-btn dexrol-btn-default dexrol-btn-sm dexrol-btn-success dexrol-active [title] => Unaccept this Answer [onclick] => dexrol.ajax({modalWidth:470,trigger:jQuery(this), container:'#dexrol-modal'}); event.stopImmediatePropagation(); return false; [data-dexrol-remote-url] => http://www.mySite.com/forum/answers/8/accept?update_target_id=dexrol-entity-content-8 [rel] => dexroltooltip ) ) )
0
Comment
Question by:St_Aug_Beach_Bum
2 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 40618344
in_array doesn't work with multidimensional arrays. You can use a pecific function like this one:
	function in_array_r($needle, $haystack) {
    foreach ($haystack as $item) {
        if (($item === $needle) || (is_array($item) && in_array_r($needle, $item))) {
            return true;
        }
    }
    return false;
}		

<?php
if (in_array_r("Unaccept this Answer", $links)) {
    echo "Testing This";
}
?>	

Open in new window

0
 

Author Closing Comment

by:St_Aug_Beach_Bum
ID: 40618884
Hi Marco,

Got it! Thank you!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

772 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