Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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;
?>
share|improve this question
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? –  Paulpro Aug 1 '11 at 18:14
    
What are you trying to do? How should the $result array look like? –  Shef Aug 1 '11 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. –  Xalloumokkelos Aug 1 '11 at 18:23
    
Whats an example q? Maybe show us the url. –  iLLin Aug 1 '11 at 18:26
    
$q may be anna or ann or reas john –  Xalloumokkelos Aug 1 '11 at 18:52

3 Answers 3

up vote 1 down vote accepted

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;
    ?>
share|improve this answer
    
+1. Beat me to it! –  Shef Aug 1 '11 at 19:01
    
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. –  Xalloumokkelos Aug 2 '11 at 0:59
    
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. –  Matt Wilson Aug 2 '11 at 5:59

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.

share|improve this answer
    
The only strtolower I see is on the q variable. How do you know its failing? –  iLLin Aug 1 '11 at 18:38
    
I have removed the strtolower on my update because I think is not must. –  Xalloumokkelos Aug 1 '11 at 18:53
foreach ($data as $value) {
    if(strpos($value['label'], $q) !== false){
        $result[] = $value;
    }
}
share|improve this answer
    
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 ? –  Xalloumokkelos Aug 2 '11 at 1:01
    
@JPampos: What do you mean by "remote file for the inputs"? –  Shef Aug 2 '11 at 5:41
    
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 –  Xalloumokkelos Aug 2 '11 at 13:00
    
@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. –  Shef Aug 2 '11 at 15:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.