Delete index of a nested element in mongo - php

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)));

Related

In Simplesamlphp The IdP should check which NameIDFormat the SP requests, and try to send back that format

I have configured multiple SP in saml20-sp-hosted.php , i want one of my SP to use mail attribute as nameid.
saml20-sp-hosted.php
$metadata['entityId1'] = array (
'entityid1' => 'entityId',
'contacts' =>
array (
0 =>
array (
'contactType' => 'technical',
'givenName' => 'XXX',
'surName' => 'XXX',
'emailAddress' =>
array (
0 => 'XXX#YYY.com',
),
),
),
'metadata-set' => 'saml20-sp-remote',
'attributes' => array('mail', 'sn', 'givenName', 'uid'),
'AssertionConsumerService' =>
array (
0 =>
array (
'Binding' => 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST',
'Location' => 'http://www.XXXYYY.com/SSO/ACS.aspx',
'index' => 0,
),
),
'NameIDFormat' => 'urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress',
);
for given above SP i want to use "mail" attribute as nameid but i have defined 'uid' attribute in given below idp file so it is overwriting the attribute.
If i am trying to overwrite with 'mail' attribute in saml20-sp-hosted.php file it is posting transient nameid.
saml20-idp-hosted.php
'authproc' => array(
3 => array(
'class' => 'saml:AttributeNameID',
'attribute' => 'uid',
'Format' => 'urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress',
),
50 => array(
'class' => 'core:AttributeMap',
'mail' => 'Email',
'sn' => 'Lastname',
'givenName' => 'Firstname',
// 'memberOf' => 'Groups',
'uid' => 'Username'
//'cn' => array('name', 'displayName', 'sn'),
),
),
Are you sure your IdP side file is saml20-idp-hosted.php, not saml20-sp-remote.php?

How to output value of an array

I am a complete beginner in PHP. However I know how to output the value of custom field. I am having a bit of problems with arrays. The post meta key is fw_options. The value has multiple arrays and looks like this:
array (
0 =>
array (
'featured_post' => false,
'featured_expiry' => '',
'_featured_book_string' => '0',
'reading_level' => 'medium',
'reading_type' =>
array (
'time' => 'fixed',
'hourly' =>
array (
'hourly_read' => '',
'estimated_hours' => '',
),
'fixed' =>
array (
'reading_times' => '500',
),
),
'reading_duration' => 'one_month',
'english_level' => 'fluent',
'readers_level' => 'starter',
'expiry_date' => '2019/12/31',
'show_attachments' => 'off',
'read_documents' =>
array (
),
'address' => '',
'longitude' => '',
'latitude' => '',
'country' =>
array (
0 => '717',
),
),
)
I have this code which I have tried with no success:
$array = get_post_meta( get_the_ID(), 'fw_options', true );
echo $array[0]['reading_type']['fixed']['reading_times'];
How can I output the value 500 from the post meta key reading_times?
Simple Print array according to key
First hold the value into a variable than print according to array key
$data = array(
'featured_post' => false,
'featured_expiry' => '',
'_featured_book_string' => '0',
'reading_level' => 'medium',
'reading_type' =>
array(
'time' => 'fixed',
'hourly' =>
array(
'hourly_read' => '',
'estimated_hours' => '',
),
'fixed' =>
array(
'reading_times' => '500',
),
),
'reading_duration' => 'one_month',
'english_level' => 'fluent',
'readers_level' => 'starter',
'expiry_date' => '2019/12/31',
'show_attachments' => 'off',
'read_documents' =>array(),
'address' => '',
'longitude' => '',
'latitude' => '',
'country' =>
array(
0 => '717',
),
);
echo $data['reading_type']['fixed']['reading_times'];
Output #500

Change array value and save to database in PHP

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

PHP Multi-Dimensional Arrays Issue

Ok I have a mult-dimensional array which has the following structure...
0 =>
array (
'membership' =>
array (
'member' =>
array (
'name' => '',
'landline' => '',
'libcard' => '',
'mobile' => '',
'email' => '',
),
'updated_at' => '',
'member_id' => 12345,
'starts_at' => '',
'id' => 14,
'group_id' => 280,
'optional_field_values' =>
array (
0 =>
array (
'optional_field' =>
array (
'name' => '',
'updated_at' => '',
'id' => 1,
'group_id' => 280,
'description' => '',
'created_at' => '',
),
'updated_at' => '',
'optional_field_id' => 1,
'membership_id' => 14,
'id' => 4,
'value' => '12539267',
'created_at' => '',
),
),
'ends_at' => '',
'joining_fee' => 0,
'created_at' => '',
),
),
Now I can access everything inside Membership and inside Member using code like...
$member[0]['membership']['member']['name']
or
$member[0]['membership']['joining_fee']
But when ever I try to access stuff inside optional_field_values I get nothing returned...
Any ideas why this is not working?
Edit:
Trying to access the field using code like...
$member[0]['membership']['optional_field_values']['value']
$member[0]['membership']['optional_field_values'][0]['value']
^ Should work...
(Edited to match OP's edit)
How about :
$member[0]['membership']['optional_field_values'][0]['value']
You can iterate over all optional field values like this :
foreach ($member[0]['membership']['optional_field_values'] as $field)
echo $field['value'];

MongoDB pull array element from a collection

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" } } }

Categories