0

The Array format

Array
(
[76ea881ebe188f1a7e7451a9d7f17ada] => Array
    (
        [rowid] => 76ea881ebe188f1a7e7451a9d7f17ada
        [id] => 1
        [qty] => 2
        [price] => 20
        [name] => First
        [options] => Array
            (
                [permName] => beer
            )

        [subtotal] => 40
    )

[e7a36fadf2410205f0768da1b61156d9] => Array
    (
        [rowid] => e7a36fadf2410205f0768da1b61156d9
        [id] => 3
        [qty] => 6
        [price] => 20
        [name] => Second
        [options] => Array
            (
                [permName] => achieve
            )

        [subtotal] => 120
    )

Is there a way for me to search this array for the title or in this case lets say e7a36fadf2410205f0768da1b61156d9 and once it is found just return the price and the qty values?

Any help would be greatly appreciated.

3 Answers 3

1

You can you array_key_exists function to check if there is any element with a given key.

$title = 'e7a36fadf2410205f0768da1b61156d9';   
if (array_key_exists($title, $arr)) {
    return array('price'=>$arr[$title]['price'], 'qty'=>$arr[$title]['qty']);
}
Sign up to request clarification or add additional context in comments.

Comments

1
$title = 'e7a36fadf2410205f0768da1b61156d9';

if (isset($arr[$title])) {
    return array('price'=>$arr[$title]['price'], 'qty'=>$arr[$title]['qty']);
}

Comments

1

This function is enough.

function search_title($title, $array){
    if(array_key_exists($array[$title])){
        return array($array[$title]['price'],$array[$title]['qty']);
    }else{
        return array( false, false);
    }
}

Usage,

list ($price, $qty) = search_title('e7a36fadf2410205f0768da1b61156d9', $array);

if($price!==false){
    // search was successful
}

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.