I have a mongodb object as follows:
array (
'_id' => new MongoId("4cc97fb0247ae8747ec5fefb"),
'posts' =>
array (
0 =>
array (
'comment' => 'Eamorr',
'fromUname' => 'Eamorr',
'time' => 1288273840,
'UTC' => '2010-10-28T14:50:40+01:00',
'ip' => '127.0.0.1',
'id' => '123lasdfiqwoei28asdf',
),
1 =>
array (
'comment' => 'Hello',
'fromUname' => 'Eamorr',
'time' => 1288277023,
'UTC' => '2010-10-28T15:43:43+01:00',
'ip' => '127.0.0.1',
'id' => 'qopqwier982389qwfa',
),
2 =>
array (
'comment' => 'Hello',
'fromUname' => 'Anonymous',
'time' => 1288283506,
'UTC' => '2010-10-28T17:31:46+01:00',
'ip' => '127.0.0.1',
'id' => 'ioqwoeias892398wrf',
),
/////
//Want to remove element 3:
/////
3 =>
array (
'comment' => 'asdfasadf',
'fromUname' => 'Anonymous',
'time' => 1288283864,
'UTC' => '2010-10-28T17:37:44+01:00',
'ip' => '127.0.0.1',
'id' => 'wwwwwiasdfn234oiasf',
),
4 =>
array (
'comment' => 'asdfasdfasdf',
'fromUname' => 'Anonymous',
'time' => 1288284076,
'UTC' => '2010-10-28T17:41:16+01:00',
'ip' => '127.0.0.1',
'id' => '290qwefoiqweproiqwerpq',
),
5 =>
array (
'comment' => 'ASDF',
'fromUname' => 'Eamorr',
'time' => 1288284331,
'UTC' => '2010-10-28T17:45:31+01:00',
'ip' => '127.0.0.1',
'id' => 'eioqw8923892hasdf',
),
6 =>
array (
'comment' => 'ASDF2',
'fromUname' => 'Eamorr',
'time' => 1288284370,
'UTC' => '2010-10-28T17:46:10+01:00',
'ip' => '127.0.0.1',
'id' => '23oaiofsaij234',
),
),
'uname' => 'Eamorr',
)
Now, I am writing some PHP code to remove posts[x]. I'm trying to use $pull to remove the array element.
I have an 'id' 'wwwwwiasdfn234oiasf' (array element 3) and I want to remove this entire array element, leaving just 6 elements in the 'posts' array.
I've tried googling and looking up the documentation to no avail... I still can't get the hang of the mongodb syntax.
I'm doing all this in PHP, but any language will do I should be able to do the translation.
Many thanks in advance,
Solution (in PHP):
$uname=whatever
$id=whatever
$mongo=new Mongo();
$walls=$mongo->people->walls;
$walls->update(array('uname'=>$uname),array('$pull'=>array('posts'=>array('id'=>$id))));
Here's how to do it using the MongoDB shell. You should be able to translate it into PHP.
A pull operation consists of the $pull modifier, a field selector and a value expression.
{ $pull: { fieldSelector: valueExpression } }
In your case the field selector is posts, since that's the array you want to update. The value expression, in plain English, is
where the id of the post equals "wwwwwiasdfn234oiasf"
This translates to { id: "wwwwwiasdfn234oiasf" }. If we combine all of this, you'll get the following $pull statement, which will remove the desired item from the array:
{ $pull: { posts: { id: "wwwwwiasdfn234oiasf" } } }
Related
I'm storing data to an array like this which is inside three nested loops (loops omitted):
$teamDetails[$k] = array(
'side' => $json['data'][$i]['rosters'][$k]['side'],
'gold' => $json['data'][$i]['rosters'][$k]['gold'],
'aces' => $json['data'][$i]['rosters'][$k]['aces_earned'],
'herokills' => $json['data'][$i]['rosters'][$k]['hero_kills'],
'winner' => translateGame($json['data'][$i]['rosters'][$k]['winner']),
'participants'[$j] => array(
'work' => 'it worked',
)
);
How can make 'participants' an array with the indices coming from $j?
That's easy
$teamDetails[$k] = array(
'side' => $json['data'][$i]['rosters'][$k]['side'],
'gold' => $json['data'][$i]['rosters'][$k]['gold'],
'aces' => $json['data'][$i]['rosters'][$k]['aces_earned'],
'herokills' => $json['data'][$i]['rosters'][$k]['hero_kills'],
'winner' => translateGame($json['data'][$i]['rosters'][$k]['winner']),
'participants' => array(
$j => array(
'work' => 'it worked',
))
);
array (
'_attributes' =>
array (
'name' => 'Rothco Product API - Variations',
),
'item_variations' =>
array (
0 =>
stdclass::__set_state(array(
'item_variation_id' => 2,
'item_index' => 3146,
'rothco_item_no' => '10002',
'upc' => '023063601212',
'inventory' => 99,
'created_date' => '2014-11-28 10:06:45.000',
'weight' => '.4000',
'image_filename' => '10002-A.jpg',
'catalog_page_no' => 183,
'msrp' => '20.9900',
'map' => '.0000',
'diameter' => '',
'price' => '8.100000',
'case_price' => NULL,
'case_quantity' => NULL,
'statuses' => '',
)),
),
)
This is my array $list.
I want to access 'item_variations' value from this array,
but if I try $list['item_variations'], or $list->['item_variations']
it is not working
If you want to reach just item_variations then
echo $list['item_variations'];
is sufficient. Things get more tricky if you would like to i.e. get value if created_date from your sample data as you got mix of arrays and objects so that require different access:
echo $list['item_variations'][0]->created_date;
which would output
2014-11-28 10:06:45.000
how i can change specific array value in PHP? i have a Movie casts info that is a array:
array (
0 =>
array (
'name' => 'Mia Wasikowska',
'id' => 'nm1985859',
'url' => 'http://www.imdb.com/name/nm1985859/',
'image' => 'http://ia.media-imdb.com/images/M/MV5BMTUzNzI0MDczN15BMl5BanBnXkFtZTgwNTQ0MjMxOTE#._V1_.jpg',
'character' => 'Alice Kingsleigh',
),
1 =>
array (
'name' => 'Johnny Depp',
'id' => 'nm0000136',
'url' => 'http://www.imdb.com/name/nm0000136/',
'image' => 'http://ia.media-imdb.com/images/M/MV5BMTM0ODU5Nzk2OV5BMl5BanBnXkFtZTcwMzI2ODgyNQ##._V1_.jpg',
'character' => 'Hatter Tarrant Hightopp',
),
2 =>
array (
'name' => 'Helena Bonham Carter',
'id' => 'nm0000307',
'url' => 'http://www.imdb.com/name/nm0000307/',
'image' => 'http://ia.media-imdb.com/images/M/MV5BMTUzMzUzMDg5MV5BMl5BanBnXkFtZTcwMDA5NDMwNA##._V1_.jpg',
'character' => 'Iracebeth',
),
3 =>
array (
'name' => 'Anne Hathaway',
'id' => 'nm0004266',
'url' => 'http://www.imdb.com/name/nm0004266/',
'image' => 'http://ia.media-imdb.com/images/M/MV5BNjQ5MTAxMDc5OF5BMl5BanBnXkFtZTcwOTI0OTE4OA##._V1_.jpg',
'character' => 'Mirana',
),
)
i need to change image value to something different. for example, change this:
'image' => 'http://ia.media-imdb.com/images/M/MV5BNjQ5MTAxMDc5OF5BMl5BanBnXkFtZTcwOTI0OTE4OA##._V1_.jpg',
to this:
'image' => '/uploads/images/artirst_0214.jpg',
and in the end i need to have it like this:
array (
0 =>
array (
'name' => 'Mia Wasikowska',
'id' => 'nm1985859',
'url' => 'http://www.imdb.com/name/nm1985859/',
'image' => '/uploads/images/artirst_032.jpg',
'character' => 'Alice Kingsleigh',
),
1 =>
array (
'name' => 'Johnny Depp',
'id' => 'nm0000136',
'url' => 'http://www.imdb.com/name/nm0000136/',
'image' => '/uploads/images/artirst_07897.jpg',
'character' => 'Hatter Tarrant Hightopp',
),
2 =>
array (
'name' => 'Helena Bonham Carter',
'id' => 'nm0000307',
'url' => 'http://www.imdb.com/name/nm0000307/',
'image' => '/uploads/images/artirst_0987.jpg',
'character' => 'Iracebeth',
),
3 =>
array (
'name' => 'Anne Hathaway',
'id' => 'nm0004266',
'url' => 'http://www.imdb.com/name/nm0004266/',
'image' => '/uploads/images/artirst_0214.jpg',
'character' => 'Mirana',
),
)
thank you
$ArrayKeys=array_keys($data);
$cntKeys=count($ArrayKeys);
for ($i=0;$i<$cntKeys;$i++)
{
$data[$ArrayKeys[$i]]["image"]="ciro";
}
print_r ($data);
Let's suppose that your array is called "data".
In this way you get can the keys of your array (0,1,2....) and on each of them you iterate to access to the img value...
Then you can access to the value using $data[key_your_array]["image"] = "something". I use
$data[$ArrayKeys[$i]]
because, as the name suggest, $ArrayKeys is an array and I want to access to the i-th element
How do I get the value of 'name'?
I've been trying various methods, including array($charge) and __toArray.
First works, second does not:
$charge_id = $charge->id;
echo $charge_id;
$name=$charge->_values->name;
echo $name;
Here is portion of the object returned by Stripe (up to the name I want to extract):
Stripe\Charge::__set_state(array(
'_opts' =>
Stripe\Util\RequestOptions::__set_state(array(
'headers' =>
array (
),
'apiKey' => 'sk_test_...',
)),
'_values' =>
array (
'id' => 'ch_73JaKii01nmbpZ',
'object' => 'charge',
'created' => 1443251926,
'livemode' => false,
'paid' => true,
'status' => 'succeeded',
'amount' => 1000,
'currency' => 'usd',
'refunded' => false,
'source' =>
Stripe\Card::__set_state(array(
'_opts' =>
Stripe\Util\RequestOptions::__set_state(array(
'headers' =>
array (
),
'apiKey' => 'sk_test_...',
)),
'_values' =>
array (
'id' => 'card_73JZrJCDzl877J',
'object' => 'card',
'last4' => '1111',
'brand' => 'Visa',
'funding' => 'unknown',
'exp_month' => 12,
'exp_year' => 2021,
'fingerprint' => 'hhzQQZIXaAVDNMPP',
'country' => 'US',
'name' => 'Steve Veltkamp',
according to this:
https://github.com/stripe/stripe-php/blob/master/lib/Charge.php
you may try to play with retrieve method:
$charge::retrieve($charge->id);
or ( https://github.com/stripe/stripe-php/blob/master/lib/StripeObject.php ):
$chargeArr = $charge->__toArray(true);
add true as argument for recursion
(Note: you should edit your question to remove your secret key and replace it with e.g. 'sk_test_...'. Even if it's just a test key, secret keys are not meant to be shared.)
The property you're trying to access is the cardholder's name. The source attribute of a charge object is actually a card object:
php > echo get_class($charge);
Stripe\Charge
php > echo get_class($charge->source);
Stripe\Card
So to get the cardholder's name, all you have to do is:
$name = $charge->source->name;
Note that name is an optional attribute for cards, so it's possible that you'll get NULL, an empty string, an email address, etc.
I have a collection in mongoDb. it is similar to the following.
array(
'_id' => new MongoId("50b35d1217ce10ac1000000f")
'Education' =>
array (
'content' =>
array (
'0' =>
array (
'Organization' => 'SUST',
'Degree' => 'BSC',
'Department' => '',
'Location' => 'Dhaka',
'Session' => '2 Years',
),
'1' =>
array (
'Organization' => 'DU',
'Degree' => 'BSC',
'Department' => '',
'Location' => 'Dhaka',
'Session' => '2 Years',
)
),
'sharing' => 'public',
),
)
I want to delete Education.content.1 from the collection.
So i used the
update(array('_id' => new MongoId('50b35d1217ce10ac1000000f')), array('$unset' => array('Education.content.1' => 1)));
As a result Education.content.1 becomes null.
But I want Education.content.1 to be deleted not to be null.
Please help me if any one knows the solution. Thanks in advance.
Use $pull after $unset:
update(array('_id' => new MongoId('50b35d1217ce10ac1000000f')),
array('$unset' => array('Education.content.1' => 1)));
update(array('_id' => new MongoId('50b35d1217ce10ac1000000f')),
array('$pull' => array('Education.content' => null)));