I cannot access the value of the array I receive from an API

Asked

Viewed 94 times

0

Good afternoon, I’m walking little by little in php, but this part of array I still get very confused, I researched a lot on the internet and I read in the php manual but I can’t do what I want, I’m doing some tests with wordpress, in it I am using an api that returns me an array with multiple results, wanted to store a specific part of that array, follows the array when I give print_t

Array ( 
[0] => stdClass Object ( 
[id] => 10978 
[code] => al060819#3 
[amount] => 90.00 
[date_created] => 2019-08-05T13:35:28 
[date_created_gmt] => 2019-08-05T18:35:28 
[date_modified] => 2019-08-05T18:52:58 
[date_modified_gmt] => 2019-08-05T23:52:58 
[discount_type] => percent 
[description] => 
[date_expires] => 2019-08-07T00:00:00 
[date_expires_gmt] => 2019-08-07T05:00:00 
[usage_count] => 15 
[individual_use] => 
[product_ids] => Array ( ) 
[excluded_product_ids] => Array ( ) 
[usage_limit] => 
[usage_limit_per_user] => 
[limit_usage_to_x_items] => 
[free_shipping] => 
[product_categories] => Array ( ) 
[excluded_product_categories] => Array ( ) 
[exclude_sale_items] => 
[minimum_amount] => 0.00 
[maximum_amount] => 0.00 
[email_restrictions] => Array ( ) 
[used_by] => Array ( 
[0] => 88 
[1] => 90 
[2] => 92 
[3] => 89 
[4] => 99 
[5] => 101 
[6] => 100 
[7] => 102 
[8] => 106 
[9] => 108 
[10] => 116 
[11] => 117 
[12] => 116 
[13] => 123 
[14] => 122 ) 
[meta_data] => Array ( 
[0] => stdClass Object ( 
[id] => 25606 
[key] => count_items 
[value] => 0 ) 
[1] => stdClass Object ( 
[id] => 25619 
[key] => slide_template 
[value] => default ) ) 
[_links] => stdClass Object ( 
[self] => Array ( 
[0] => stdClass Object ( 
[href] => Null) ) 
[collection] => Array ( 
[0] => stdClass Object ( 
[href] => Null ) ) ) ) ) 

wanted to access used_by and save in another variable, tried in some ways, but all returned error ex:

$teste = $array[0]['used_by'];

or

$teste = $array['used_by'];

msg of error :

Fatal error: Uncaught Error: Cannot use object of type stdClass as array in C:\xampp\htdocs\teste.php:37 Stack trace: #0 {main} thrown in C:\xampp\htdocs\teste.php on line 37

Could someone please explain to me how it works? because I’m very lost in it, sorry for the stupid question, I think it can be repeated, but I researched a lot earlier and I couldn’t understand, I thank you from the beginning.

  • If the data is JSON, the link above will answer the question.

1 answer

3


The error message says that you cannot use an object of the type stdClass as a array.

If you look at your exit, you’ll see that your array is an object stdClass, no other array:

Array ( 
  [0] => stdClass Object ( 
    [id] => 10978 
    [code] => al060819#3 
    [amount] => 90.00 
    ...

Then it’s wrong to do $array[0]['used_by'].

To access an object’s value you need to use ->:

$array[0]->used_by
  • Hmm, I didn’t know about that, I’ll read the articles you sent, thank you very much

  • I know this isn’t the ideal location and you might not even see it, but is it possible to use this array value in a select? type $test = $array[0]->used_by SELECT * FROM test WHERE user = $test would it be possible to use this array in Where? or just making a real loop?

  • @Faillen I see all the comments I get... and yes, it should be possible. You can interpolate: "SELECT ... FROM teste WHERE coluna = {$array[0]->used_by}" or something like that

  • Thank you very much again, I will do the tests here. Thank you very much

Browser other questions tagged

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