Take Data from Session with Foreach

Asked

Viewed 58 times

-2

seems simple, but I’m all night researching and testing various methods and I get nothing. So, I decided to post.

I have the following item on session:

["prefixo1"]=>
  array(6) {
    ["getCreatedTime"]=>
    int(1618373454)
    ["getCaption"]=>
    string(17) "teste app legenda"
    ["getCommentsCount"]=>
    int(3)
    ["getLikesCount"]=>
    int(4)
    ["getLink"]=>
    string(39) "https://..."
    ["getImageHighResolutionUrl"]=>
    string(294) "https://..."
  }

My code for testing:

*linha 59:* foreach ($_SESSION as $array) {
*linha 60:* foreach($array as $key => $midia) {
*linha 61:* print "$key : $midia<br>";
*linha 62:* }
*linha 63:* }

Result, in theory, seems satisfactory...

getCreatedTime : 1618373661
getCaption : teste app legenda
getCommentsCount : 3
getLikesCount : 4
getLink : https://...
getImageHighResolutionUrl : https://...

I have the mistakes:

Warning: foreach() argument must be of type array|object, string given > **on line 60**
Warning: foreach() argument must be of type array|object, int given > **on line 60**

To display the data, for example, I have no return

$midia['getCreatedTime'];

I still get the bug:

Warning: Undefined array key "getCreatedTime"

Where am I going wrong?

Edited: I’m saving this way on session

$_SESSION['prefixo'.$key]['getCreatedTime'] = $media->getCreatedTime();
$_SESSION['prefixo'.$key]['getCaption'] = $media->getCaption();
$_SESSION['prefixo'.$key]['getCommentsCount'] = $media->getCommentsCount();
$_SESSION['prefixo'.$key]['getLikesCount'] = $media->getLikesCount();
$_SESSION['prefixo'.$key]['getLink'] = $media->getLink();
$_SESSION['prefixo'.$key]['getImageHighResolutionUrl'] = $media->getImageHighResolutionUrl();

2 answers

0


Hello, Junior! Welcome to Stack Overflow!

When using $midia['getCreatedTime'] you are trying to call the index getCreatedTime of $midia, which is not an array, but rather a string/int within the second foreach.

If you want to use the previous way, delete the second foreach. Thus, $midia will become an array and you will be able to access its indexes, as in the example below:

foreach ($_SESSION as $midia) {
    echo $midia['getCaption'];
}
// teste app legenda
  • Thank you for the welcome! In this way I tried, and got the following error: Fatal error: Uncaught Typeerror: Cannot access offset of type string on string

  • Instead of giving the echo $midia, exchange for var_dump($midia) and send here the result.

  • I just posted as an answer, and a way I managed to get the figures, but it’s still not as I need.

  • You need index names (type prefixo1)?

  • I don’t need, I just need the figures... In case, it’s 10 records.

  • Are you sure you are passing the right values to $_SESSION? Try to see if it is not empty. Here I did the test and my example worked.

  • Yes, the records exist. With var_dump($_SESSION); I can check and they are there. But they appear in the form I posted in the original question.

  • Look, this might solve the problem: https://phpsandbox.io/n/black-mouse-em7m-idprk. If you do, please let me know that I’ll update my reply.

  • Yes, this way solved the problem. The question now that I was not right was to take the data... $value['getCaption']; for example does not return anything to me.

  • You still don’t understand how the foreach works. $value of the example will always be the element value getCaption or any other element. It is not an array. If you want to do it by calling values that way, I keep my answer the same: use only one foreach. I updated the example for you to take a look.

  • If you want to access the value of getCaption of prefixo1, for example, without using a foreach, just use $_SESSION['prefixo1']['getCaption'].

  • In the first example, it worked. In this second example, here it does not work. I have the following error: Fatal error: Uncaught Typeerror: Cannot access offset of type string on string in

  • I can access other data from Session, on other pages, as it is in your example, but only this prefix data that I can not. Here’s how my Session:https://ideone.com/Hs7QZ8 I think I might be mixing array with objects? Is that there? for errors...

  • By var_dump, they are not objects, but arrays. Without another part of the code it is difficult to help you. Since you are working with $_SESSION, I suggest checking if you started it on the page you use. If you do not use session_start(), $_SESSION will be empty.

  • What part would you need? Before hand, thank you for your help. I really have been locked into this all day. In the original question, I posted how I store the data in Session. And I am using session_start instead().

  • I was able to solve the problem. It was in the form that I was saving the data on Session, for example. It was saving like this: $_SESSION['prefixo'.$key] and now I’m saving like this [ $_SESSION['prefixos'][$key] and now I can access as your examples. Thank you!

Show 11 more comments

0

New updates.

So I can go through and take the values:

foreach ($_SESSION['prefixo1'] as $key => $value) {
echo "$key: $value<br>";
} 

But I must inform the name of session. In case I have 10 records of these. I have to do 10x this code?

The command var_dump($midia); results to me:

array(6) {
["getCreatedTime"]=>
int(1618373661)
["getCaption"]=>
string(17) "teste app legenda"
["getCommentsCount"]=>
int(3)
["getLikesCount"]=>
int(4)
["getLink"]=>
string(39) "https://..."
["getImageHighResolutionUrl"]=>
string(294) "https://..."
}

Browser other questions tagged

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