PHP organise rows of the same values - php

I am retrieving data from sql server and then present them in an organised way.
I have an array of object like this:
Array
(
[0] => stdClass Object
(
[post_id] => 1
[controller] => Cato
[controller_method] => edit
[http_method] => post
)
[1] => stdClass Object
(
[post_id] => 1
[controller] => Cato
[controller_method] => edit
[http_method] => get
)
[2] => stdClass Object
(
[post_id] => 1
[controller] => Cato
[controller_method] => create
[http_method] => post
)
[3] => stdClass Object
(
[post_id] => 1
[controller] => Cato
[controller_method] => create
[http_method] => get
)
)
Somehow I need to convert to things like this:
Array(
[0] => Array(
[controller] => controller1,
[controller_methods] => Array (
[0] => Array (
[controller_method] => create,
[http_methods] => Array (
[0] => get,
[1] => post
)
),
[0] => Array (
[controller_method] => edit,
[http_methods] => Array (
[0] => get,
[1] => post
)
)
)
),
[0] => Array(
[controller] => controller2,
[controller_methods] => Array (
[0] => Array (
[controller_method] => create,
[http_methods] => Array (
[0] => get,
[1] => post
)
),
[0] => Array (
[controller_method] => edit,
[http_methods] => Array (
[0] => get,
[1] => post
)
)
)
)
)
I always encounter things like this when working with php with sql db
Is there any convenient way to do this?

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.

Reorder Multi Dimensional Array using Custom Keys

I have an array like below
Array
(
[0] => Array
(
[text] => one
[mp3] => 1.mp3
)
[1] => Array
(
[text] => two
[mp3] => 2.mp3
)
[2] => Array
(
[text] => three
[mp3] => 3.mp3
)
)
And I have another array with index to sort array(1,0,2) ,So With these I want following
Array
(
[0] => Array(
[text] => two[mp3] => 2. mp3
)
[1] => Array(
[text] => one[mp3] => 1. mp3
)
[2] => Array(
[text] => three[mp3] => 3. mp3
)
)
I googled and Found Few solutions on stackoverflow , But None seems to be successful for me
$order=array(1,0,2);
$orderedarray = array_merge(array_flip($order),$myarr);
$myarr = $orderedarray;
print_r($myarr);
Which outputs following
(
[0] => 0
[1] => 1
[2] => 2
[3] => Array
(
[text] => one
[mp3] => 1.mp3
)
[4] => Array
(
[text] => two
[mp3] => 2.mp3
)
[5] => Array
(
[text] => three
[mp3] => 3.mp3
)
)
You need to use array_multisort.
$order =[1,0,2];
array_multisort($myarr, $order);
Var_dump($myarr);
https://3v4l.org/cCT9d

Get a specific value from a multidimensional array provided by AgileCRM

I'm trying to get the first deal ID from AgileCRM.
When using:
$test = json_decode($deal, true);
print_r($test);
I get the following result:
Array (
[0] => Array (
[colorName] => WHITE
[id] => 5686812383117312
[apply_discount] =>
[discount_value] => 0
[discount_amt] => 0
[discount_type] => Value
[name] => New Home Loan
[contact_ids] => Array (
[0] => 5645056174194688
)
[custom_data] => Array (
)
[products] => Array (
)
[description] => New Lead
[expected_value] => 0
[milestone] => New Loan
[probability] => 10
[close_date] => 1521192269
[created_time] => 1510824270
[milestone_changed_time] => 0
[entity_type] => deal
[notes] => Array (
)
[note_ids] => Array (
)
[note_created_time] => 0
[pipeline_id] => 5719238044024832
[archived] =>
[lost_reason_id] => 0
[deal_source_id] => 0
[total_deal_value] => 0
[updated_time] => 1510824270
[isCurrencyUpdateRequired] => 1
[currency_conversion_value] => 0
[tags] => Array (
)
[tagsWithTime] => Array (
)
[contacts] => Array (
[0] => Array (
[id] => 5645056174194688
[type] => PERSON
[properties] => Array (
[0] => Array (
[type] => SYSTEM
[name] => first_name
[value] => piet
)
[1] => Array (
[type] => SYSTEM
[name] => last_name
[value] => pompies
)
[2] => Array (
[type] => SYSTEM
[name] => name
[value] =>
)
)
)
)
[owner] => Array (
[id] => 5178546118721536
[domain] => domainname
[email] => myemail#email.com
[phone] =>
[name] => Piet Pompies
[pic] => https://d1gwclp1pmzk26.cloudfront.net/img/gravatar/48.png
[schedule_id] => Piet Pompies
[calendar_url] => https://homeside.agilecrm.com/calendar/Piet_Pompies
[calendarURL] => https://homeside.agilecrm.com/calendar/Piet_Pompies
)
)
)
I want to echo "5686812383117312" from "[id] => 5686812383117312" (4th line in the array above)
I've tried "foreach" statements but my expertise on it is limited and can't seem to get it right.
Any help will be appreciated.
In order to access the ID field you should:
get the array's first key
Access the required field
Array:
Array ( //$test
[0] => Array ( //first key [0]
[colorName] => WHITE
[id] => 5686812383117312 //the required field ['id']
[apply_discount] =>
PHP:
$test = json_decode($deal, true);
print_r($test);
echo $test[0]['id']; //Output: 5686812383117312

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

Understanding FB Page IDs from API

I'm working with the the fb page and I have pulled posts from a use using:
$fbapp = PVConfiguration::getConfiguration('afbapp');
$facebook = new Facebook( array('appId' => $fbapp -> app_id, 'secret' => $fbapp -> app_secret, 'cookie' => true ));
$facebook->api( $this -> fb_page_id . '/feed');
Now a sample result looks something like this:
[0] => Array
(
[id] => 311007125672751_315405295232910
[from] => Array
(
[category] => Retail and consumer merchandise
[name] => Example Demos
[id] => 311007125672751
)
[story] => Phonefare Demos added a new photo.
[story_tags] => Array
(
[0] => Array
(
[0] => Array
(
[id] => 311007125672751
[name] => Example Demos
[offset] => 0
[length] => 15
[type] => page
)
)
)
[picture] => https://fbcdn-photos-e-a.akamaihd.net/hphotos-ak-ash2/576447_315405255232904_1958203550_s.jpg
[link] => https://www.facebook.com/photo.php?fbid=315405255232904&set=a.315405251899571.62826.311007125672717&type=1&relevant_count=1
[icon] => https://static.xx.fbcdn.net/rsrc.php/v2/yz/r/StEh3RhPvjk.gif
[actions] => Array
(
[0] => Array
(
[name] => Comment
[link] => https://www.facebook.com/311007125672751/posts/315405295232910
)
[1] => Array
(
[name] => Like
[link] => https://www.facebook.com/311007125672751/posts/315405295232910
)
)
[privacy] => Array
(
[description] => Public
[value] => EVERYONE
[friends] =>
[networks] =>
[allow] =>
[deny] =>
)
[type] => photo
[status_type] => added_photos
[object_id] => 315405255232904
[created_time] => 2012-12-21T15:04:37+0000
[updated_time] => 2012-12-21T15:04:37+0000
[shares] => Array
(
[count] => 2
)
[likes] => Array
(
[data] => Array
(
[0] => Array
(
[id] => 45608554
[name] => Michele A
)
)
[paging] => Array
(
[cursors] => Array
(
[after] => NDU2MDg1NjY=
[before] => NDU2MDg1NjY=
)
)
)
)
My greatest point of confusion is this. I want to store the post in the database as a bigint. But the return id is
311007125672751_315405295232910
, and I'm guessing its two IDS. So which ID should I be storing in the database to reference later? What are both IDs for? Is there a fundamental flaw for breaking the IDS apart to start them as BigInts/UUIDs?

Categories