?
Solved

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

Posted on 2015-02-18
2
Medium Priority
?
65 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

770 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