Solved

using square brackets in mongo for array

Posted on 2013-11-23
10
900 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 108

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 108

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
 
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 42

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
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 108

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 42

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 108

Expert Comment

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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now