1

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.

Muiter
  • 1,470
  • 6
  • 26
  • 39
  • 6
    Please copy and paste the array. Don't attach images as we can't use them – pr1nc3 Aug 28 '18 at 07:29
  • 3
    Use `var_dump` to see more information of your array, `print_r` can be misleading. – deceze Aug 28 '18 at 07:31
  • 1
    @Muiter, a lesson for the future, try to paste the output of the array in one row using *echo "
    " just before the statement. It's not really helpful to read a extensively big array in one line like that.
    – Rafael Aug 28 '18 at 07:35
  • 2
    The *needle* is generally the term used for the subject to search (`'extmax'`). The `41` thing here, is generally called the *key*. – Gabriel Aug 28 '18 at 07:37
  • 2
    So two problems perhaps: your value actually *isn't* `"extmax"` but `"extmax "` (in fact, `string(8) "extmax "` says there's one other hidden character in there for a total of `8` characters), and potentially it'll match the numbers first due to type juggling (see duplicate). – deceze Aug 28 '18 at 07:45
  • No, you're don't search for `'string(8) "extmax "'`. Your string isn't `'string(8) "extmax "'`. That is merely the debug output of `var_dump` telling you that the value is a `string` with `(8)` characters and the value `extmax `. – deceze Aug 28 '18 at 07:58
  • Thank you for explaing this @decese. When I use `extmax` or `extmax ` I still see no result ? – Muiter Aug 28 '18 at 08:07
  • 1
    Again, `var_dump` tells you that the string is 8 characters long, which obviously doesn't work for either "extmax" nor "extmax ". There's some other hidden character in there. Debug your string. Perhaps look at its raw bytes: `echo bin2hex($lines[41])`. – deceze Aug 28 '18 at 08:25

1 Answers1

3
$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!!!

pr1nc3
  • 8,108
  • 3
  • 23
  • 36
  • 1
    The much easier solution is to use `$strict` search mode. – deceze Aug 28 '18 at 07:44
  • You are right. I will also add it as a third improved solution. Thanks for mentioning – pr1nc3 Aug 28 '18 at 07:46
  • 1
    PHP doesn't have named arguments. `$strict=true` is nonsense and/or doesn't do what you think it does. Just do `array_search('extmax', $array, true)`. – deceze Aug 28 '18 at 07:57
  • Didn't know that i was always using strict parameter like this. Edited my answer and will look further into this. Until now everytime i used it like $strict=true it worked fine for me but if you mention that it's wrong i will investigate on that. Thanks for pointing out. – pr1nc3 Aug 28 '18 at 07:58