-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();
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
– Junior Like
Instead of giving the
echo $midia
, exchange forvar_dump($midia)
and send here the result.– Pedro
I just posted as an answer, and a way I managed to get the figures, but it’s still not as I need.
– Junior Like
You need index names (type
prefixo1
)?– Pedro
I don’t need, I just need the figures... In case, it’s 10 records.
– Junior Like
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.– Pedro
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.– Junior Like
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.
– Pedro
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.– Junior Like
You still don’t understand how the foreach works.
$value
of the example will always be the element valuegetCaption
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.– Pedro
If you want to access the value of
getCaption
ofprefixo1
, for example, without using a foreach, just use$_SESSION['prefixo1']['getCaption']
.– Pedro
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
– Junior Like
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...
– Junior Like
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.– Pedro
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().
– Junior Like
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!– Junior Like