DavSev
DavSev

Reputation: 1111

PHP associative array get value by key

I have an assiciative multidenentional array, what I am tring to do is to receive a value by key name. for example, when I will write $call['NAME'] I will get the key value

this is may array:

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] => 
        )
)

What i want to achieve is to echo each value by writing his key name.

the main array is called $all_calls this is what i havee done so far:

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

what am i missing here?

Upvotes: 1

Views: 16313

Answers (4)

Metallic Skeleton
Metallic Skeleton

Reputation: 689

Try this $keys = array("ID","START", "END", "NAME", "CALLERID" );

$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/>";
}
}

Upvotes: 2

Kunwar Kishor
Kunwar Kishor

Reputation: 71

you can get value by using this given code also:-

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

Upvotes: 1

Pradyut Manna
Pradyut Manna

Reputation: 588

Just replace foreach loop by this

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

Upvotes: 1

Rohit Ailani
Rohit Ailani

Reputation: 910

try this

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;
   }
}

as you are getting object inside the array so you need to convert it fully to an array and then loop through.

Upvotes: 1

Related Questions