Merging two objects - php

Quotes object:
Array
(
[0] => Array
(
[sitecaptions] => Array
(
[id] => 2
[caption] => Great camera deals!
[linkurl] => http://www.99hotdeals.com/cat/Cameras
and Camcorders
)
)
)
The Posts Object:
Array
(
[0] => Array
(
[Post] => Array
(
[id] => 2797
[post_title] => xx1
[item_desc] => xx desc
[dateadded] => 2009-12-22 11:10:15
)
[Category] => Array
(
[0] => Array
(
[id] => 99
[name] => Others
)
)
)
[1] => Array
(
[Post] => Array
(
[id] => 2798
[post_title] => xx2
[item_desc] => xx2 desc
[dateadded] => 2009-12-22 11:10:45
)
[Category] => Array
(
[0] => Array
(
[id] => 99
[name] => Others
)
)
)
)
As you can see, the Posts Object contains two elements, [Post] and
[Category] for each record [0],[1] etc. I want to insert the
[sitecaptions] element into that Posts Object so that in effect it
looks like:
Array
(
[0] => Array
(
[Post] => Array
(
[id] => 2797
[post_title] => xx1
[item_desc] => xx desc
[dateadded] => 2009-12-22 11:10:15
)
[Category] => Array
(
[0] => Array
(
[id] => 99
[name] => Others
)
)
[sitecaptions] => Array
(
[id] => 2
[caption] => Great camera deals!
[linkurl] => http://www.99hotdeals.com/cat/Cameras
and Camcorders
)
)
)
How do I combine two objects like that? Or how do I insert elements
into an existing object? Hope I'm clear about what I'm asking. Thanks
for your time...

lets call these objects $Quotes and $Posts respectively.
Quotes object:
Array (
[0] => Array (
[sitecaptions] => Array (
[id] => 2
[caption] => Great camera deals!
[linkurl] => http://www.99hotdeals.com/cat/Cameras and Camcorders )
)
)
)
The Posts Object:
Array (
[0] => Array (
[Post] => Array (
[id] => 2797
[post_title] => xx1
[item_desc] => xx desc
[dateadded] => 2009-12-22 11:10:15
)
[Category] => Array (
[0] => Array (
[id] => 99
[name] => Others
)
)
)
[1] => Array (
[Post] => Array (
[id] => 2798
[post_title] => xx2
[item_desc] => xx2 desc
[dateadded] => 2009-12-22 11:10:45
)
[Category] => Array (
[0] => Array (
[id] => 99
[name] => Others
)
)
)
)
do you want the [sitecaptions] from the $quotes to be in both of your $posts elements? or just the one with the same key?
as to say if you have just $quotes[0] only $posts[0] will be affected. OR both $posts[0] and $posts[1] will be affected.
if you want $quotes[0] to be in both $post elements you can do this:
foreach ($posts as $key=>$post) {
$posts[$key]['sitecaptions'] = $quotes[0]['sitecaptions'];
}
if you want only the elements from $quotes that have the same index as the elements in $posts you can do this:
$posts = array_merge_recursive($posts,$quotes);
doing this second one would have $posts[1] being without a ['sitecaptions'] element.
end result:
Array (
[0] => Array (
[Post] => Array (
[id] => 2797
[post_title] => xx1
[item_desc] => xx desc
[dateadded] => 2009-12-22 11:10:15
)
[Category] => Array (
[0] => Array (
[id] => 99
[name] => Others
)
)
[sitecaptions] => Array (
[id] => 2
[caption] => Great camera deals!
[linkurl] => http://www.99hotdeals.com/cat/Cameras and Camcorders
)
)
)
Hope it helps!

Related

Adobe insights api not giving data for few metrics

When I run reports in adobe experience cloud for few metrics with page dimension, all metrics are coming correctly, but when I am trying to pull same data via api, few revenue metrics(e.g carts,orders,cartadditions,productinstances) are coming as zero.
I am using curl request to fetch data with access token generated from JWT (Service Account) Authentication.
Here is the my array for post request -
Array
(
[rsid] => XXX
[dimension] => variables/page
[globalFilters] => Array
(
[0] => Array
(
[type] => dateRange
[dateRange] => 2022-04-01T00:00:00.000/2022-04-02T00:00:00.000
)
)
[metricContainer] => Array
(
[metrics] => Array
(
[0] => Array
(
[id] => metrics/pageviews
)
[1] => Array
(
[id] => metrics/visitors
)
[2] => Array
(
[id] => metrics/exits
)
[3] => Array
(
[id] => metrics/bounces
)
[4] => Array
(
[id] => metrics/entries
)
[5] => Array
(
[id] => metrics/carts
)
[6] => Array
(
[id] => metrics/orders
)
[7] => Array
(
[id] => metrics/cartadditions
)
[8] => Array
(
[id] => metrics/productinstances
)
)
[metricFilters] => Array
(
)
)
[settings] => Array
(
[metricSort] => desc
[limit] => 5000
[page] => 0
[countRepeatInstances] => 1
)
)
when I try to fetch almost the same metrics but on daterange dimension then they are coming fine.
Array
(
[rsid] => XXX
[dimension] => variables/daterangeday
[globalFilters] => Array
(
[0] => Array
(
[type] => dateRange
[dateRange] => 2022-04-01T00:00:00.000/2022-04-02T00:00:00.000
)
)
[metricContainer] => Array
(
[metrics] => Array
(
[0] => Array
(
[id] => metrics/visitors
)
[1] => Array
(
[id] => metrics/bounces
)
[2] => Array
(
[id] => metrics/entries
)
[3] => Array
(
[id] => metrics/carts
)
[4] => Array
(
[id] => metrics/orders
)
[5] => Array
(
[id] => metrics/cartadditions
)
[6] => Array
(
[id] => metrics/productinstances
)
)
[metricFilters] => Array
(
)
)
[settings] => Array
(
[metricSort] => desc
[limit] => 5000
[page] => 0
[countRepeatInstances] => 1
)
)
I am new to Adobe and have very limited access to Adobe experience cloud. What could be the issue.

PHP array reorganize with unique count

I have following array which has ID as index, and some count as value. Ultimate goal is to get total of unique IDs in another array.
Array
(
[0] => Array
(
[0] => Team Object
(
[id] => 1
[countStat] => 25
)
)
[1] => Array
(
[0] => Team Object
(
[id] => 2
[countStat] => 24
)
)
[2] => Array
(
[0] => Team Object
(
[id] => 1
[countStat] => 23
)
)
[3] => Array
(
[0] => Team Object
(
[id] => 3
[countStat] => 23
)
)
[4] => Array
(
[0] => Team Object
(
[id] => 5
[countStat] => 21
)
)
[5] => Array
(
[0] => Team Object
(
[id] => 3
[countStat] => 21
)
)
[6] => Array
(
[0] => Team Object
(
[id] => 5
[countStat] => 20
)
)
[7] => Array
(
[0] => Team Object
(
[id] => 2
[countStat] => 20
)
)
)
I want result like below.
Array
(
[0] => Array
(
[0] => Team Object
(
[id] => 1
[countStat] => 48
)
)
[1] => Array
(
[0] => Team Object
(
[id] => 2
[countStat] => 44
)
)
[3] => Array
(
[0] => Team Object
(
[id] => 3
[countStat] => 44
)
)
[4] => Array
(
[0] => Team Object
(
[id] => 5
[countStat] => 41
)
)
)
I has spend few hours working on it but couldn't get solution. Can someone please help ?
Thank you.
Looks like you want to sum team object's countStats for like ids.
I've created a similar data structure, looped through it to form a temporary array that uses the id as a key, with associated amounts (to later sum - mine is amount, yours is countStats).
Then sum those and recreate desired output.
<?php
class Team
{
public $id;
public $amount;
public function __construct($id, $amount)
{
$this->id = $id;
$this->amount = $amount;
}
}
$input =
[
[new Team(1, 3)],
[new Team(1, 4)],
[new Team(2, 5)],
[new Team(2, 7)]
];
foreach($input as $subarray)
$amounts[$subarray[0]->id][]=$subarray[0]->amount;
print_r($amounts);
foreach($amounts as $k => $v)
$result[] = [new Team($k, array_sum($v))];
print_r($result);
Output:
Array
(
[1] => Array
(
[0] => 3
[1] => 4
)
[2] => Array
(
[0] => 5
[1] => 7
)
)
Array
(
[0] => Array
(
[0] => Team Object
(
[id] => 1
[amount] => 7
)
)
[1] => Array
(
[0] => Team Object
(
[id] => 2
[amount] => 12
)
)
)

Unable to extract data from array in php

The array is received from the facebook api and i am not able to extract the likes array from the array,Please help me
[data] => Array (
[0] => Array (
[message] => Hello
[id] => 729659027165160_729651713832558
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 729659027165160
)
)
[paging] => Array (
[cursors] => Array (
[after] => NzI5NjU5MDI3MTY1MTYw
[before] => NzI5NjU5MDI3MTY1MTYw
)
)
)
)
[1] => Array (
[id] => 729659027165160_718306454967084
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 1719747118259908
)
)
[paging] => Array (
[cursors] => Array (
[after] => MTcxOTc0NzExODI1OTkwOA==
[before] => MTcxOTc0NzExODI1OTkwOA==
)
)
)
)
[2] => Array (
[id] => 729659027165160_541135166017548
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 1162428970453842
)
)
[paging] => Array (
[cursors] => Array (
[after] => MTE2MjQyODk3MDQ1Mzg0Mg==
[before] => MTE2MjQyODk3MDQ1Mzg0Mg==
)
)
)
)
[3] => Array (
[message] => Panipaata leni prathivaadu philosophy cheppevade.... Wish Facebook introduce an unlike button soon!!!!
[id] => 729659027165160_520677651396633
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 1162428970453842
)
[1] => Array (
[id] => 806391372817118
)
[2] => Array (
[id] => 928633297192567
)
[3] => Array (
[id] => 824812004311172
)
[4] => Array (
[id] => 10207344532684729
)
[5] => Array (
[id] => 1188171664544003
)
)
[paging] => Array (
[cursors] => Array (
[after] => MTE4ODE3MTY2NDU0NDAwMw==
[before] => MTE2MjQyODk3MDQ1Mzg0Mg==
)
)
)
)
[4] => Array (
[id] => 729659027165160_110578795739856
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 1162428970453842
)
)
[paging] => Array (
[cursors] => Array (
[after] => MTE2MjQyODk3MDQ1Mzg0Mg==
[before] => MTE2MjQyO
)
)
)
)
)
I am able to extract the id from the above array,but unable to extract the count of likes and message.
Try this code, it iterates in your array and stores all ids and stores the message, the likes Array and the number of likes only if they exist (Supposing that your array is named $myarray):
$result = array();
foreach($myarray['data'] as $data){
$item = array();
$item['id'] = $data['id'];
if( isset($data['message']) || isset($data['likes']) ){
if(isset($data['message'])) $item['message'] = $data['message'];
if(isset($data['likes'])) {
$item['likes'] = array();
foreach($data['likes']['data'] as $like){
$item['likes'][] = $like['id'];
}
$item['countlikes'] = count( $data['likes']['data'] );
}
}
$result[] = $item;
}
print_r($result);
With your example Array the result will be:
Array
(
[0] => Array
(
[id] => 729659027165160_729651713832558
[message] => Hello
[likes] => Array
(
[0] => 729659027165160
)
[countlikes] => 1
)
[1] => Array
(
[id] => 729659027165160_718306454967084
[likes] => Array
(
[0] => 1719747118259908
)
[countlikes] => 1
)
[2] => Array
(
[id] => 729659027165160_541135166017548
[likes] => Array
(
[0] => 1162428970453842
)
[countlikes] => 1
)
[3] => Array
(
[id] => 729659027165160_520677651396633
[message] => Panipaata leni prathivaadu philosophy cheppevade.... Wish Facebook introduce an unlike button soon!!!!
[likes] => Array
(
[0] => 1162428970453842
[1] => 806391372817118
[2] => 928633297192567
[3] => 824812004311172
[4] => 10207344532684729
[5] => 1188171664544003
)
[countlikes] => 6
)
[4] => Array
(
[id] => 729659027165160_110578795739856
[likes] => Array
(
[0] => 1162428970453842
)
[countlikes] => 1
)
)

Removing duplicates in an array having Apache_Solr_Document object in PHP

Updated: I have two separate arrays which hold some data returned from search() function of SolrPhpClient. The arrays contain data in form of Apache_Sole_Document objects. These objects in turn contain the actual fields and values. I merge these two arrays to get a single array holding all items using array_merge() of PHP
The array will have some duplicate items which needs to be removed.
I am not sure how to achieve it in this structure.
The array structure is as such:
Array ( [0] => Apache_Solr_Document Object (
[_documentBoost:protected] =>
[_fields:protected] => Array ( [id] => 111 [name] => ABCD )
[_fieldBoosts:protected] => Array ( [id] => [name] => )
)
[1] => Apache_Solr_Document Object (
[_documentBoost:protected] =>
[_fields:protected] => Array ( [id] => 222 [name] => DEFG )
[_fieldBoosts:protected] => Array ( [id] => [name] => )
)
[2] => Apache_Solr_Document Object (
[_documentBoost:protected] =>
[_fields:protected] => Array ( [id] => 333 [name] => LMNO )
[_fieldBoosts:protected] => Array ( [id] => [name] => )
)
[3] => Apache_Solr_Document Object (
[_documentBoost:protected] =>
[_fields:protected] => Array ( [id] => 111 [name] => ABCD )
[_fieldBoosts:protected] => Array ( [id] => [name] => )
)
[4] => Apache_Solr_Document Object (
[_documentBoost:protected] =>
[_fields:protected] => Array ( [id] => 444 [name] => PQRS )
[_fieldBoosts:protected] => Array ( [id] => [name] => )
)
[5] => Apache_Solr_Document Object (
[_documentBoost:protected] =>
[_fields:protected] => Array ( [id] => 222 [name] => DEFG )
[_fieldBoosts:protected] => Array ( [id] => [name] => )
)
)
As you can see there is a [id] field and a [name] field.
I would like to remove duplicates from the array comparing the [id] field.
The final array after removing duplicates should look like this:
Array ( [0] => Apache_Solr_Document Object (
[_documentBoost:protected] =>
[_fields:protected] => Array ( [id] => 111 [name] => ABCD )
[_fieldBoosts:protected] => Array ( [id] => [name] => )
)
[1] => Apache_Solr_Document Object (
[_documentBoost:protected] =>
[_fields:protected] => Array ( [id] => 222 [name] => DEFG )
[_fieldBoosts:protected] => Array ( [id] => [name] => )
)
[2] => Apache_Solr_Document Object (
[_documentBoost:protected] =>
[_fields:protected] => Array ( [id] => 333 [name] => LMNO )
[_fieldBoosts:protected] => Array ( [id] => [name] => )
)
[3] => Apache_Solr_Document Object (
[_documentBoost:protected] =>
[_fields:protected] => Array ( [id] => 444 [name] => PQRS )
[_fieldBoosts:protected] => Array ( [id] => [name] => )
)
)
How can I achieve this? somebody please help!
I guess you could iterate over the array and remove the duplicates, but that doesn't seem like a neat solution to me.
I'm not familiar with the SolrPhpClient, but Solr does support grouping on fields. You can group on the id by adding this part to your request:
group=true&group.field=id
The documents returned will be grouped on the id. For more details check this page.
Update:
I looked at the SolrPhpClient documentation and see that you can add additional parameters to your request like this:
$additionalParameters = array(
'fq' => 'a filtering query',
'facet' => 'true',
'facet.field' => array(
'field_1',
'field_2'
)
);
$results = $solr->search($query, $start, $rows, $additionalParameters);
I assume you can add the grouping parameters to this:
$additionalParameters = array(
'group' => 'true',
'group.field' => 'id'
)
);
$results = $solr->search($query, $start, $rows, $additionalParameters);
For more details on this, check this page

Remove parent level array from set of arrays and merge nodes

I am generating menu from the database, it is working perfectly but only problem is, my parent node is ROOT which I have to remove from the array.
Array ( [0] => Array ( [label] => ROOT [url] => Array ( [0] => category/view [id] => 1 )
[items] => Array (
[0] => Array ( [label] => DESIGNERS [url] => Array ( [0] => category/view [id] => 2 ) )
[1] => Array ( [label] => WOMEN [url] => Array ( [0] => category/view [id] => 3 ) [items] => Array ( [0] => Array ( [label] => CURRENT SALES [url] => Array ( [0] => category/view [id] => 8 ) [items] => Array ( [0] => Array ( [label] => SUIT SELECTIONS [url] => Array ( [0] => category/view [id] => 10 ) ) ) ) [1] => Array ( [label] => ENDING SOON [url] => Array ( [0] => category/view [id] => 9 ) ) ) )
[2] => Array ( [label] => MEN [url] => Array ( [0] => category/view [id] => 4 ) )
[3] => Array ( [label] => MAKE IT YOUR OWN [url] => Array ( [0] => category/view [id] => 5 ) )
[4] => Array ( [label] => CLEARANCE [url] => Array ( [0] => category/view [id] => 6 ) )
[5] => Array ( [label] => OUT OF THE BOX [url] => Array ( [0] => category/view [id] => 7 ) ) ) ) )
I want the array to start with [items] => Array(), how do I remove the parent node.
The desired solution must be:
Array (
[0] => Array ( [label] => DESIGNERS [url] => Array ( [0] => category/view [id] => 2 ) )
[1] => Array ( [label] => WOMEN [url] => Array ( [0] => category/view [id] => 3 ) [items] => Array ( [0] => Array ( [label] => CURRENT SALES [url] => Array ( [0] => category/view [id] => 8 ) [items] => Array ( [0] => Array ( [label] => SUIT SELECTIONS [url] => Array ( [0] => category/view [id] => 10 ) ) ) ) [1] => Array ( [label] => ENDING SOON [url] => Array ( [0] => category/view [id] => 9 ) ) ) )
[2] => Array ( [label] => MEN [url] => Array ( [0] => category/view [id] => 4 ) )
[3] => Array ( [label] => MAKE IT YOUR OWN [url] => Array ( [0] => category/view [id] => 5 ) )
[4] => Array ( [label] => CLEARANCE [url] => Array ( [0] => category/view [id] => 6 ) )
[5] => Array ( [label] => OUT OF THE BOX [url] => Array ( [0] => category/view [id] => 7 ) ) ) ) )
I am not sure I understood well, is that what you want?
$desired_array=$old_array[0]['items']

Categories