PHP关联数组按键获取值

时间:2016-10-07 07:51:09

标签: php arrays associative-array

我有一个assiciative multidenentional数组, 我要做的是通过密钥名称接收值。 例如,当我写$call['NAME']时,我会得到键值

这可能是数组:

Array
(
    [0] => stdClass Object
        (
            [ID] => 15678996
            [START] => 2016-10-07 10:33:08
            [END] => 2016-10-07 10:39:16
            [NAME] => פרי גנך
            [CALLERID] => 0504446349
            [CALLEE] => 089975757
            [O_EXTEN] => 1
            [EXTEN] => 1
            [SUPERID] => 49256
            [CUSTID] => 320748
            [PRICE] => 0.0
            [ANS_TIME] => 2016-10-07 10:33:09
            [DISPOSITION] => ANSWER
            [DURATION] => 367
            [INCOME] => 0.00
            [PR_NUMBER] => 723924039
            [CONCATENATION] => 320748,ANSWER;
            [OUTERID] => 
        )

    [1] => stdClass Object
        (
            [ID] => 15677268
            [START] => 2016-10-07 08:53:47
            [END] => 2016-10-07 08:54:21
            [NAME] => פרי גנך
            [CALLERID] => 0544300515
            [CALLEE] => 089975757
            [O_EXTEN] => 1
            [EXTEN] => 1
            [SUPERID] => 49256
            [CUSTID] => 320748
            [PRICE] => 0.0
            [ANS_TIME] => 2016-10-07 08:53:48
            [DISPOSITION] => ANSWER
            [DURATION] => 33
            [INCOME] => 0.00
            [PR_NUMBER] => 723924039
            [CONCATENATION] => 320748,ANSWER;
            [OUTERID] => 
        )
)

我想要实现的是通过写下他的密钥来回显每个值。

主阵列称为$all_calls 这是我到目前为止所做的:

foreach($all_calls as $call ){
  foreach ($call as $key => $value){
    echo '<b>Key</b> '.$key .': <b>Value</b> '. $value;
  }
}

我在这里缺少什么?

4 个答案:

答案 0 :(得分:2)

试试这个     $ keys = array(&#34; ID&#34;,&#34; START&#34;,&#34; END&#34;,&#34; NAME&#34;,&#34; CALLERID&#34;) ;

$myarray = array
(
array
    (
        "ID" => "15678996",
        "START" => "2016-10-07 10:33:08",
        "END" => "2016-10-07 10:39:16",
        "NAME" => "Myname",
        "CALLERID" => "0504446349"

    )
,
array
    (
         "ID" => "15678997",
        "START" => "2016-10-07 10:33:08",
        "END" => "2016-10-07 10:39:16",
        "NAME" => "Myname2",
        "CALLERID" => "0504446348"
    )
) ;


//print_r($myarray);

foreach ( $myarray as $array ){
foreach($keys as $key ){
    echo $array[$key];
    echo "<br/>";
}
}

答案 1 :(得分:1)

试试这个

function toArray($obj)
{
    if (is_object($obj)) $obj = (array)$obj;
    if (is_array($obj)) {
        $new = array();
        foreach ($obj as $key => $val) {
            $new[$key] = toArray($val);
        }
    } else {
        $new = $obj;
    }

    return $new;
}
$all_calls = toArray($all_calls);
foreach($all_calls as $call ){
   foreach ($call as $key => $value){
      echo '<b>Key</b> '.$key .': <b>Value</b> '. $value;
   }
}

当您在数组中获取对象时,您需要将其完全转换为数组然后循环。

答案 2 :(得分:1)

只需用这个

替换foreach循环
foreach($all_calls as $call ){
$call=get_object_vars($call);
foreach ($call as $key => $value){
echo '<b>Key</b> '.$key .': <b>Value</b> '. $value;
}
}

答案 3 :(得分:1)

你也可以通过使用这个给定的代码获得价值: -

$count=count($all_calls);
for($i=0; $i<$count; $i++){
$cell=$all_calls[$i];
echo $cell->NAME;
echo $cell->CALLERID;
}