Solved

using square brackets in mongo for array

Posted on 2013-11-23
10
969 Views
Last Modified: 2016-02-10
I am not clear how to add square brackets for array in mongod and php.  Have done via console, but not in php.

  $item = array(
    'mod' => (string)"firstone",
	'pages' => array('grocersl' => array([('types'=>array('fruit'=>array('read'=>1,'write'=>2))))),
	'bannedFruits' => (string)"mandarin"]
  );

Open in new window


Which goes in like

        "_id" : ObjectId("52913262d3ff4da428000000"),
        "type" : "grocers",
        "store" : {
                "stock" : {
                        "fruit" : {
                                "apples" : {
                                        "johnathon" : 1,
                                        "grannies" : 2
                                }
                        }
                }
        },
        "makeActive" : 1
------------
I neeed to produce the following and add the square brackets, to produce the array.  Adding the square brackets is the only bit i have difficulty with.

_id" : ObjectId("52913262d3ff4da428000000"),
        "type" : "grocers",
        "store" : {
                "stock" : {
                        "fruit" :[
                                     {
                                   "apples" : {
                                        "johnathon" : 1,
                                        "grannies" : 2
                                    },
                                     {
                                   "oranges" : {
                                        "sweet" : 1,
                                        "grannies" : 2
                                    }
                              ],
                        }
                }
        },
        "makeActive" : 1
0
Comment
Question by:debbieau1
  • 4
  • 4
  • 2
10 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39671967
The code snippet does not appear to match the JSON displays.  Can you please set up the SSCCE so we can replicate the issue and run some tests?  Thanks, ~Ray
0
 
LVL 1

Author Comment

by:debbieau1
ID: 39671981
Very sorry there was an error.  I have just  corrected and pasted the php code below.   I tested and with inserting $item does work now.

  $item = array(
    'mod' => (string)"grocers",
	'store' => array('stock' => array('fruit'=>array('apples'=>array('johnathon'=>1,'grannies'=>2)))),
	'makeActive' => 1,		
  );

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39672011
NP, none pf us are perfect!  Is the question answered or do you want to provide us with the SSCCE so we can offer some help?
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 1

Author Comment

by:debbieau1
ID: 39672096
Hi Ray
I might need to leave it and work through it myself.  i am not familiar with SSCCE, seems a bit to read and I'm really short on time on this one.  I just thought I could paste php code into the code blocks here, like I usually do.    Thanks anyway.
0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 39672597
The code you posted is not valid JSON (mismathcing and missing brackets ), so the following is only a guess (assuming you do want valid JSON):

<?php
$item = new stdClass;

$item->type = 'grocers';

$item->store->stock->fruit = array(
	array('apples' => array('jonathon'=>1, 'grannies'=>2)),
	array('oranges' => array('sweet'=>1, 'grannies'=>2))
);

$item->makeActive = 1;

echo json_encode($item);
?>

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39672673
@debbieau1: The whole point of the SSCCE (which is not really that long or hard to understand) is to help you save time and get a good working answer as fast as possible.  As with most things in PHP programming, the quality of the answer is directly related to the quality of the problem definition and the test data you give us.  So when the test data does not match the outputs you want, we are left wondering, "Which part of the example is correct?"

When I first looked at this question, I took the code snippet with the PHP $item = array... and installed it on my server.  It produced a parse error, so it was obvious that it hadn't come from a working PHP script.  It didn't match the JSON.  It's that sort of thing that slows down the whole process.

<?php // RAY_temp_debbieau1.php
error_reporting(E_ALL);
echo '<pre>';

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28302260.html


  $item = array(
    'mod' => (string)"firstone",
	'pages' => array('grocersl' => array([('types'=>array('fruit'=>array('read'=>1,'write'=>2))))),
	'bannedFruits' => (string)"mandarin"]
  );

Open in new window

Outputs: Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW) in /path/to/RAY_temp_debbieau1.php on line 10

While it may seem counter-intuitive, test-driven development is actually a much faster process than writing code, then trying to find the errors later.  Once you get to the point that you understand TDD, your work will go faster, perhaps a great deal faster, your work products will be more accurate and free of bugs, and you'll never want to go back to the old ways.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html

If you can show us the input data that you have, and show us the output that you want, we can almost always help you bridge the gap.  Often, if you post the data, you will be able to get tested and working code examples here at EE.  It's one of the great bargains on the internet!
0
 
LVL 1

Author Comment

by:debbieau1
ID: 39674270
Hi Ray

I appreciate your advice and not disagreeing.  In this instance I was only after a correct syntax, not really debugging of code.

I apologise,  I did not phrase the question better.

I have since solved it and no longer need help on this.  The structure of the nesting just needed adjusting.
0
 
LVL 1

Author Comment

by:debbieau1
ID: 39674276
I've requested that this question be closed as follows:

Accepted answer: 0 points for debbieau1's comment #a39674270

for the following reason:

The nested arrays, needed adjusting
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39674277
I've already posted the answer for you so it's a bit late to request a delete!
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39674329
Agree with @ChrisStanyon: This is not an answer!
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Is there a simpler dropbox system? 10 34
php help 34 58
Need to remove %20 from url in get 17 36
simple html dom php accessing table/cell  values 4 18
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses four methods for overlaying images in a container on a web page
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 …

839 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