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.

I want to set array value by their index/value in php or javascript in cakephp.I fetch data from stored procedure in model.My demo code is

Array
(
    [0] => Array
        (
            [text] => --Select--
            [value] => 
        )

    [1] => Array
        (
            [value] => 269
            [text] => Being amount paid to supplier
        )

    [2] => Array
        (
            [value] => 268
            [text] => Cash Received 
        )

    [3] => Array
        (
            [value] => 267
            [text] => Cheque Received 
        )

)

I want to set dropdown value ,my second array value is

Array
(
    [0] => Array
        (
            [DefaultNarration_071] => 268
        )

)

so how i set my dropdown value 268 id.so please suggest me appropriate solution.

share|improve this question

closed as unclear what you're asking by Touki, giammin, John Willemse, Mark Bertenshaw, Rahil Wazir May 13 at 8:38

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

5  
What have you created on your own? Show us your code? And your data should be in PHP format –  Marcin Nabiałek May 13 at 6:43
    
Hello sir , I create one dropdown field in that dropdown i bind data from stored Procedure then i get result in first array which i show .after i want to set particular index value that i get into 2nd array . So how i set particular dropdown values giving index or value. –  Shamika May 13 at 6:50

1 Answer 1

up vote 0 down vote accepted
$array_list=array(array("value"=>0,"text"=>"--Select--"),array("value"=>268,"text"=>"Cash received"));

$selected=268;
$text="";
foreach($array_list as $entry){
    if($entry['value']==$selected){
        $text=$entry['text'];
        break;
    }
}

echo "Selected Option: ".$text;

Text will now contain the selected option, if I understood your question correct.

share|improve this answer
    
I want to set only "Cash Received" on page load. so how to set?? –  Shamika May 13 at 8:47
    
...I think that is what is now in the variable $text. Your explanation is a bit weak. –  D. Schalla May 13 at 8:49
    
How to set particular default value from array in php? –  Shamika May 13 at 8:52
    
You can replace the '$text="";' with your default value, it can be written as string or also if it is the first child of the array_list: $text=array_list[0]['text']; –  D. Schalla May 13 at 8:58

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