在数组中搜索关键字以获取值 - php

Search keyword in array to get the value - php

基本上我正在从我的 table 中读取 BLOB 数据。

$sql = "SELECT blob_data as datajson FROM tablename";
$res=$obj->_executeQuery($sql);
$res=$obj->getAll($res);

这将在我的 BLOB 中提供数据,我已将其转换为数组。

for($y=0;$y<count($res);$y++)                           
{
    $decode = json_decode($res[$y]['datajson']);
    if(!empty($decode))
    {
        foreach($decode as $name => $value)  
        {               
            print_r($value);
        }
    }
    else
    {
        return(0);
    }   
}

现在我的print_r($value);会给我

stdClass Object
(
    [name] => dinner
    [value] => Dinner~360
)
stdClass Object
(
    [name] => fullday
    [value] => Full Day~805
)
stdClass Object
(
    [name] => expenseamount
    [value] => Expense Amount~805
)

现在我需要通过 name 搜索 expenseamount 并且应该得到它的值。在这里我应该得到

Expense Amount~805.

我不能从它的位置做 foreach($decode[2] as $name => $value) ,因为位置每次都会改变。我无法按名称搜索。如何做到这一点?

试试这个,

foreach($decode as $name => $value)  
    {               
        if($value->name =="expenseamount") {
              echo $value->value;
        }
    }