?
Solved

using square brackets in mongo for array

Posted on 2013-11-23
10
Medium Priority
?
1,380 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 111

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 111

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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 46

Accepted Solution

by:
Chris Stanyon earned 2000 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 111

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 46

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 111

Expert Comment

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
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.

584 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