473,402 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

how to use in_array

omerbutt
638 512MB
hi i need to compare a value inside the array that is returned via recordset from database
Expand|Select|Wrap|Line Numbers
  1. Array
  2. (
  3.     [0] => Array
  4.         (
  5.             [email] => ranatouqeer@msn.com
  6.         )
  7.  
  8.     [1] => Array
  9.         (
  10.             [email] => omer@etgi.us
  11.         )
  12.  
  13. )
  14.  
i am trying to use in_array() to search inside the array but in_array() doesnt seem to do so here is the code
Expand|Select|Wrap|Line Numbers
  1. $email=$this->input->post('email');
  2. if(in_array($email,$newsletter_array)){
  3.             echo 'yes';
  4.         }else{
  5.             echo 'no';
  6.         }
  7.  
i have tried using it this way too
Expand|Select|Wrap|Line Numbers
  1. if(in_array(array(array('email'=>$email)),$newsletter_array)){
  2.             echo 'yes';
  3.         }else{
  4.             echo 'no';
  5.         }
  6.  

what am i doing wrong
regards,
Omer Aslam
Sep 17 '12 #1

✓ answered by omerbutt

i did it by using the following method
Expand|Select|Wrap|Line Numbers
  1. function array_value_recursive($key, array $arr){
  2.         $val = array();
  3.         @array_walk_recursive($arr, function($v, $k) use($key, &$val){ if($k == $key) array_push($val, $v);});
  4.         return count($val) > 1 ? $val : array_pop($val);
  5.     }
  6. $new_array=array_value_recursive('email',$newsletter_array);
  7.  
  8.             if(in_array($email,$new_array)){
  9. echo 'matched';
  10. }else{
  11. echo 'not found';
  12. }
  13.  
regards,
Omer Aslam

1 1430
omerbutt
638 512MB
i did it by using the following method
Expand|Select|Wrap|Line Numbers
  1. function array_value_recursive($key, array $arr){
  2.         $val = array();
  3.         @array_walk_recursive($arr, function($v, $k) use($key, &$val){ if($k == $key) array_push($val, $v);});
  4.         return count($val) > 1 ? $val : array_pop($val);
  5.     }
  6. $new_array=array_value_recursive('email',$newsletter_array);
  7.  
  8.             if(in_array($email,$new_array)){
  9. echo 'matched';
  10. }else{
  11. echo 'not found';
  12. }
  13.  
regards,
Omer Aslam
Sep 17 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: AJ Z | last post by:
I am using in_array() to search for a value ("other"), in order to validate a form. If I pass $_POST as the array to search PHP says that it is an invalid datatype. It is an array and if I copy...
2
by: Mountain Man | last post by:
Hi, The in_array function sometimes malfunctions on my computer yielding the error message shown below. The code shown below usually works, but sometimes doesn't. This even happens with the same...
3
by: Phil Powell | last post by:
if (is_array($_POST)) { foreach ($this->getAssocSectionsObjArray($key, $dbAP) as $obj) { print_r($obj); print_r(" in array? "); print_r(in_array($obj, $result)); print_r("<P>"); if...
6
by: Craig Keightley | last post by:
I am trying to compare values of a string entered into an array but having no results, is this possible to achieve: <?php $ids = $row_rsProduct; // A comma separated list of values...
5
by: Phil Powell | last post by:
Here is my array. Plain and simple enumerative array with values being strings. So why does this fail??? print_r(in_array('album', array_keys($boolword))); This produces FALSE or NULL....
4
by: paul brown | last post by:
hello, I have this code sample: class Sexboard extends Master { var $userid; var $errormessage; var $dc_metro = array(20, 46, 51);
3
by: Tom Barnes | last post by:
Check out this code: // Start Code ------------- function test_in_array($val) { $a = array('key' => $val); printf("in_array: %d, value:%s<BR>", in_array('key', $a), $a); } test_in_array(0);...
6
by: Berimor | last post by:
Hi, i've been always used the in_array() function to check availabylity of a value in array. Until today. Simple code: (do not run it tho :) ) <? function getmicrotime(){ list($usec, $sec) =...
3
by: rich | last post by:
I keep getting the error: Warning: in_array(): Wrong datatype for second argument on line 679 here is the code. I declare and fill the array $specprimA = array(); $specprimA=...
3
by: Sonnich | last post by:
The following code is an exact copy of my current code. The idea is to check 2 parts (of string arrays), and add those only once to a common array. I check for existance of an array in a string,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.