How to access array returned by a function in php

0 votes

I'm using a template engine that inserts code in my site where I want it.

I wrote a function to test for something which is quite easy:

myfunction() { return '($this->data["a"]["b"] ? true : false)'; }

The problem is, $this->data is private, and I can't access it everywhere, so I have to use getData(); which causes my problem.

$this->getData()['a']['b']

does not work, and assigning the value first doesn't either because it will be used directly in an if() block.

Any ideas?

Nov 10, 2020 in PHP by kartik
• 37,500 points
359 views

1 answer to this question.

0 votes

Hello @kartik,

Since PHP 5.4 it's possible to do exactly that:

getSomeArray()[2]

On PHP 5.3 or earlier, you'll need to use a temporary variable.

Hope it helps!!

Thank You!!

answered Nov 10, 2020 by anonymous
• 82,740 points

Related Questions In PHP

0 votes
0 answers

How to sort an array of associative arrays by value of a given key in PHP?

Given this array: $inventory = array( ...READ MORE

Jul 24 in PHP by Kithuzzz
• 4,080 points
20 views
0 votes
0 answers

How to convert an array to a string in PHP?

What can I do to get the ...READ MORE

May 30 in PHP by Kichu
• 19,040 points
23 views
0 votes
0 answers

How to convert a string to an array in php

I want to convert a string into ...READ MORE

Jun 1 in PHP by Kichu
• 19,040 points
25 views
0 votes
0 answers

How to access function value in another function in PHP Classes

How can I return a variable value ...READ MORE

Jun 10 in PHP by narikkadan
• 11,280 points
21 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,740 points
14,630 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,740 points
1,652 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,740 points
1,368 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,740 points
19,828 views
0 votes
1 answer

How to access a global variable in a PHP function?

Hello @kartik, It is not working because you have to ...READ MORE

answered Nov 10, 2020 in PHP by Niroj
• 82,740 points
2,110 views
0 votes
1 answer

How to delete element by value in array (not key) using php?

Hello @kartik, Using array_search() and unset, try the following: if (($key = ...READ MORE

answered Sep 16, 2020 in PHP by Niroj
• 82,740 points
218 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP