php array get single value [duplicate] - php

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

Related

Response with unwanted new lines appears while encoding array to JSON [duplicate]

This question already has answers here:
Pretty-Printing JSON with PHP
(27 answers)
Closed 6 months ago.
Having an array printed with print_r method:
Array
(
[0] => Array
(
[id] => 44
[item_level] => 0
[position] => 10
[parent_position] =>
[title] => PHP Tutorial
[qty] => 0
[comment] =>
[status] =>
[unit] =>
)
[1] => Array
(
[id] => 46
[item_level] => 1
[position] => 20
[parent_position] =>
[title] => Algorithms
[qty] => 1
[comment] =>
[status] =>
[unit] =>
)
[2] => Array
(
[id] => 48
[item_level] => 2
[position] => 30
[parent_position] => 20
[title] => PHP and MySQL Databases
[qty] => 1
[comment] =>
[status] =>
[unit] =>
)
)
I'm trying to encode it as JSON using either:
json_encode($array, JSON_NUMERIC_CHECK);
json_encode($array);
The output in both cases is the following though:
[{"id":"44","item_level":0,"position":10,"parent_position":"","product_id":"","title":"PHP
Tutorial","qty":0,"comment":"","status":"","unit":""},{"id":"46","item_level":1,"position":20,"parent_position":"","title":"Algorithms","qty":1,"comment":"","status":"","unit":""},{"id":"48","item_level":2,"position":30,"parent_position":20,"title":"PHP
and MySQL
Databases","qty":1,"comment":"","status":"","unit":""}]
Yes, there are the newline characters put between some, but not all, space-separated words. In Postman, the response arrives broken into multiple lines, the same happens when my frontend gets this kind of response, causing it to report JSON parsing error.
How to transform the array to JSON format properly?
PHP version here is 7.0.33.
Tried to look for any non-printable characters that might have been added on the way, as it was suggested in the comments, but wasn't able to locate any.
Anyway, pretty printing solved the issue in a satisfying way:
json_encode($array, JSON_PRETTY_PRINT);
The only thing to be considered, is casting numbers to proper types on arrival to frontend, since JSON_PRETTY_PRINT prints out all of them as strings (i.e. {"quantity": "1"}).

how find element in this array? [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 1 year ago.
Array
(
[result] => 0
[room] => Array
(
[name] => room for one to one video meeting: 306100
[service_id] => 5f6c5b420dd7940701cc36b1
[owner_ref] => 306100
[settings] => Array
(
[mode] => group
[scheduled] =>
[adhoc] => 1
[duration] => 30
[participants] => 1
[auto_recording] =>
[screen_share] => 1
[canvas] =>
[media_configuration] => default
[quality] => SD
[moderators] => 1
[active_talker] => 1
[max_active_talkers] => 1
[single_file_recording] =>
[media_zone] => XX
)
[sip] => Array
(
[enabled] =>
)
[created] => 2021-04-19T05:52:26.779Z
[room_id] => 607d1a9a4e11874ceda79a96
)
)
how to find room_id this array
The array_search() function searches an array for a given value and returns the key. The function returns the key for val if it is found in the array. It returns FALSE if it is not found. If val is found in the array arr more than once, then the first matching key is returned.
Syntax
array_search(val, arr, strict)
Parameters
val − The value to be searched
arr − The array to be searched
strict − Possible values are TRUE or FALSE. Search for identical elements in the array, set to TRUE.
example :-
<?php
$arr = array("p"=>20,"q"=>20,"r"=>30,"s"=>40);
echo array_search(20,$arr,true);
?>

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