0

How can I edit this foreach loop so that I will be able to use strpos to look if q is found in the label ? The result array will contain those values.

$q may be anna or ann or reas john

<?php

$q = $_GET["q"];
if (!$q) return;

$data = Array(
    Array(
        'label' => 'anna c13',
        'category' => 'Products'
    ),
    Array(
        'label' => 'anders andersson',
        'category' => 'People'
    ),
    Array(
        'label' => 'andreas johnson',
        'category' => 'People'
    )
);

$result = array();
foreach ($data as $value) {
    array_push($result, array(
        "label" => $value["label"],
        "category" => $value["category"]
    ));
}


$json = json_encode($result);

echo $json;
?>
5
  • 1
    Your loop doesn't work because $key contains a numeric index and $value contains an array. But, I'm not sure what you're trying to do? Convert a list of labels and categories into a JSON of names and emails? Where are the email address supposed to come from? Commented Aug 1, 2011 at 18:14
  • What are you trying to do? How should the $result array look like? Commented Aug 1, 2011 at 18:18
  • Excuse me, I have updated my question for better understanding. The result array must contain those values that q is found in the label. Commented Aug 1, 2011 at 18:23
  • Whats an example q? Maybe show us the url. Commented Aug 1, 2011 at 18:26
  • $q may be anna or ann or reas john Commented Aug 1, 2011 at 18:52

3 Answers 3

1

This will output every array in $data where $q is somewhere in 'label'.

   <?php

    if( !isset( $_GET["q"] )) return;
    $q = $_GET["q"];

    $data = Array(
        Array(
            'label' => 'anna c13',
            'category' => 'Products'
        ),
        Array(
            'label' => 'anders andersson',
            'category' => 'People'
        ),
        Array(
            'label' => 'andreas johnson',
            'category' => 'People'
        )
    );

    $result = array();
    foreach ($data as $value) {
        if( strpos( $value['label'], $q ) !== false ) {
            $result[] = $value;
        }
    }


    $json = json_encode($result);

    echo $json;
    ?>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for it. Is this faster than a mysql query? Both ways will have a very big amount of data to search for. In the SQL query I use MATCH AGAINST.
I'm not sure about MATCH AGAINST, but I know that if comparing this to a mysql query using LIKE, the mysql would be considerably faster.
0

You haven't defined keys for your $data array - so it automatically take the form of:

 array(
    0=>array(...),
    1=>array(...),
    2=>array(...)
  )

This means that you're using strtolower on an int - so that's probably why it's failing.

2 Comments

The only strtolower I see is on the q variable. How do you know its failing?
I have removed the strtolower on my update because I think is not must.
0
foreach ($data as $value) {
    if(strpos($value['label'], $q) !== false){
        $result[] = $value;
    }
}

4 Comments

Thank you for your answer. Sorry that I can not upvote you, this is because of my low score. When it is necessary to use a remote file for the inputs instead of having them in the jQuery function ?
@JPampos: What do you mean by "remote file for the inputs"?
I mean to have the json data in a remote file like my example above. In other case, the json are in the jquery function
@JPampos: In that case you would have to run a loop on the JavaScript. However, that's another question, and you should either figure it out, or at least try to, and ask another question here on SO with your attempt.

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.