-1

Need help here Please.

I have this type array from json_decode($items). The output array is like below :

Array
(
    [0] => Array
        (
            [recipient_type] => EMAIL
            [note] => For Sender
            [receiver] => abc@gmail.com
            [sender_item_id] => BB_000001
        )

    [1] => Array
        (
            [recipient_type] => EMAIL
            [note] => For Sender
            [receiver] => cde@gmail.com
            [sender_item_id] => BB_000002
        )

)

Then I used this code to get the data from the array :

foreach($items as $item) {
    $data = array(
                'RecipientType' => $item->RecipientType,  
                'Note'          => $item->Note,  
                'Receiver'      => $item->Receiver,
    );
}

But i got error on $item->RecipeientType said :

Trying to get property of non-object

Seem that I cannot get The data from the array.

How should I get the data in the right way?

Thank You

Danish Ali
  • 2,354
  • 3
  • 15
  • 26
Dennis Liu
  • 2,268
  • 3
  • 21
  • 43
  • 1
    `$item` is a non-object variable, you have to access the value using index e.g:`$item['recipient_type'], $item['note'], etc.` –  Jan 11 '19 at 00:21
  • 1
    You say you've used `json_decode($items)` but the output array in your question says you used `json_decode($items, true)`. Next time, please provide **all** the relevant code – Phil Jan 11 '19 at 00:25
  • @catcon thank you. this is exactly what i mean. But with json_decode set to true. – Dennis Liu Jan 11 '19 at 00:31
  • @Phil yes thank you, when i set json decode to true i can get the item with $item['recipient_type'] – Dennis Liu Jan 11 '19 at 00:31
  • You are not getting an array of objects. So do like this. `$data = array( 'RecipientType' => $item['RecipientType'], 'Note' => $item['Note'], 'Receiver' => $item['Receiver'], );` – Danish Ali Jan 11 '19 at 05:35
  • @DanishAli Thanks. – Dennis Liu Jan 11 '19 at 22:47

1 Answers1

1

The original array has the key recipient_type so it should be $item->recipient_type as well.

Besides, json_decode has a second boolean parameter that gives you an array right away. Try json_decode($items, true)

Danish Ali
  • 2,354
  • 3
  • 15
  • 26
sui
  • 751
  • 6
  • 10
  • almost get it right, but with json_decode set to true i cannot get the array item using $item->recipeitn_type, it must use $item['recipient_type']. But LOL i type a wrong key haha. thank you for showing the mistake. – Dennis Liu Jan 11 '19 at 00:33