Can't echo array items [duplicate] - php

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 6 years ago.
I have an array which I can't seem to access properly, been trying to echo the address. It's called $user and here is the print_r for it:
Array(
[0] => stdClass Object
(
[id] => 1
[firstname] => Casper
[lastname] => *lastname*
[email] => someEmail
[username] => aUsername
[password] => *encrypted password*
[address] => myAddress
[address2] =>
[city] =>
[state] =>
[zipcode] => 1111
[join_date] => current timestamp
)
)
1
It's in codeigniter and so far I've tried:
echo $user['address'];
echo $user->address;
Both of them with no luck. Any help is greatly appriciated

You have a multi-dimensional array and the first index is 0, also inside the first index you have the object. to access an array of object you need to use ->
You have to echo like given code:
echo $user[0]->address;

Related

php array get single value [duplicate]

This question already has answers here:
How to get single value from this multi-dimensional PHP array [duplicate]
(6 answers)
Closed 5 years ago.
I have an array:
print_r($response);
will show:
Response: Array
(
[errors] => false
[return] => Array
(
[price_recurring] => 0
[country_code] => NL
[invoicemethod] => instant
[product_id] => 0
[affiliate] =>
[number_of_periods] => 1
[description] => Betaling voor eventid 274
[period_duration] => 1 month
[date] => 29/11/2017 19:42
[order_quantity] => 1
[vat] => 21
[amount_total] => 4599
[total_paused_days] => 0
[custom] => WEB1511980957x194237
[emailaddress] => ik#ik.nl
[amount_affiliate_initial] => 0
[total] => 45,99
[current_status] => completed
[amount_affiliate_recurring] => 0
[amount_total_affiliate] => 0
[id] => 1790226
[price_initial] => 4599
[current_number_of_periods] => 1
[firstname] =>
)
)
How can i extract the value 'custom'?
I've tried:
$response = array_shift($response);
echo $response['custom'];
but it will show nothing. What am i doing wrong here? I know i am close.
$response is an array with two keys errors and return. $response['return'] in turn, is an array with several keys, including custom. Therefore, to access custom, you need to reference $response['return']['custom']
you have an array inside an array
$response["return"]["custom"]
also try to var_dump (or print_r depends on what you prefer) instead of echoing when you try to navigate
var_dump( $response["custom"]);
would tell you a lot more than echoing null, which prints nothing

How can I access array's item when there is an object in it? [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 5 years ago.
I have an array like this:
echo "<pre>";
print_r($res_users);
/*
Array
(
[0] => stdClass Object
(
[position] => 1
[user] => stdClass Object
(
[full_name] => دکوراسیون
[is_private] => 1
[has_anonymous_profile_picture] =>
[byline] => 39.3k followers
[profile_pic_url] => https://scontent-frt3-1.cdninstagram.com/t51.2885-19/s150x150/11352072_817936521647202_201395223_a.jpg
[pk] => 1480272396
[follower_count] => 39326
[is_verified] =>
[mutual_followers_count] => 0
[username] => sajad.sobhi
)
)
)
All I'm trying to do is selecting following URL from array above:
https://scontent-frt3-1.cdninstagram.com/t51.2885-19/s150x150/11352072_817936521647202_201395223_a.jpg
Which is the value of profile_pic_url. How can I get that?
Noted that this doesn't work:
echo $res_users[0]['user']['profile_pic_url'];
Try $res_users[0]->user->profile_pic_url. Because $res_users[0] is object type
Try it by this way
echo $res_users[0]->user->profile_pic_url;

Access to a specific array in php [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 6 years ago.
i create an array and i would like to access to the value but i really don't know how :
Here is my array :
Array
(
[0] => Array
(
[id] => 1
[id_orga] => 36094152
[nom] => Adresse externe
[url] => https://cubber.zendesk.com/api/v2/organizations/36094152.json
[created] => 2015-01-22 08:00:53
[updated] => 2015-01-22 08:00:53
[tickets] => Array
(
)
[monthly] => Array
(
[0] => ArrayObject Object
(
[storage:ArrayObject:private] => Array
(
[assist] => 0
[maint] => 0
[total] => 0
)
)
and i would like for example to access to the value "0" in the key "assist" and change it to "1" for example and i don't know how to do it.
Suppose the source array is called $myArray. Try:
$myArray[0]['monthly'][0]->storage['assist'] = 1
If that doesn't work, try:
$myArray[0]['monthly'][0]['storage']['assist'] = 1
Let me know which works so I delete the other.

array does not echo correc value [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 6 years ago.
I just initialize an array $exp_arr with some values. The print_r($exp_arr[0]) results the following values
Array
(
[0] => Array
(
[id] => 40
[email] => shishir012010#gmail.com
[company_name] => Yahoo
[title] => Developer2
[location] => Noida
[description] =>
[smonth] => January
[syear] => 2012
[emonth] => December
[eyear] => 2015
[status] => 0
)
)
After that echo $exp_arr[0]['location'] does not print anything.
I also tried var_dump($exp_arr[0]['location']) and it results in output of NULL.
I can't track the problem. I have put the echo statement just after the print_r statement just like that
echo "<pre>";
print_r($exp_arr[0]);
echo $exp_arr[0]['location'];
var_dump($exp_arr[0]['location']);
To explain Karma Pals point, your array is already one deep in a 0 index, so if you do this:
print_r($exp_arr[0][0]['location'])
You will get your location out. You need to check if your array structure is correct based on that.
Your vardump of just $exp_arr will look like something this:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 40
[email] => shishir012010#gmail.com
[company_name] => Yahoo
[title] => Developer2
[location] => Noida
[description] =>
[smonth] => January
[syear] => 2012
[emonth] => December
[eyear] => 2015
[status] => 0
)
)
)

Generate json to php find ID value through php [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 6 years ago.
Find ID value through php
stdClass Object ( [Providers] => Array (
[0] => stdClass Object (
[Main_Fax] =>
[Signed_POA] => No
[Signed_HIPAA] => No
[Website] => www.google.com
[VAR_Provider_Type] => Template Test
[State_Temp] =>
[City] =>
[Notes] =>
[Main_Phone] =>
[ZIP] =>
[Provider_Type_Temp] =>
[State] => Alaska
[Signed_Purchae_Agreement] => No
[Reimbursement_Rate] => 0
[ID] => 977948000009029003
[Provider_Name] => Amit
[Address_Line_1] =>
[In_Network] => No
[Address_Line_2] =>
)
)
)
Seprate this json & show id value through php -> means this value [ID] => 977948000009029003
Let your array is $arr.
Now try this:
$arr->Providers[0]->ID;
Result:
977948000009029003
$data->Providers[0]->ID
See the structure of the object you gave us. You can see there's an object with a item called Providers. You can then see that the value of "Providers" is an array indexed by a integer, which inside it has an Object. The object contains the ID you wish to obtain.
Object > Array > Object

Categories