Solved

using square brackets in mongo for array

Posted on 2013-11-23
10
1,082 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
[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
  • 4
  • 4
  • 2
10 Comments
 
LVL 110

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 110

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
Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

 
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 110

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 110

Expert Comment

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

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
The viewer will learn how to count occurrences of each item in an array.
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 …

717 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