How to search for value in Array/object in PHP?

Asked

Viewed 830 times

0

Personal I have the following array:

stdClass Object (
   [Chats] => Array (
       [0] => stdClass Object (
           [Chat] => stdClass Object (
               [ChatId] => 13383
               [UserId] => a2784a6dcf
               [SystemId] => a2784a6dcf~a2784a6dcf_OVL
               [Fullname] => chris
               [Email] => [email protected]
           )
       )
       [1] => stdClass Object (
           [Chat] => stdClass Object (
               [ChatId] => 13383
               [UserId] => a2784a6dcf
               [SystemId] => a2784a6dcf~a2784a6dcf_OVL
               [Fullname] => chris
               [Email] => [email protected]
           )
       )
   )
)

I’m in doubt on how I would travel these arrays taking the value user_id?

How do I access the value of UserId?

  • 2

    Have you ever tried to do anything?

  • I’ve tried it like this: $array->chats[0]->chat->UserId but I don’t know if it’s right!

  • 3

    That’s the idea, but the keys are Chats and Chat, with the letter c capital letter

4 answers

3


Whereas you have the following structure:

$data = (object) [
  "Chats" => [
    (object) ["Chat" => (object) ["ChatId" => 1000]],
    (object) ["Chat" => (object) ["ChatId" => 2000]],
    (object) ["Chat" => (object) ["ChatId" => 3000]],
    (object) ["Chat" => (object) ["ChatId" => 4000]],
    (object) ["Chat" => (object) ["ChatId" => 5000]],
  ]
];

It is worth noting that when doing the cast of a array associative to object PHP will create an instance of stdClass, so we can say that the above structure is equivalent to the question only disregarding the unused fields (UserId, SystemId, etc.).

You can get the list of ids in various ways.

1) Through in loop loop

$ChatIds = [];

foreach ($data->Chats as $Chat) {
    $ChatIds[] = $Chat->Chat->ChatId;
}

Getting the result [1000, 2000, 3000, 4000, 5000].

See working on Ideone

2) Through the function array_column

$Chats = array_column($data->Chats, "Chat");
$ChatIds = array_column($Chats, "ChatId");

Getting the result [1000, 2000, 3000, 4000, 5000].

See working on Ideone

3) Through the function array_map

$ChatIds = array_map(function ($chat) {
  return $chat->Chat->ChatId;
}, $data->Chats);

Getting the result [1000, 2000, 3000, 4000, 5000].

See working on Ideone

  • Perfect! I used the first way you said it, loop loop. !

0

You will have to go putting the keys as indexes, run the example below:

<?php
    $array[0]['teste'][12]['abc'] = "ola";
    print_r($array);
?>

Note that an array has been created with the keys 0, test, 12, abc which are the indices that lead up to the information "hello".

In your example probably if you were assigned to an array, the code can look like this:

$array["Chats"][0]["Chat"]["UserId"]
  • 1

    If you check the question again you will see that most of the values are actually object instances of stdClass, then the access is by the object notation, not array.

  • It’s true Anderson, I didn’t pay attention to it and now I’ve seen your answer up there as well and it really has coherence, it’s the lack of "c" characters in capital letters. But the good thing is that if you need to manipulate an array you will also have an example.

0

There goes what I did:

To access the value of Userid, I realized that I will have to use the notation to access objects (->) wherever stdClass Object and normal array indexes where you have array.

echo $array->Chats[0]->Chat->UserId;

To scroll through this array would look like this:

foreach ($array->Chats as $key => $array2){
    $aux .= $array2->Chat->UserId;
}

0

By your example, it would be enough to do this:

$obj = new stdClass('Chats');

$chat_obj_0 = new stdClass();
$chat_obj_0->ChatId = 133831;
$chat_obj_0->UserId = "a2784a6dcf1";
$chat_obj_0->SystemId = "a2784a6dcf~a2784a6dcf_OVL1";
$chat_obj_0->Fullname = "chris 1";
$chat_obj_0->Email = "[email protected]";

$chat_obj_1 = new stdClass();
$chat_obj_1->ChatId = 133832;
$chat_obj_1->UserId = "a2784a6dcf2";
$chat_obj_1->SystemId = "a2784a6dcf~a2784a6dcf_OVL2";
$chat_obj_1->Fullname = "chris 2";
$chat_obj_1->Email = "[email protected]";



   $n1 = (object) ['Chat' => $chat_obj_0];

   $n2 = (object) ['Chat' => $chat_obj_1];



$obj->Chats = array(
    0 =>  $n1,
    1 =>  $n2
  );


foreach($obj->Chats as $item) {

       echo 'Chat ID: ' . $item->Chat->ChatId . "<br>";
       echo 'User ID: ' . $item->Chat->UserId;
}

print_r($obj);

Ideone

  • Your example is different from the question, if the object is like the question the correct one would be echo 'Chat ID: ' . $item->Chat->ChatId . "<br>";&#xA; echo 'User ID: ' . $item->Chat->UserId;

  • @Guilhermenascimento, I had not seen, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.