1

I have a $search_array like this

Array
(
    [1] => Array
        (
            [type] => book
            [search] => steve
        )

    [2] => Array
        (
            [type] => book
            [search] => john
        )


foreach ($search_array as $s) {

    $arrayid = //???????
    $searchtype = $s['type'];
    $search = urlencode($s['search']);

getResult($arrayid);

}

I'm trying to figure out how to get the array number. So for the first result i need $arrayid to be 1. How do I reference that in the foreach loop?

Thanks

0

5 Answers 5

6

Use a foreach with "$key => $value"

Array
(
    [1] => Array
        (
            [type] => book
            [search] => steve
        )

    [2] => Array
        (
            [type] => book
            [search] => john
        )

foreach ($search_array as $key => $s) {

    $arrayid = $key
    $searchtype = $s['type'];
    $search = urlencode($s['search']);

getResult($arrayid);

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

Comments

3

Adding $arrayid => in your foreach loop declaration will auto-assign $arrayid with the current array index.

foreach ($search_array as $arrayid => $s) {
  // ...
  getResult($arrayid);
}

See foreach on PHP Manual.

Comments

3
foreach ($search_array as $arrayid => $s) { 

Please read PHP documentation before asking such basic questions

1 Comment

@vpets a very simple search would have helped a lot.
0
foreach ($search_array as $arrayid => $s) {
    // your code here
}

Comments

0

This is a way you could retrieve data from a Php array. It will retrieve all entries from array $value and their key $key.

foreach ($array as $key => $value) {
  // $key is the array index
}

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.