how to remove primary key from result set in laravel mongodb? - php

I have following query
$tag_query = UserTags::query();
$tag_query->whereIn('tag_id' , $insert_data['to_device']);
$tag_users = $tag_query->get(['user_id']);
it is generating following result -
Array
(
[0] => Array
(
[_id] => 57da358e7ac6f6740e8b456a
[user_id] => 57d67290823fb647dd174739
)
[1] => Array
(
[_id] => 57da358e7ac6f6740e8b456c
[user_id] => 57d672cb823fb647dd17473a
)
[2] => Array
(
[_id] => 57da358e7ac6f6740e8b4571
[user_id] => 57d67549d81e1845e4dba983
)
)
I don't want _id in result array , how can I remove it without using loop?
expected output -
Array
(
[0] => Array
(
[user_id] => 57d67290823fb647dd174739
)
[1] => Array
(
[user_id] => 57d672cb823fb647dd17473a
)
[2] => Array
(
[user_id] => 57d67549d81e1845e4dba983
)
)
please help! Thanks!

You can apply projections to your queries using the project() method:
$tag_query = UserTags::query();
$tag_query->whereIn('tag_id' , $insert_data['to_device']);
$tag_users = $tag_query->project(['_id' => 0, 'user_id' => 1])->get();

Related

How to edit inner array inside another array in php?

I have an array coming as below which is dynamic. Now I want to edit a specific array for "BHD0000000002". I am getting the following array through ajax request. However I cannot edit the inner array element. For example, I am trying to edit the inner array value for the key "BHD0000000002" but unable to fix the issues. Can anyone help sir/madam?
This is the original array:
$budget_detail_array=Array
(
[0] => Array
(
[BHD0000000001] => 10000
)
[1] => Array
(
[BHD0000000002] => 12212121
)
[2] => Array
(
[BHD0000000003] => 212121
)
[3] => Array
(
[BHD0000000004] => 212121
)
[4] => Array
(
[BHD0000000005] => 212121
)
[5] => Array
(
[BHD0000000006] => 2121
)
[6] => Array
(
[BHD0000000007] => 2121
)
[7] => Array
(
[BHD0000000008] => 21221
)
[8] => Array
(
[BHD0000000009] => 2112212
)
)
I want to edit the above array as :
Array
(
[0] => Array
(
[BHD0000000001] => 10000
)
[1] => Array
(
[BHD0000000002] => 5000
)
[2] => Array
(
[BHD0000000003] => 212121
)
[3] => Array
(
[BHD0000000004] => 212121
)
[4] => Array
(
[BHD0000000005] => 212121
)
[5] => Array
(
[BHD0000000006] => 2121
)
[6] => Array
(
[BHD0000000007] => 2121
)
[7] => Array
(
[BHD0000000008] => 21221
)
[8] => Array
(
[BHD0000000009] => 2112212
)
)
So far I have tried with the following code but it is not working. Anyone can help?
foreach($budget_detail_array as $key=>$value){
foreach($value as $keyval=>$val){
if($keyval=='BHD0000000002'){
$val=5000;
}
}
/*if($value[$budget_id]){
$value[$budget_id]=100;
}else{
$value[$budget_id]=700;
}*/
}
print_r($budget_detail_array);exit;
In order to modify an associative array value you can do this:
$budget_detail_array[1]['BHD0000000002'] = 5000;
And then check the result again
print_r($budget_detail_array);

How to remove parent array index from array

I want to remove parent array index in array.
Following is my array.
Array
(
[0] => Array
(
[0] => Array
(
[id] => 296
[username] => David0123
[profile_slug] => david-love
)
)
[1] => Array
(
[0] => Array
(
[id] => 297
[username] => Anne_wils
[profile_slug] => anne-chase
)
)
[2] => Array
(
[0] => Array
(
[id] => 300
[username] => malina001
[profile_slug] => malina-reid
)
)
)
And I want like this way..
Array(
[0] => Array
(
[id] => 296
[username] => David0123
[profile_slug] => david-love
)
[1] => Array
(
[id] => 297
[username] => Anne_wils
[profile_slug] => anne-chase
)
[2] => Array
(
[id] => 300
[username] => malina001
[profile_slug] => malina-reid
)
)
I used following script for it but not work.
$myMainArray = json_decode(json_encode($allEscorts),true);
$i=0;
foreach( array_values($myMainArray) as $k=> $val){
echo $val[$i]['id'];
$i++;
}
I want to display data each element but first i have to remove parent array indexes.
You can use array_map to pull values up one level
$myMainArray = json_decode(json_encode($allEscorts),true);
$myMainArray = array_map(function($el) {
return $el[0];
}, $myMainArray);
You should check if the first array could be generate as you wish.
If not you can use array_map to get the first index from the inner-array.
for example:
$result = array_map(function($item){
return $item[0]; // always return the first array-index
}, $first_array);

PHP how to create a new multidimensional array with 1 or more of the keys are same?

I have the following Multidimensional arrays
if($this->conn->row_count($sql)==true){
while($row=$statement->fetch()){
$problems[]=[
'order_id'=>$row['order_id'],
'details'=>['remark'=>$row['remark'],'date'=>$row['date']]
];
}
return $problems;
}
And then i print it out
echo '<pre>',print_r($problems),'</pre>';
Result
Array
(
[0] => Array
(
[order_id] => 4683
[details] => Array
(
[remark] => happy
[date] => 2018-06-09---10:16:41am
)
)
[1] => Array
(
[order_id] => 4683
[details] => Array
(
[remark] => unhappy
[date] => 2018-06-10---03:29:00am
)
)
[2] => Array
(
[order_id] => 4440
[details] => Array
(
[remark] => very unhappy
[date] => 2018-06-10---03:34:00am
)
)
)
I am trying to group them into a new Multidimensional Array if same order_id and join details. order_id could have more than 2 because there may more remarks on this order_id *just like the following
Array
(
[0] => Array
(
[order_id] => 4683
[details] => Array
(
[remark] => happy
[date] => 2018-06-09---10:16:41am
),
(
[remark] => unhappy
[date] => 2018-06-10---03:29:00am
)
)
[1] => Array
(
[order_id] => 4440
[details] => Array
(
[remark] => very unhappy
[date] => 2018-06-10---03:34:00am
)
)
)
For a clearer picture, please take a look at the screenshot
Any idea how to create a new array just like the Output?
You can group it by an associative array. You can use array_values to convert the associative array into a simple array.
$problems = [];
if($this->conn->row_count($sql)==true){
while($row=$statement->fetch()){
if ( !isset( $problems[ $row['order_id'] ] ) ) {
$problems[ $row['order_id'] ] = [
'order_id' => $row['order_id'],
'details' => []
];
}
$problems[ $row['order_id'] ]['details'][] = ['remark'=>$row['remark'],'date'=>$row['date']];
}
return array_values( $problems );
}

Cakephp getting all data form left side table

I am using following binding
$this->FranchiseZip->bindModel(array(
'belongsTo'=>array(
'Franchise',
),
),false
);
it will return result in below format
Array
(
[0] => Array
(
[FranchiseZip] => Array
(
[id] => 1
)
[Franchise] => Array
(
[name] => abc
)
)
but i have many records in FranchiseZip model where Franchise id is null. it will not return those rows. Please tell me how i fetch those rows in cakephp. i want result in following format
Array
(
[0] => Array
(
[FranchiseZip] => Array
(
[id] => 1
)
[Franchise] => Array
(
[name] => abc
)
)
[1 => Array
(
[FranchiseZip] => Array
(
[id] => 123
)
[Franchise] => Array
(
[name] =>
)
)
thanks

PHP echo out multidimensional array from facebook var

I have a facebook array and I am trying to echo out the names and ID.
All I have is this array: $friends_list.
When I use this code:
print_r($friends_list);
Then the following comes out below: But how do I loop thru these? Or what if I just wanted to find out how many names there are or just call on one var by id. In short how do I echo out $friends_list which is a multiD array?
Array ( [data] => Array ( [0] => Array ( [name] => Wendy Cukierski [id] => 524320383 ) [1] => Array ( [name] => Leticia Mercado Correa [id] => 537763225 ) [2] => Array ( [name] => Olivia Urbina [id] => 538711855 ) [3] => Array ( [name] => Ruth Albrecht [id] => 541610111 ) [4] => Array ( [name] => Josh Brahm [id] => 577546485 ) [5] => Array ( [name] => Kim Yu [id] => 583515871 ) [6] => Array ( [name] => SisterTracey Dugas [id] => 643567171 ) [97] => Array ( [name] => Mary Martinez [id] => 100004696266210 ) ) [paging] => Array ( [next] => https://graph.facebook.com/1566027944/friends?limit=5000&offset=5000&__after_id=100004696266210 ) )
DUPLICATE --> How to store a Facebook user's friends list in MySQL using PHP?
$friends_list = file_get_contents('https://graph.facebook.com/me/friends?access_token=' . $atoken );
$friends_list_array = json_decode($friends_list,true);
$arr= $friends_list_array['data'];
$friend_ids_arr = array();
foreach($arr as $friend) {
$friend_ids_arr[] = $friend['id'];
}
$friend_ids = implode("," , $friend_ids_arr);
echo $friend_ids; // output: id1,id2,id3...etc

Categories