Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1213
  • Last Modified:

using square brackets in mongo for array

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
debbieau1
Asked:
debbieau1
  • 4
  • 4
  • 2
1 Solution
 
Ray PaseurCommented:
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
 
debbieau1Author Commented:
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
 
Ray PaseurCommented:
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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
debbieau1Author Commented:
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
 
Chris StanyonCommented:
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
 
Ray PaseurCommented:
@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
 
debbieau1Author Commented:
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
 
debbieau1Author Commented:
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
 
Chris StanyonCommented:
I've already posted the answer for you so it's a bit late to request a delete!
0
 
Ray PaseurCommented:
Agree with @ChrisStanyon: This is not an answer!
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now