Removing duplicates in an array having Apache_Solr_Document object in PHP - 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

Related

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

How can I sort an array based on date element?

I have an array as follow in php. It contains of two array of std objects.
How can I sort it based on report_date element?
as you see report_date is unix time.
Array(
[current] => Array
(
[0] => stdClass Object
(
[city_fa_name] => مشهد ۳
[report_date] => 1440963000
[id] => 3
)
[1] => stdClass Object
(
[city_fa_name] => مشهد ۳
[report_date] => 1441049400
[id] => 2
)
)
[saved] => Array
(
[0] => stdClass Object
(
[city_fa_name] => مشهد ۳
[report_date] => 1441049400
[id] => 2
)
[1] => stdClass Object
(
[city_fa_name] => مشهد ۳
[report_date] => 1441135800
[id] => 1
)
))
thanks in advance.

Retrieve the value from the array and then add it to the new PHP

i have big problem, because i don't know how get values from this array where value is be key into new array. This is my source array
Array
(
[0] => Array
(
[ID] => 250602
[NAME] => qwe
)
[1] => Array
(
[ID] => 250603
[NAME] => wer
)
[2] => Array
(
[ID] => 250629
[NAME] => sdf
)
[3] => Array
(
[ID] => 250629
[NAME] => xcv
)
[4] => Array
(
[ID] => 250629
[NAME] => fghfgh
)
[5] => Array
(
[ID] => 250601
[NAME] => pggd
)
[6] => Array
(
[ID] => 250601
[NAME] => dfgdfg
)
[7] => Array
(
[ID] => 250606
[NAME] => dfgdfg
)
)
When id is the same it will be created a new table that will look like for id = 250629
[NAME] => Array
(
[0] => sdf
[1] => xcv
[2] => fghfgh
)
How about foreach loop like this?
<?php
$final_array=array();
foreach($arrays as $sub_arr){ //it will traverse loop for all sub-arrays
$final_array[$sub_arr['ID']][]=$sub_arr['NAME'];
}
print_r($final_array); //you should see expected output.
?>
It will product below output for your given data:
Array
(
[250602] => Array
(
[0] => qwe
)
[250603] => Array
(
[0] => wer
)
[250629] => Array
(
[0] => sdf
[1] => xcv
[2] => fghfgh
)
[250601] => Array
(
[0] => pggd
[1] => dfgdfg
)
[250606] => Array
(
[0] => dfgdfg
)
)
Working Demo
Like this
$by_name = array();
foreach($your_array as $item)
$by_name[$item['ID']] []= $item['name'];
This makes use of php's lazy array initialization ([]= creates a new array implicitly).
If you get your array from mysql, you might also consider GROUP_CONCAT.

PHP - updating multidimensional arrays

Using foreach loops in PHP I would like to add ids to the following object...
$array_before
Array
(
[1111] => Array
(
[Name] => Name A
[Subcats] => Array
(
[11111] => Array
(
[Name] => Name A.1
)
[11112] => Array
(
[Name] => Name A.2
)
)
)
[2222] => Array
(
[Name] => Name B
[Subcats] => Array
(
[22221] => Array
(
[Name] => Name B.1
)
[22222] => Array
(
[Name] => Name B.2
)
)
)
)
... so it looks similar to the below:
$array_after
Array
(
[1111] => Array
(
[Id] => 1
[Name] => Name A
[Subcats] => Array
(
[11111] => Array
(
[Id] => 1
[Name] => Name A.1
)
[11112] => Array
(
[Id] => 2
[Name] => Name A.2
)
[11113] => Array
(
[Id] => 3
[Name] => Name A.2
)
)
)
[2222] => Array
(
[Id] => 2
[Name] => Name B
[Subcats] => Array
(
[22221] => Array
(
[Id] => 1
[Name] => Name B.1
)
[22222] => Array
(
[Id] => 2
[Name] => Name B.2
)
)
)
)
Could someone point me in the right direction?
Thanks,
LG
Try this:
$id = 0;
array_walk($array,function(&$a) use (&$id) {$a['id'] = ++$id;});
This will modify the originl array to add the IDs, rather than create a new one.

Merging two objects

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!

Categories