Welcome to PhoneASK, where you can ask questions and receive answers from other members of the community.

Is it possible to access an index on a casted expression?

asked 7 days ago by nmichaels

I am trying to deal with some input that may or may not be an array, with the intent of obtaining the first item if the input is an array, and the item itself otherwise.

Here is how I was planning to go about it:

$payload = ((array)json_decode($mapOrArray))[0];

This did not sit well with the interpreter, however. Instead, I am currently using this:

$payload = array_values((array)json_decode($mapOrArray))[0];

Only once I've wrapped the casted array in a useless method call can I use this approach.

Is accessing an index of the result of a cast impossible with a single statement? If so, what is a better way to deal with this task, preferably without introducing an extraneous variable?

Your answer

1 Answers

answered 7 days ago by William
list($payload) = (array) json_decode($mapOrArray);

Related Questions

In php is possible to access elements of the current Array by index? Example: $arr = array( 'index1' => function() { echo 'foo'; //foo }, 'index2' => function() { $arr['index1']() // foo ???????
I am using Python 2.7 and I am fairly familiar with using regular expressions and how to use them in Python. I would like to use a regex to replace comma delimiters with a semicolon. The problem is th
How would I run a query (on PostgreSQL internal tables) to retrieve the conditional expression for the partial index (roles_user_group_role_idx below in this case). # \d roles Table public.roles Col
How can I access to an array element inside handlebars template using a variable instead of an hardcoded value? I need to do something like: {{#each condition in conditions}} {{App.ops.[condition.op].
I have a Microsoft Access file (.mdb) with 2.000.000 row on it and i'm using a query : select count(*) from tblABC where instr(Column1 , something) > 0 to get all record contain the word 'somet
I need to choose some elements from the given list, knowing their index. Let say I would like to create a new list, which contains element with index 1, 2, 5, from given list [-2, 1, 5, 3, 8, 5, 6]. W
I want to be able to dynamically build an index so I can iterate through the objects properties with buttons. Below is what I've tried with, which doesn't work. How could this be achieved? var bodyNr
Is there a way to get the list index name in my lapply() function? n = names(mylist) lapply(mylist, function(list.elem) { cat(What is the name of this list element?\n }) I asked before if it's poss
I need to access a specific item from IEnumerable @model ; I know that I can reach all item by below code: @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.Fo
I'm writing data from Java to an Access database on Windows 32 bit. When I write a record, I need to retreive the row ID / primary key so that I can a) update the record easily if I want to and b) cro
In knockout.js 2.1.0, in a template using the foreach binding, you can access the current item's index though the $index() function. In a nested foreach binding, is there any way to access the index o
I'm working with a specialised text file, which is essentially a long list of names and random sequence of letters associated to each name. I'm attempting to extract a particular consensus sequence I'
I understand there is a difference between a PDF and a text searchable PDF. Text searchable PDFs have a text overlay that is used for searching. Is it possible to extract this text overlay into a txt
Does anyone know if there is any way to restrict access to the remote on the client? I have 2 repositories, I shoud commit to one of those (let's call it A), not the other (B), but I technically have
I'm trying to create a wireframe vertex/fragment shader in Unity. It seems possible according to this paper. The general ideas seems to be that you pass a distance vector calculated in the vertex shad
Is it possible to access 32-bit registers in C ? If it is, how ? And if not, then is there any way to embed Assembly code in C ? I`m using the MinGW compiler, by the way. Thanks in advance!
I have a set of html files and an SQLite database, which I would like to access from the browser, using the file:// scheme. Is it possible to access the database and create queries (and tables) using
I'm trying to target a class element (appearing more than once in the DOM) by its index or iteration, strictly using CSS. I know of several different ways of achieving this: I could toss the element(s
I'm trying to design some UserControl classes in Blend 3. I want parts of them to be collapsed when created at runtime, but I want to be able to edit their component parts without fiddling with code
This question already has an answer here: subsetting a data.table using !=<some non-NA> excludes NA too 3 answers DT[!(x == .)] and DT[x != .] treat NA in x inconsistently 4 answers W
Consider the following code: (function() { var a = 5; var someFunc = function() { ... }; function anotherFunc() { ... }; })(); window.myGlobalObj = { init: function() { // and somehow here I want to a
I'm starting to play with Zend FrameWork, but I have had troubles with it. I try the follow the super guide From Zerto To Zend Framework in 10 minutes, but it amounted to nothing. I also was getting
I have a dataframe with IDs and timestamps as a multi-index. The index in the dataframe is sorted by IDs and timestamps and I want to pick the lastest timestamp for each IDs. for example: IDs timestam
With an associative array such as that one: $fruits = array(d=>lemon, a=>orange, b=>banana, c=>apple); I tried accessing to a value using : $fruits[2]; This gives me a PH
I have an observable array: var myObservableArray = ko.observableArray([ { name: Car, price: 9999 }, { name: Shoes, price: 20 }, { name: Paper, price: 1 } ]); I'm trying to access the pri
I have a table where the last column in each row contains a little loading icon which I would like to display when a button inside the table is clicked. When each table row is generated with ng-repea
Let's say I have two fields A and B and one textbox B_input. I would like to set up a query so it sums all entries of column A where B = B_input Currently I have: ==Sum(IIf([B_input]<>All,[A],
I wonder if I can deny accessing http://domain.tld/index.php and allow only http://domain.tld/index.php?var=val Is this possible with a rewritecond and rewriterule? I cannot figure this out. Any help?
I need to perform a regular expression search for a string x in another string y, but I then need to know the token (word) index of the first character of the hit after tokenizing (splitting) string y
I have an existing (working!) linq expression: from ca in db.CustomAnswer join ss in db.SurveySubmission on ca.SubmissionId equals ss.Id join cq in db.CustomQuestion on ca.QuestionId equals cq.Id wher
i have a split front-end and back-end access database. i am planning to move it into sql server. the front end will be c# linq to sql. question: is it going to be possible to have multiple users use t
I am trying to export some data that I have in some Backbone collections to a csv file. So far I am opening a new export page using java script like so var href = []; href.push('ExportAnalysis.aspx
There are at least two ways that I know of to write a Symbian application: 1. J2ME 2. A native application. My question is, does the SDK/API for either of those methods (or any other method) grant me
Visual studio 2010 has added a call hierarchy function. Does anyone know if it is possible to access this from within a plugin?
Is it possible to access HttpContext.Current.Session through a WebAPI ? can we make it inheriting IRequiresSession? I have a generic handler doing a Session set after an API call which I want to remov
Currently developing a Windows Phone 8 application in pure C++/Direct3D. We now need to access the Device Manufacturer name but it seems to only be available under C# which, according to my research i
We use ELMAH error exception logging in our application. I'd like to keep ELMAH secure from regular users while still making it available to administrators/developers of the application. When you set
I'm adding AppleScript support to my sandboxed Mac App Store app. I would like the user to be able to add files to the app through a script, using a command I wrote, like so: add file at path /path/t
I have the following HTML: <div id=main> <a id=sub1>sub1</div> <a id=sub2>sub2</div> </div> <div id=other style=display:none> some stuff </div>
I have read through the examples here, but it does not seem to include the following case. Let A be a three dimensional array with dimensions 128 x 128 x 3. I want to pick sets of 3 integers at random
I posted recently a question about the memory overhead due to virtuality in C++. The answers allow me to understand how vtable and vptr works. My problem is the following : I work on supercomputers, I
Inspired by this question. Short version: Why can't the compiler figure out the compile-time type of M(dynamic arg) if there is only one overload of M or all of the overloads of M have the same return
I have a strange problem, at least one I've never come across. I have a precondition where customers have simple regular expressions associated with labels. The labels are all they care about. What I
I know it is possible to retrieve a property name or a method with a return type. But is it also possible to get a method name without a return type via LINQ expression trees? Example: string methodna
Why am I receiving the error: Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer express at this code: @model IEnumerable
To begin with, I know about C++ Standard (ISO/IEC 14882:2003): Section 11.5, Paragraph 1, and this is not that case (but compliler apparently does not think so). I try to call protected base class met
I want to declare a generic collection of objects and be able to access them through the indexer either by a key string value or by index. How do I do this? Is there is an out of the box .Net class th
I am running the below complex statement to provide an value in Column SODist that increments for each successive matching value in the SalesOrderNo field. I use an ID as auto-number to simulate
I'm trying to get the data from a third party website that has some public data, more specifically, Catholic Daily Liturgy from http://www.cnbb.org.br. They have it in Portuguese until the end of the
I'm sorry if this question has been asked already but I did search and couldn't find it. Basically I have a drop down menu in my page that normally needs to appear on top of the main content. But then