1

nI´m trying to retrieve the numeric index of a particular column in my database. I need to access it from both numeric and associative indexes, so I need the fetch to be BOTH.

Here is the sample code:

$data = mysqli_query($con, $sql) or die(mysql_error());
$student = mysqli_fetch_array($data) or die(mysql_error());

$index = array_search("columnName", array_keys($student));

It always returns 0 no matter what "columnName" I place into it. I managed to work this around with a loop or having two arrays, one numeric and other associative, but I´d like to understand why does´t this work...

6
  • What does array_keys($student) return? Commented Jun 2, 2014 at 0:59
  • It returns an array where the keys are my table´s numeric index and the values are my column names. If I call it in a foreach (array_keys($student) as $keys=>$values) echo $keys.$values they display normally. Commented Jun 2, 2014 at 1:05
  • My guess is that is what you want it to be. Please do var_dump(array_keys($student)) and show us what it is Commented Jun 2, 2014 at 1:09
  • array(46) { [0]=> int(0) [1]=> string(2) "id" [2]=> int(1) [3]=> string(4) "nome" [4]=> int(2) [5]=> string(9) "sobrenome" [6]=> int(3) [7]=> string(5) "idade" [8]=> int(4) [9]=> string(11) "instrumento" [10]=> int(5) [11]=> string(3) "dia" [12]=> int(6) } Commented Jun 2, 2014 at 1:14
  • Might this [0]=>int(0) be the problem? Commented Jun 2, 2014 at 1:15

3 Answers 3

3

This (array_search) may returns Boolean false on failure or integer(index of item) value:

Warning This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

For example, this will return 0 but it's the first index (found):

$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('blue', $array); // 0
if(getType($key) == 'boolean'){
    // not found
}

Result is 0 here and if it's integer then it's the index of first item. So make sure that, whether the 0 is found index or not. You may set the third parameter to true to overcome this problem:

strict If the third parameter strict is set to TRUE then the array_search() function will search for identical elements in the haystack. This means it will also check the types of the needle in the haystack, and objects must be the same instance.

So, using a strict comparison you may ensure the appropriate result:

$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('blue', $array, true);
if($key !== false) {
    // Found the index in $key
}
Sign up to request clarification or add additional context in comments.

Comments

2

The problem is that you have a value that is 0. Because 0 == 'id'. But you can set the third parameter to true to make a strict match. So it would be:

$index = array_search('columnName', array_keys($student), true);

3 Comments

PHP will case 'id' to an int which is 0 and 0 == 0. true means that === is used which will compare that the types are the same also
And why does PHP change 'id' to INT 0? Thank you very much, by the way!
Because when you use == PHP tries to make them into the same type before comparing them.
0

How about this ?:

$arrayMap = array();
foreach( array_keys( $array ) as $index => $key ) {
     $arrayMap[] = array(
          'index' => $index,//position 
          'key' => $key,//text key
          'value' => $array[$key]  //value
     );
}

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.