Solved

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

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

749 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