Viewed   108 times

array_key_exists is not working for large multidimensional array. For ex

$arr = array(
    '1' => 10,
    '2' => array(
        '21' => 21,
        '22' => 22,
        '23' => array(
            'test' => 100,
            '231' => 231
        ),
    ),
    '3' => 30,
    '4' => 40
);

array_key_exists('test',$arr) returns 'false' but it works with some simple arrays.

 Answers

4

array_key_exists does NOT work recursive (as Matti Virkkunen already pointed out). Have a look at the PHP manual, there is the following piece of code you can use to perform a recursive search:

<?php
function array_key_exists_r($needle, $haystack)
{
    $result = array_key_exists($needle, $haystack);
    if ($result) return $result;
    foreach ($haystack as $v) {
        if (is_array($v)) {
            $result = array_key_exists_r($needle, $v);
        }
        if ($result) return $result;
    }
    return $result;
}
Tuesday, December 20, 2022
4

You need to give your submit <input> a name or it won't be available using $_POST['submit']:

<p><input type="submit" value="Submit" name="submit" /></p>
Wednesday, August 24, 2022
3

The other responses focus on the differences between the two functions. This is true, but if the source array does not contain null or 0 or "", ... (empty values) values you can benchmark the speed of the two functions:

<?php

function makeRandomArray( $length ) {
    $array = array();
    for ($i = 0; $i < $length; $i++) {
        $array[$i] = rand(1, $length);
    }

    return $array;
}

function benchmark( $count, $function ) {
    $start = microtime(true);
    for ($i = 0; $i < $count; $i++) {
        $function();
    }
    return microtime(true) - $start;
}

$runs = 100000;
$smallLength = 10;
$small = makeRandomArray($smallLength);

var_dump(benchmark($runs, function() {
    global $small, $smallLength;
    array_key_exists(rand(0, $smallLength), $small);
}));
var_dump(benchmark($runs, function() {
    global $small, $smallLength;
    !empty($small[rand(0, $smallLength)]);
}));

Which gave me the following results:

For a small array:

  • array_key_exists: float(0.18357992172241)
  • empty: float(0.072798013687134)
  • isset: float(0.070242881774902)

For a relative big array:

  • array_key_exists: float(0.57489585876465)
  • empty: float(0.0068421363830566)
  • isset: float(0.0069410800933838)

So if it's possible it's faster to use empty or isset.

Saturday, November 19, 2022
 
3

Well, you see, this one is hard, because the general description on the PHP array functions page does not say that this function does what you're looking for.

But you can sort the array using ksort(), and then use this: array_values() . From the page from the PHP manual:

array_values() returns all the values from the input array and indexes numerically the array.

Friday, October 14, 2022
 
1

There are several problems with your sql code (e.g. never mix DISTINCT and GROUP BY, use WHERE in inner select etc). That being said your query should look something like

SELECT regd, Roll_no, Name_of_Student, Test_date,
       English, f_eng, 
       Mizo, f_mz,  
       Hindi, f_hn,  
       Mathematics,  f_maths, 
       SS, f_ss, 
       Science, f_sc, 
       score, fmscore, perc, Rank 
FROM 
(
  SELECT t.*, IF(@p = score, @n, @n := @n + 1) AS Rank, @p := score 
    FROM
  (
    SELECT regd, Roll_no, Name_of_Student, Test_date,
            SUM(IF(Subject = 'English'    , Mark_score, 0)) English,
            SUM(IF(Subject = 'English'    , Full_mark,  0)) f_eng, 
            SUM(IF(Subject = 'Mizo'       , Mark_score, 0)) Mizo,
            SUM(IF(Subject = 'Mizo'       , Full_mark,  0)) f_mz, 
            SUM(IF(Subject = 'Hindi'      , Mark_score, 0)) Hindi,
            SUM(IF(Subject = 'Hindi'      , Full_mark,  0)) f_hn, 
            SUM(IF(Subject = 'Mathematics', Mark_score, 0)) Mathematics, 
            SUM(IF(Subject = 'Mathematics', Full_mark,  0)) f_maths, 
            SUM(IF(Subject = 'SS'         , Mark_score, 0)) SS,
            SUM(IF(Subject = 'SS'         , Full_mark,  0)) f_ss, 
            SUM(IF(Subject = 'Science'    , Mark_score, 0)) Science,
            SUM(IF(Subject = 'Science'    , Full_mark,  0)) f_sc,
            SUM(Full_mark) fmscore,
            SUM(Mark_score) score, 
            SUM(Mark_score) / SUM(Full_mark) * 100 perc 
      FROM cxexam, (SELECT @n := 0, @p := 0) n 
     WHERE Test_date BETWEEN '2013-07-01' AND '2013-07-31'
     GROUP BY regd 
     ORDER BY score DESC
  ) t
) r 

Here is SQLFiddle demo

Now php code

$link = mysql_connect('localhost', 'user', 'password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('dbname', $link);
if (!$db_selected) {
    die ('Can't use db : ' . mysql_error());
}

$sql = "
    SELECT regd, Roll_no, Name_of_Student, Test_date,
           English, f_eng, 
           Mizo, f_mz,  
           Hindi, f_hn,  
           Mathematics,  f_maths, 
           SS, f_ss, 
           Science, f_sc, 
           score, fmscore, perc, Rank 
    FROM 
    (
      SELECT t.*, IF(@p = score, @n, @n := @n + 1) AS Rank, @p := score 
        FROM
      (
        SELECT regd, Roll_no, Name_of_Student, Test_date,
                SUM(IF(Subject = 'English'    , Mark_score, 0)) English,
                SUM(IF(Subject = 'English'    , Full_mark,  0)) f_eng, 
                SUM(IF(Subject = 'Mizo'       , Mark_score, 0)) Mizo,
                SUM(IF(Subject = 'Mizo'       , Full_mark,  0)) f_mz, 
                SUM(IF(Subject = 'Hindi'      , Mark_score, 0)) Hindi,
                SUM(IF(Subject = 'Hindi'      , Full_mark,  0)) f_hn, 
                SUM(IF(Subject = 'Mathematics', Mark_score, 0)) Mathematics, 
                SUM(IF(Subject = 'Mathematics', Full_mark,  0)) f_maths, 
                SUM(IF(Subject = 'SS'         , Mark_score, 0)) SS,
                SUM(IF(Subject = 'SS'         , Full_mark,  0)) f_ss, 
                SUM(IF(Subject = 'Science'    , Mark_score, 0)) Science,
                SUM(IF(Subject = 'Science'    , Full_mark,  0)) f_sc,
                SUM(Full_mark) fmscore,
                SUM(Mark_score) score, 
                SUM(Mark_score) / SUM(Full_mark) * 100 perc 
          FROM cxexam, (SELECT @n := 0, @p := 0) n 
         WHERE Test_date BETWEEN '2013-07-01' AND '2013-07-31'
         GROUP BY regd 
         ORDER BY score DESC
      ) t
    ) r";

$result = mysql_query($sql);
if(!$result) {
    die(mysql_error()); // TODO: better error handling
}

while($row = mysql_fetch_assoc($result)) {
    echo "{$row['regd']} - {$row['Rank']}<br>";
}

Output (as expected):

40 - 1
2  - 2
3  - 2
20 - 3
Monday, October 24, 2022
 
keymone
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share