[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

returning an element from JSON

Posted on 2013-05-20
2
Medium Priority
?
363 Views
Last Modified: 2013-06-03
Im trying to get just 1 element from a JSON request:-
switch (json_decode($rowNewInput['json'])['type']) { 

Open in new window


The contents of "$rowNewInput['json']" are:-

{
"type" : "combo",
"attribute" : "SQL",
"values" : "Select distinct `First Name` from `staff`;"
}

And all I want is the 'combo' value.

Although I could decode the JSON into a variable and then pull the variable out of the created array, I was hoping to do it all on one line.

However I get an error to saying:-
Parse error: syntax error, unexpected '['

Any suggestions what Im doing wrong?

Thanks in advance.
0
Comment
Question by:tonelm54
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 39180747
JSON uses object-oriented notation, not array notation.  Try the -> instead of the brackets and quotes.

Also, trying to "do it all on one line" is a fools errand.  That kind of code makes it harder to debug and understand the data structures.  Use as many statements as needed, no less, no more!
http://www.laprbass.com/RAY_temp_tonelm54.php

<?php // RAY_temp_tonelm54.php
error_reporting(E_ALL);

$jso = <<<EOD
{
"type" : "combo",
"attribute" : "SQL",
"values" : "Select distinct `First Name` from `staff`;"
}
EOD;

// DECODE THE STRING INTO AN OBJECT
$obj = json_decode($jso);

// ACTIVATE THIS TO SEE WHAT IS IN THE OBJECT
// var_dump($obj);

// WHAT IS IN THE type PROPERTY
var_dump($obj->type);

Open in new window

Best regards, ~Ray
0
 
LVL 34

Expert Comment

by:Slick812
ID: 39182270
greetings  tonelm54, , not sure about how you "think" about the methods you use in your code, as you have a PHP ['type']  array access for what you have as a javascript object in the javascript object notation string. I have to work in PHP and javascript at the same time to build pages, and often get mixed up in the code syntax for each,
In javascript you are allowed to access OBJECT properties with array methods like - jsobj['type'] - , but in PHP this is not allowed, , I often forget which Object access to use, and put -> in javascript and put a "." in PHP, which causes fatal errors in either one.
If you are used to javascript, you will need to watch your code when changing to PHP code writing, as many things like ['type'] for object access will get you  -
Parse error: syntax error, unexpected '['

and -
switch (json_decode($rowNewInput['json'])->type) {
just might work

but if I forget and use -
switch (json_decode($rowNewInput['json']).type) {
it does Not work.

for your own development error trace, I have found that compound abstraction code like -
switch (json_decode($rowNewInput['json'])->type) {
is harder to debug.

I like this better for debugging -
    $newIn = json_decode($rowNewInput['json']);
    switch ($newIn->type) {
because you can more clearly see what line and method causes the error
0

Featured Post

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question