Find key in array is not finding the text while it is there [duplicate]

125
August 28, 2018, at 6:20 PM

This question already has an answer here:

  • array_search return wrong key [duplicate] 4 answers

I have an large array. I need to find the needle of a specific text.

Therefore I use:

$afmeting = array_search('extmax', $lines);

When I echo $afmeting there is nothing there. But when I use print_r on the array I see it is there (needle 41). So what might be wrong?

Array ( [0] => 0 [1] => section [2] => 2 [3] => header [4] => 9 [5] => acadver [6] => 1 [7] => ac1027 [8] => 9 [9] => acadmaintver [10] => 70 [11] => 105 [12] => 9 [13] => dwgcodepage [14] => 3 [15] => ansi_1252 [16] => 9 [17] => requiredversions [18] => 160 [19] => 0 [20] => 9 [21] => lastsavedby [22] => 1 [23] => user [24] => 9 [25] => insbase [26] => 10 [27] => 0 [28] => 20 [29] => 0 [30] => 30 [31] => 0 [32] => 9 [33] => extmin [34] => 10 [35] => 0 [36] => 20 [37] => 0 [38] => 30 [39] => 0 [40] => 9 [41] => extmax [42] => 10 [43] => 260 [44] => 20 [45] => 150 [46] => 30 [47] => 0 [48] => 9 [49] => limmin [50] => 10 [51] => -26 [52] => 20 [53] => -15 [54] => 9 [55] => limmax [56] => 10 [57] => 286 [58] => 20 [59] => 165 [60] => 9 [61] => orthomode [62] => 70 [63] => 0 [64] => 9 [65] => regenmode [66] => 70 [67] => 1 [68] => 9 [69] => fillmode [70] => 70 [71] => 1 [72] => 9 [73] => qtextmode [74] => 70 [75] => 0 [76] => 9 [77] => mirrtext [78] => 70 [79] => 1 [80] => 9 [81] => ltscale [82] => 40 [83] => 1 [84] => 9 [85] => attmode [86] => 70 [87] => 1 [88] => 9 [89] => textsize [90] => 40 [91] => 0.2 [92] => 9 [93] => tracewid [94] => 40 [95] => 05 [96] => 9)

print_r gives: [41] => extmax

var_dump gives: [41]=> string(8) "extmax "

EDIT after alle tips and tricks:

Thank you all for contributing, so when I use this code all should be ok?

$afmeting = array_search('string(8) "extmax "', $lines, true);

But still no value when I echo $afmeting.

Answer 1
$array=array(0=>'0',1=>'section',3=>'header',4=>'extmax');
$afmeting = array_search('extmax', $array);
echo $afmeting;

The error is happening because of your "int" records in the array. If you mark all your array values as string, you will get your array key result. The above code will return you the array key 4. Instead if 0 was an int value it would not return the array key that your needle is located.

A solution to this is after you get your array you use array_map function to convert all your array field to string like below:

$array=array(0=>0,1=>'section',3=>'header',4=>'extmax');
$array = array_map('strval', $array);
$afmeting = array_search('extmax', $array);
echo $afmeting;

This will provide you with the right key.

Edit

Another solution is to use parameter $strict of array_search and set it to true

$array=array(0=>0,1=>'section',3=>'header',4=>'extmax');
$afmeting = array_search('extmax', $array, true);
echo $afmeting;

This will also provide you with the right array key.

More about array_search function you can find here!!!

READ ALSO
web audio player based on cookies - php backend [on hold]

web audio player based on cookies - php backend [on hold]

I want to make a page, which sets cookie with value of a audio file, and title and after an audio has been ended the cookie will be updated and in the web page the audio will be replaced with the new oneI have the following code, but its enter on the ended event:

137
Laravel SQL[23000] Delete error

Laravel SQL[23000] Delete error

There are some problems with delete function on one tableI need a function that will handle the exception for foreign key constraints

167
Insert replace with codeigniter and multiple checkbox

Insert replace with codeigniter and multiple checkbox

I am working on a project, where I am making an access based on the user group, but I have a problem when doing multiple insertthe question is how can I enter a value into the database from the checkbox with the insert replace

192
How to get value by key from multidimensional array from an object attribute as number in PHP

How to get value by key from multidimensional array from an object attribute as number in PHP

How to get value by key from the multidimensional array in PHP

133