0

So I'm trying to make a function that searches a string in a whole array. But it give me anything... So my array looks like this :

    array:1 [▼
      "list" => array:2 [▼
        "pagination" => array:5 [▶]
        "entries" => array:11 [▼
          0 => array:1 [▼
            "entry" => array:8 [▼
              "firstName" => "Doctor"
              "lastName" => "Who"
              "emailNotificationsEnabled" => true
              "telephone" => "0123456789"
              "company" => []
              "id" => "DW"
              "enabled" => true
              "email" => "[email protected]"
            ]
          ]
          1 => array:1 [▶]
          2 => array:1 [▶]
          3 => array:1 [▶]
          4 => array:1 [▶]
          5 => array:1 [▶]
          6 => array:1 [▶]
          7 => array:1 [▶]
          8 => array:1 [▶]
          9 => array:1 [▶]
          10 => array:1 [▶]
        ]
      ]
    ]

So for exemple at first I did $key = array_search("doctor", $users); but this gives me nothing. So I thought it was because I have a multidimensional array. So I reduced it to just one array (and I would search in the rest of the original array with a for loop), so now I'm working with this array that I got with $users['list']['entries'][0]

array:1 [▼
  "entry" => array:8 [▼
    "firstName" => "Doctor"
    "lastName" => "Who"
    "emailNotificationsEnabled" => true
    "telephone" => "0123456789"
    "company" => []
    "id" => "DW"
    "enabled" => true
    "email" => "[email protected]"
  ]
]

But $key = array_search("doctor", $users['list']['entries'][0]); still doesn't give me anything (but false).

Does anyone know where is my mistake ? Because I couldn't find a solution of my problem yet and it's been a pretty long time I'm on it... I'm still a beginner in php so maybe I've missed something obvious and i'm sorry if I did.

Thank you in advance !

7
  • 2
    First, your array is still multi-dimensional. You need $users['list']['entries'][0]['entry']. Second, array_search only finds exact, case-sensitive matches, and none of the values in the array are "doctor". Commented Jun 27, 2017 at 13:38
  • @iainn Oh ok thank you ! Even if you add false like that array_search("value", $array, false)? Commented Jun 27, 2017 at 13:40
  • Yep. false is the default behaviour, so you don't ever need to explicitly pass it. You generally want to provide true in most cases anyway, since otherwise you'll get false positives when comparing strings to boolean true. In this case, if you run array_search("doctor", $array, false), it will match the emailNotificationsEnabled field. Commented Jun 27, 2017 at 13:47
  • @iainn Ok with $key = array_search("Doctor", $users['list']['entries'][0]['entry']); it works and displays me firstName but when i put false you're right it always gives me emailNotificationsEnabled with whatever value i use. So how can I search something in my array without case sensitivity ? Commented Jun 27, 2017 at 13:52
  • 1
    See stackoverflow.com/questions/4168107/… Commented Jun 27, 2017 at 13:52

1 Answer 1

1

you are searching for doctor,but value stored in your array is as Doctor. So either search for Doctor as

$key=array_search('Doctor', $users['list']['entries'][0]));

or for case in-sensetive search use

$key=array_search(strtolower('Doctor'), array_map('strtolower', $users['list']['entries'][0]));

Note: as your array is multidimensional, so use loop to search your value

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

3 Comments

Disabling the strict option doesn't mean that the search is case-insensitive.
Ok first I've forgotten the ['entry'] to make my array unidimensional so I now use $users['list']['entries'][0]['entry'] which works with a case-sensitive search , thats one big problem solved I guess aha But for the case in-sensetive search I always get this error Warning: strtolower() expects parameter 1 to be string, array given whatever I do. Do you know where that comes from ?
Ok I know why it gives me this error : it is because of "company" which is an array.. I'll open another issue if I can't find how to solve this since it is another problem thank you !

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.