0

I have the following $qtips_messages array,

Array
(
    [0] => Array
        (
            [id] => 1
            [tips_title] => email_tips 
            [tips_message] => ex:[email protected]
            [tips_key] => key_email
        )

    [1] => Array
        (
            [id] => 2
            [tips_title] => website_tips 
            [tips_message] => ex:http://www.yahoo.co.in
            [tips_key] => key_website
        )

    [2] => Array
        (
            [id] => 3
            [tips_title] => zipcode_tips
            [tips_message] => ex:60612
            [tips_key] => key_zipcode
        )

    [3] => Array
        (
            [id] => 4
            [tips_title] => phone_tips 
            [tips_message] => ex:1234567890
            [tips_key] => key_phone
        )
)

For example, I want to get the tips message for the tip_title 'email_tips' I have tried with following code,

foreach($qtips_messages as $qtipsArray){
    foreach($qtipsArray as $qkey=>$qvalue){
        if($qtipsArray['tips_title'] == 'email_tips'){
            $emailtipsMessage = $qtipsArray['tips_message'];
        }
    }
}

When i ran the above code i did not get any value.
What is wrong with this code?

3 Answers 3

2

You only need one loop:

$message = null;
foreach ($array as $tips) {
    if ($tips['tips_title'] == 'email_tips') {
        $message = $tips['tips_message'];
        break;
    }
}

I'd probably go for something like this though:

$message = current(array_filter($array, function ($tip) { return $tip['tips_title'] == 'email_tips'; }));
echo $message['tips_message'];
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks,$array will be associative array ? still i did not get any value.
$array is your array, $qtips_messages I guess.
$message = null; foreach ($qtips_messages as $tips) { if ($tips['tips_title'] == 'email_tips') { $message = $tips['tips_message']; break; } } echo $message; exit; sorry deceze, i did not get value
Works fine for me: codepad.org/hxtMno7T I can only show you to way, you have to do the debugging yourself.
1
$array  = array();
foreach($result AS $k =>$val)
   $array[$val['tips_key']]    = $val['tips_message'];
return $array;

Now the array $array have all the values for q-tips based on their keys...

Hope this will helps you...

Comments

0

try this way.

$array  = array();  
foreach($qtips_messages AS $k =>$val):  
if($val['tips_title']=='email_tips')  
{  
  $array[$val['tips_key']] = $val['tips_message'];  
  print_r($array);  
  break;  
}  
endforeach;  

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.