Solved

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

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Suggested Solutions

Title # Comments Views Activity
Read a remote csv file from a https URL 8 47
what do I need to host my own web sites? 13 49
Wordpress Cron - Send XML Data to external server FTP 3 21
Ajax and PHP 9 29
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to dynamically set the form action using jQuery.
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.

791 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