0

I am trying to create a function that is able to check an array for a certain value and if that value exists then to return the value of the index.

I have the following code that queries the db and puts the result in an array but when i pass that array into the function nothing is returned for some reason.

Can someone please help me with this.

    $conn = mysql_connect($host, $user, $pass); 
    $conn = mysql_select_db($db, $conn);

    $sql="SELECT DATE_FORMAT(due_date,'%d') AS day,short,tid FROM tasks WHERE due_date BETWEEN '2014-8-01' AND '2014-8-31'";    

    $result=mysql_query($sql);

    $num = mysql_num_rows($result);

    for ($i = 0; $i < $num; $i++)
    {
        $events[] = mysql_fetch_assoc($result);
    }

    var_dump($events);
    echo "Check Array results is " . checkArray("15", $events);

    function checkArray($day, &$arr){
        $key = array_search($day, $arr);
        echo "the key is ". $key;
        return $key;
    }

This is the output of var_dump($events)

array(2) { 
    [0]=> array(3) {
        ["day"]=> string(2) "15"
        ["short"]=> string(6) "test 1"
        ["tid"]=> string(1) "1"
    }
    [1]=> array(3) {
        ["day"]=> string(2) "31"
        ["short"]=> string(6) "test 2"
        ["tid"]=> string(1) "2"
    }
} 
4
  • Why are you just not checking for the day in the MySQL statement, why this extra step? Commented Aug 14, 2014 at 13:20
  • 1
    You have an array of arrays. You would need to loop through them to get the key with the corresponding value. Commented Aug 14, 2014 at 13:23
  • Can't you just add WHERE day = [yourValue] directly in your SQL request ? Commented Aug 14, 2014 at 13:33
  • I need to get all data in between those dates, its for a calendar app Commented Aug 14, 2014 at 15:01

2 Answers 2

1

You can use this for your function checkArray() and it will return the first matching key or false if the day is not found,

function checkArray($day, $array){
    foreach ($array as $key => $value)
    {
        if (array_search($day, $value)) 
            return $key;  
     }
     return false; 
};

However, why don't you just add day = '15' in your SQL?

Sign up to request clarification or add additional context in comments.

1 Comment

that worked perfect thanks, i don't do that in my sql, because i need to all the dates within that month, i just used 15 for testing, i actually have a loop that goes through all the days
0

You want a recursive search of the array, if your array is more than one dimension.

array(2) { 
    [0]=> array(3) { 
        ["day"]=> string(2) "15" 
        ["short"]=> string(6) "test 1" 
        ["tid"]=> string(1) "1" 
    } 
    [1]=> array(3) { 
        ["day"]=> string(2) "31" 
        ["short"]=> string(6) "test 2" 
        ["tid"]=> string(1) "2" 
    } 
}


<?php
    function recursive_array_search($needle,$haystack) {
        foreach($haystack as $key=>$value) {
            $current_key=$key;
            if($needle===$value OR (is_array($value) && 
                recursive_array_search($needle,$value) !== false)) {

                return $current_key;
            }
        }
        return false;
    }
 ?>

I took this from array_search.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.