MongoDB + PHP: How to push item to array with a spcecific key? - php

In MongoDB-PHP I am using the following example code to push a new entry to the end of an array inside a collection...
$data = array(
"domain"=>"superduperyoyo.com",
"number"=>123,
"week"=>5,
"year"=>2012
);
$db->domains->save(
array( 'someid' => $someid),
array( '$push' => array( 'data' => $data ))
);
This returns keys like 0,1,2,3....
ie.
[someid] => somesupercoolid123
[data] => Array
(
[0] => Array
(
[domain] => superduperyoyo.com
[number] => 123
[week] => 5
[year] => 2012
)
[1] => Array(...)
[2] => Array(...)
)
What I want to do is store YearWeekNumber as the key like this...
[someid] => somesupercoolid123
[data] => Array
(
[201205123] => Array
(
[domain] => superduperyoyo.com
[number] => 123
[week] => 5
[year] => 2012
)
[201206123] => Array(...)
[201207123] => Array(...)
)
How do you save/update the key along with the new entry? I am assuming you can't use $push. That you just use .save or .update but how do you pass the key?

You'd do this by using $set:
$data = array(
"domain"=>"superduperyoyo.com",
"number"=>123,
"week"=>5,
"year"=>2012
);
$db->domains->update(
array( 'someid' => $someid),
array( '$set' => array( 'data' => array( 201205123 => $data )))
);
I would however not recommend doing this. It's better to set another key with this "201205123" value as otherwise you wouldn't be able to do range queries on this value or set an index.

$data = array(
"domain"=>"superduperyoyo.com",
"number"=>123,
"week"=>5,
"year"=>2012
);
$update = array(
'$push' => array('data.201205123' => $data )
);

Related

How to change array format in PHP?

I have simple question:
How to change this array
Array ( [0] => taxonomy-term-1 [1] => taxonomy-term-2 [2] => taxonomy-term-3 )
to this array?
array( 'taxonomy-term-1', 'taxonomy-term-2', 'taxonomy-term-3' )
Why I am asking for that:
$slugs = wp_get_post_terms($post->ID,'ml_patternsCustomTaxonomy',['fields'=>'slugs']);
returns
Array ( [0] => taxonomy-term-1 [1] => taxonomy-term-2 [2] => taxonomy-term-3 )
but
array(
'title' => $title,
'content' => trim($patternContent),
'categories' => $slugs
)
doesn't work, and
//
'categories' => array( 'taxonomy-term-1', 'taxonomy-term-2', 'taxonomy-term-3' ),
works.
Why?

How to access *key value in array using php

i have below array with key combined * i want get the value using array key
i got response from api
Array (
[pos] => Bitpay\Token Object
(
[token:protected] => 8Q13oFMiBLBcqFCK5wWfhUYxxhcpkq4C6Xqh6ipgbxTm
[resource:protected] =>
[facade:protected] => pos
[createdAt:protected] =>
[policies:protected] => Array
(
)
[pairingCode:protected] =>
[pairingExpiration:protected] =>
)
[pos/invoice] => Bitpay\Token Object
(
[token:protected] => 4XyeM78xv6ywzTB3Cc2yak7Bb9duAW1DaCu5XDAVuSEQ
[resource:protected] =>
[facade:protected] => pos/invoice
[createdAt:protected] =>
[policies:protected] => Array
(
)
[pairingCode:protected] =>
[pairingExpiration:protected] =>
)
)
after i convert array i got below array structure, I want get a token:protected value, How can i get this
Array(
[*token] => 8Q13oFMiBLBcqFCK5wWfhUYxxhcpkq4C6Xqh6ipgbxTm
[*resource] =>
[*facade] => pos
[*createdAt] =>
[*policies] => Array
(
)
[*pairingCode] =>
[*pairingExpiration] =>
)
I want take get a token value, Any one Please help.
Using quotes should make it all possible:
<?php
$myArray = array(
'*token' => '8Q13oFMiBLBcqFCK5wWfhUYxxhcpkq4C6Xqh6ipgbxTm',
'*resource' => '',
'*facade' => 'pos',
'*createdAt' => '',
'*policies' => array
(
),
'*pairingCode' => '',
'*pairingExpiration' => '',
);
print $myArray['*token'];
?>

How to achive below multi dimentional array in PHP?

<?php
[USED] => Array
(
[USER] => Array
(
[#attributes] => Array
(
[NAME] => locuz
[HOST] => srpth1cn03.local
[IP] => 10.106.2.48
[USED_LICENSES] => 1
[LOGIN_TIME] => 2014-12-10 07:34
[CHECKOUT_TIME] => 2014-12-10 07:34
)
)
)
I want to change above array to below:
[USED] => Array
(
[USER] => Array
(
[0] => Array
(
[#attributes] => Array
(
[NAME] => rdtank
[HOST] => it30992
[IP] => 10.106.21.134
[DENIED_LICENSES] => 1
[LOGIN_TIME] => 2014-12-09 15:55
[DENIAL_TIME] => 2014-12-09 15:55
)
)
)
)
Put a [] before you save attributes as below.
foreach($all_attributes as $attributes){
$sample_array['USED']['USER'][] = $attributes;
}
the code you wrote seems the result of 2 array dump.
so if you need to setup 2 variable in php that contains data you should use this code
<?php
$used = array(
'users' => array(
array(
'name' => 'locuz',
'host' => 'srpth1cn03.local',
'ip' => '10.106.2.48',
'used_licenses' => 1,
'login_time' => '2014-12-10 07:34',
'checkout_time' => '2014-12-10 07:34'
)
)
);
$denied = array(
'users' => array(
array(
'name' => 'rdtank',
'host' => 'it30992',
'ip' => '10.106.21.134',
'denied_licenses' => 1,
'login_time' => '2014-12-09 15:55',
'denial_time' => '2014-12-09 15:55'
)
)
);
hope this helps.

Codeigniter XML-RPC Response Multi Dimensional Array Approach

I need to create an XML-RPC server that gets cities with their corresponding IDs. What I do as a response is looking weird to me because of unnecessary duplicate entries but I couldnt find a better way.
Array
(
[cityID] => Array
(
[0] => 34
[1] => 35
[2] => 06
)
[cityName] => Array
(
[0] => Istanbul
[1] => Izmir
[2] => Ankara
)
)
I implemented above response. With this implementation:
$response = array(
array(
'cityID' => array(array('34', '35', '06'), 'array'),
'cityName' => array(array('Istanbul', 'Izmir', 'Ankara'), 'array')
),
'struct'
);
The problem is I want to take a response like this :
Array
(
[cities] => Array
(
['34'] => 'Istanbul'
['35'] => 'Izmir'
['06'] => 'Ankara'
)
)
So I tried to implement it like this :
$response = array(
array(
'cities' => array(array('34'=>'Istanbul', '35'=>'Izmir', '06'=>'Ankara'), 'array')
),
'struct'
);
But it fails with this implementation. What am I doing wrong ?
Thanks
You have array like following
$response = array ( 'cityID' => array (
0 => 34,
1 => 35,
2 => 06
),
'cityName' => array(
0 => 'Istanbul',
1 => 'Izmir',
2 => 'Ankara'
)
);
$newarray = array();
foreach($response['cityID'] as $key => $cityid){
$newarray['cities'][$cityid] = $response['cityName'][$key];
}
print_r($newarray);
You will be getting the expected array.
Array
(
[cities] => Array
(
[34] => Istanbul
[35] => Izmir
[6] => Ankara
)
)
This is how I do it, in Code Igniter 3
$array = array ( 'cityID' => array (
0 => 34,
1 => 35,
2 => 06
),
'cityName' => array(
0 => 'Istanbul',
1 => 'Izmir',
2 => 'Ankara'
)
);
foreach($array['cityID'] as $key => $cityid){
$response[] = array(array(
$cityid => array($array['cityName'][$key],'string'),
),'struct');
}
return $this->xmlrpc->send_response(array($response,'array'));

Removing unnecessary nested index in array (generated by Cake PHP)

The following question can either be solved by probably changing my use of the find method in Cake PHP OR using some PHP function. I would prefer to solve it using Cake but it doesn't matter too much. I have any array like this:
Array
(
[0] => Array
(
[Model] => Array
(
[id] => 14
[foo] => bar
)
)
[1] => Array
(
[Model] => Array
(
[id] => 15
[foo] => something
)
) .............
I just want to remove the Model index and just use the numeric one. The following function generated this array:
$arr = $this->Model->find('all', array('contain' => false ) );
I probably need to change the 'contain' part of the call. Basically, in addition to the data that appears under each Model index, I also have a second and third model and the contain = false just restricts Cake from getting data from the current model (Model).
If I understand your question correctly, I think CakePHP's Set::combine function will help http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::combine :
$result = Set::combine($your_array, '{n}.Model.id', '{n}.Model.data');
This will result in:
Array
(
[14] => Array
(
[foo] => bar
)
[15] => Array
(
[foo] => something
)
)
you have to write your own piece of code to modify your array , here is a function that will do what you want (you can improve it)
function reformArray($array,$modelName)
{
foreach($arr as $key => $value)
{
$newArr[] = $arr[$key][$modelName];
}
return $newArr;
}
you have to pass your array and the Model Name to this function and will return you the result
$a = array(
array(
'User' => array(
'id' => 2,
'group_id' => 1,
'Data' => array(
'user' => 'mariano.iglesias',
'name' => 'Mariano Iglesias'
)
)
),
array(
'User' => array(
'id' => 14,
'group_id' => 2,
'Data' => array(
'user' => 'phpnut',
'name' => 'Larry E. Masters'
)
)
),
array(
'User' => array(
'id' => 25,
'group_id' => 1,
'Data' => array(
'user' => 'gwoo',
'name' => 'The Gwoo'
)
)
)
);
RUN THIS TO REMOVE THE MODEL NAME USER
Set::extract($a, '{n}.User');
WILL RETURN THIS
$a = array(
array(
'id' => 2,
'group_id' => 1,
'Data' => array(
'user' => 'mariano.iglesias',
'name' => 'Mariano Iglesias'
)
),
array(
'id' => 14,
'group_id' => 2,
'Data' => array(
'user' => 'phpnut',
'name' => 'Larry E. Masters'
)
),
array(
'id' => 25,
'group_id' => 1,
'Data' => array(
'user' => 'gwoo',
'name' => 'The Gwoo'
)
)
);
In CakePHP 2:
$data = $this->Model->find('all');
$data = Set::extract('/Model/.', $data );
You can achieve this result by foreach also
foreach ($data as $k => &$t) {
$t = $t['Model']
}
It will be much easier with Object. Change the Array with Object.
Actually I had faced the same problem with nested array And I found the solution with Set::map($array);
If you don't want the model_name as nested array, You can prefer this solution.
$data = array(
array(
"IndexedPage" => array(
"id" => 1,
"url" => 'http://blah.com/',
'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
'get_vars' => '',
'redirect' => '',
'created' => "1195055503",
'updated' => "1195055503",
)
),
array(
"IndexedPage" => array(
"id" => 2,
"url" => 'http://blah.com/',
'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
'get_vars' => '',
'redirect' => '',
'created' => "1195055503",
'updated' => "1195055503",
),
)
);
$mapped = Set::map($data);
/* $mapped now looks like: */
Array
(
[0] => stdClass Object
(
[_name_] => IndexedPage
[id] => 1
[url] => http://blah.com/
[hash] => 68a9f053b19526d08e36c6a9ad150737933816a5
[get_vars] =>
[redirect] =>
[created] => 1195055503
[updated] => 1195055503
)
[1] => stdClass Object
(
[_name_] => IndexedPage
[id] => 2
[url] => http://blah.com/
[hash] => 68a9f053b19526d08e36c6a9ad150737933816a5
[get_vars] =>
[redirect] =>
[created] => 1195055503
[updated] => 1195055503
)
)
you may do this :
$tmp = $this->Model->find('all', array('contain' => false ) );
$arr = $tmp['ModelName'];

Categories