PHP Accessing array values using Object Operator with key [duplicate] - php

This question already has answers here:
How to convert an array to object in PHP?
(35 answers)
Closed 2 years ago.
Say I have an array like the following:
$arr = array(
'id' => 123,
'title' => 'Example Title',
);
How come I cannot access the values using PHP's object operator (->)? In theory, I should be able to do $arr->title but that doesn't work and I have to access it as $arr['title'] instead.
I've been reading plenty of examples of people using the object operator however it's not returning anything but a null value.
Has something changed in recent versions of PHP or am I misunderstanding the examples given?

this code work 100% fine
$arr = (object) array(
'id' => 123,
'title' => 'Example Title',
);
echo $arr->title;

Related

How to convert PHP multidimensional associative array to API http query in the format the API specifies? [duplicate]

This question already has answers here:
How to save PHP array in database by converting array into JSON?
(3 answers)
Format array output
(1 answer)
Convert a series of PHP variables to a JSON object [duplicate]
(3 answers)
Converting MySQL result array to JSON [duplicate]
(2 answers)
Convert flat array to a delimited string to be saved in the database
(13 answers)
Closed 9 months ago.
I have the following array
$folder_data = array(
"title" => "Testing API Creation",
"description" => "Testing the Wrike API by creating this folder.",
"project" => array(
"status" => "OnHold",
"startDate" => "2022-05-19",
"endDate" => "2022-06-19",
"contractType" => "Billable",
"budget" => 100
)
);
When I run it through http_build_query(), it gives me this (urldecode() used for clarity):
title=Testing API Creation&description=Testing the Wrike API by creating this folder.&project[status]=OnHold&project[startDate]=2022-05-19&project[endDate]=2022-06-19&project[contractType]=Billable&project[budget]=100
The API throws an error saying that project[status] is an invalid parameter
The API docs give this within the curl example. You can see that the data for "project" is grouped:
title=Test folder&description=Test description&project={"ownerIds":["KUFK5PMF"],"startDate":"2021-10-19","endDate":"2021-10-26","contractType":"Billable","budget":100}
They're nesting the query into objects, I guess? How would I go about doing that with my PHP array?
I tried a recursive function someone had done, but it didn't give me what it's looking for either.
Any help is appreciated! Thanks!
The project parameter is JSON in their example, so use json_encode() to create that.
$folder_data = array(
"title" => "Testing API Creation",
"description" => "Testing the Wrike API by creating this folder.",
"project" => json_encode(array(
"status" => "OnHold",
"startDate" => "2022-05-19",
"endDate" => "2022-06-19",
"contractType" => "Billable",
"budget" => 100
))
);

Is there a way how to "encode" PHP array into PHP code? [duplicate]

This question already has answers here:
print an array as code
(4 answers)
Closed 2 years ago.
for very special purpose I need to transform PHP array into PHP code. I will try to explain what I mean in code below:
$a = array('a' => 'abc', 'path' => INCLUDE_DIR.'/file.txt', 'number' => 1234);
$php_code = some_magic_function($a);
echo $php_code; // outputs: "array('a' => 'abc', 'path' => '/path/to/file.txt', 'number' => 1234)"
So it would write back array as PHP code. I can not find any library that would help me with this.
Any ideas or hints ? Thanks in advance.
I think you want to print the array as a string, in the exact format it would be written in your code.
If so, then the var_export() function is what you need, e.g. something like this:
$a = array('a' => 'abc', 'path' => '/file.txt', 'number' => 1234);
$str = var_export($a, true);
echo $str;
Demo: http://sandbox.onlinephpfunctions.com/code/0b1da8fc0199a34539a55313680e982f9fddd14f

Why is this PHP array not the same? [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 4 months ago.
I'm not understanding why the array:
<? $data = array( 'items[0][type]' => 'screenprint'); ?>
Is not the same as
<? echo $data['items'][0]['type']; ?>
I'm trying to add to the array but can't seem to figure out how?
array( 'items[0][type]' => 'screenprint')
This is an array which has one key which is named "items[0][type]" which has one value. That's not the same as an array which has a key items which has a key 0 which has a key type. PHP doesn't care that the key kinda looks like PHP syntax, it's just one string. What you want is:
$data = array('items' => array(0 => array('type' => 'screenprint')));
I hope it's obvious that that's a very different data structure.
It should be:
$data = [
'items' => [['type' => 'screenprint']]
];
echo $data['items'][0]['type'];

Smarty doesn't display multidimensional arrays [duplicate]

This question already has answers here:
Smarty: How to reference to the associative array index
(4 answers)
Closed 8 years ago.
I have problem with displaying my array in smarty. It looks like this. Declaration of array:
index.php:
$rewrites = array(
'en' => array(
'homepage' => 'homepage'
),
'de' => array(
'homepage' => 'zuhause'
),
);
$smarty->assign('rewrites', $rewrites);
And in template file:
{$rewrites|#print_r}
{$rewrites[de][homepage]}
First line prints whole array like it is, so array is assigned. But second line shows nothing, why? How to do it properly? If I do it like this {$rewrites.de.homepage} it works but I really need to declare my array value like this {$rewrites[de][homepage]} because 'de' comes from other variable, that define current language. My target is {$rewrites[$lang][homepage]} for example.
Use:
{$rewrites[$lang]['homepage']}
You can use also:
{$rewrites.{$lang}.homepage}
Simply try this:
{$rewrites.de.homepage}
you can do is as follows {$rewrites[$lang].homepage}

How to get key value of array? [duplicate]

This question already has answers here:
Getting the key of the only element in a PHP array
(6 answers)
Closed 8 years ago.
How do I get the value of a key of any array item? Like how a foreach loop turns it into $k => $v...except I only want to do that once, so no need for a loop. Do I really need to make a new array that it flips to?
Take this for example.
1 => array(
'street' => 'Street Address ',
'town' => 'Town/City '
),
2 => array(
'state' => 'State '
),
Those are arrays inside a bigger array. And now I tried to do this
array_flip($thatarrayupthere[2]['state'])
What I want to receive from that is "state" because that is the key name. But I'm getting errors.
I'm not exactly sure what you wan't, but if you just want to get the key of the second array in any given array this might help.
$key = key($array[2]);
In your example above you will get "state" in your $key variable.
$key = array_keys($array[2]);
print_r($key);
ref: http://php.net/manual/en/function.array-keys.php

Categories