Solved

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

Posted on 2015-02-18
2
60 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 30

Accepted Solution

by:
Marco Gasi earned 500 total points
Comment Utility
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
Comment Utility
Hi Marco,

Got it! Thank you!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PHP Variable into a number 3 33
how to us ob_start in a function 11 32
php documentation 4 19
Phone Dialer 5 33
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
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.

762 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

9 Experts available now in Live!

Get 1:1 Help Now