1

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?

3
  • 3
    These are stdClass not associative arrays.. Commented Oct 7, 2016 at 7:52
  • stdClass objects are objects Commented Oct 7, 2016 at 7:53
  • stdClass object can be used in a foreach loop.. Commented Oct 7, 2016 at 7:56

4 Answers 4

2

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/>";
}
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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.

Comments

1

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

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.