How to create an array based upon two value of an array? - php

I have an array with multiple keys,
Array
(
[0] => stdClass Object
(
[type] => ab_micro_list
[title] => Testing List
[user_id] => 70318
[micro_list_id] => 390
)
[1] => stdClass Object
(
[type] => ab_micro_list
[title] => Testing List
[user_id] => 70319
[micro_list_id] => 390
)
)
based upon this array I need to create array like this
Array
(
['Testing List'] => Array
(
['users'] => Array
(
[0] => 70318
[1] => 70319
)
)
)
Is there any way to achieve this. If someone has any suggestions, Please share.

Try this.
$arr = array
(
[0] => stdClass Object
(
[type] => ab_micro_list
[title] => Testing List
[user_id] => 70318
[micro_list_id] => 390
)
[1] => stdClass Object
(
[type] => ab_micro_list
[title] => Testing List
[user_id] => 70319
[micro_list_id] => 390
)
);
$finalArr = array();
foreach($arr as $key=>$value){
$finalArr[$value->title]['users'][] = $value->user_id;
}

Related

Working with 2 json arrays with connected data via ID

Been trying to work with two arrays and extract information from the arrays. I can do this with the foreach but I have linked file references (an ID) which targets data in another array.
Array 1 -
Array
(
[type] => land
[id] => 0b1b522e-0cd2-4880-b3ac-a5d86bc2e837
[relationships] => Array
(
[address] => Array
(
[data] => Array
(
[type] => address
[id] => bb89e6c3-9114-4192-a0a7-82c08402d36e
)
)
[details] => Array
(
[data] => Array
(
[type] => details
[id] => f8f0e489-bb86-4857-a815-ab6338d90f26
)
)
[lettingsListing] => Array
(
[data] =>
)
[primaryImage] => Array
(
[data] => Array
(
[type] => media
[id] => 58574088-01a2-45da-a66e-cd3ce1741377
)
)
[images] => Array
(
[data] => Array
(
[0] => Array
(
[type] => media
[id] => 58574088-01a2-45da-a66e-cd3ce1741377
)
[1] => Array
(
[type] => media
[id] => 32d9d605-d55d-48b7-aa62-5e22f762f165
)
[2] => Array
(
[type] => media
[id] => c879656a-a34f-4c93-b6c7-49d3c7b11804
)
[3] => Array
(
[type] => media
[id] => 434f8d79-df19-474d-a275-6c9a5fb0985b
)
[4] => Array
(
[type] => media
[id] => cefccee0-cd30-4c69-9f6f-2bd76116a619
)
[5] => Array
(
[type] => media
[id] => c0377f6c-4279-4176-a5bd-0190b7fd97a8
)
[6] => Array
(
[type] => media
[id] => 6869c65a-bb55-4b9f-8dc1-71ffa5eb84dc
)
[7] => Array
(
[type] => media
[id] => c9b13725-059a-4c96-a8fd-77c9fc9fe05f
)
[8] => Array
(
[type] => media
[id] => edbdddb2-afc7-43ab-9a79-94e0e8e63597
)
[9] => Array
(
[type] => media
[id] => 320de2d7-6ecd-4ec7-9250-f72e228be8a7
)
[10] => Array
(
[type] => media
[id] => 63daaccf-d8d2-4b55-aece-5a01e379fba0
)
[11] => Array
(
[type] => media
[id] => 5d2c253a-03f5-4ea0-b06d-dd17c80d5f3b
)
)
)
)
)
The images in the array are like:
[0] => Array
(
[type] => media
[id] => 58574088-01a2-45da-a66e-cd3ce1741377
)
The ID then corresponds to another array (array 2)
Array
(
[type] => media
[id] => 58574088-01a2-45da-a66e-cd3ce1741377
[attributes] => Array
(
[name] => p048205_01
[order] => 1
[is_featured] =>
[feature_index] =>
[title] =>
[is_image] => 1
[url] => ***urlhere***
)
)
)
So my question is how to get the associated data ([url]) with each ID from the other array. I thought about array_merge but that did not help. My only other thinking is to do a foreach inside the current loop but I have heard it best to not to that?
Create a new array to hold the image urls from what you called array2, but which has the id as the key, so you can jump straight to the URL from your other foreach loop.
$img_urls = []
foreach( $array2 as $a ) {
$img_urls[$a['id']] = $a;
}
Or if you only want the url in this new array
$img_urls = []
foreach( $array2 as $a ) {
$img_urls[$a['id']] = $a['attributes']['url'];
}
As RiggsFolly said, you need to manipulate the second array with images like this:
$new_arr_images = [];
foreach ($arr_images as $value) {
$new_arr_images[$value['id']] = $value['attributes']['url'];
}
// then loop through first array and populate it with image urls based on the ID

PHP How to access the multi-dimensional arrays

I know there are so many solutions for multi-dimensional array access. But any of them didn't work for me.
I have this array and i need to access the name property by looping through the whole array. How do i access the Value 'TEXTPRINT' which is in the Name attribute?
Array
(
[0] => Array
(
[ParentSurvey] => Array
(
[attributes] => Array
(
[type] => SM_Survey__c
[url] => /services/data/v36.0/sobjects/SM_Survey__c/a181400000FHVy0AAH
)
[Id] => a181400000FHVy0AAH
[Company__c] => a0Qa000000ME65MEAT
[Type__c] => Company Critical Roles
[Company__r] => Array
(
[attributes] => Array
(
[type] => Company__c
[url] => /services/data/v36.0/sobjects/Company__c/a0Qa000000ME65MEAT
)
[Id] => a0Qa000000ME65MEAT
[Name] => TEXTPRINT
)
)
[DescList] => Array
(
[0] => Array
(
[attributes] => Array
(
[type] => SM_Survey_Detail__c
[url] => /services/data/v36.0/sobjects/SM_Survey_Detail__c/a171400000PkHmsAAF
)
[Id] => a171400000PkHmsAAF
[CR_Designation__c] => a0ka0000004r6biAAA
[SM_Survey__c] => a181400000FHVy0AAH
[CR_Designation__r] => Array
(
[attributes] => Array
(
[type] => Designation__c
[url] => /services/data/v36.0/sobjects/Designation__c/a0ka0000004r6biAAA
)
[Id] => a0ka0000004r6biAAA
[Name] =>Manager
)
)
I tried this code to print the value..
foreach ($result as $record){
print_r($record['Array']['ParentSurvey']['Company__r']['Name']);
}
Try this
foreach ($result as $record){
print_r($record['ParentSurvey']['Company__r']['Name']);
}

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.

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

Cakephp Set::sort()

im using cakephp 1.3. Im trying to sort an array using Set::sort() function but is not working.. any idea on how to do this? below is the array im using.
Array (
[0] => Array
(
[Group] => Array
(
[name] => Team A
)
[Members] => Array
(
[0] => Array
(
[name] => George
[Code] => Array
(
[name] => C
)
)
[1] => Array
(
[name] => Hall
[Code] => Array
(
[name] => A
)
)
[2] => Array
(
[name] => Mike
[Code] => Array
(
[name] => B
)
)
)
)
im sorting the array using this :
$data = Set::sort($data, '{n}.Members.{n}.Code.name', 'asc');
im expecting an output like this:
Array
(
[0] => Array
(
[Group] => Array
(
[name] => Team A
)
[Members] => Array
(
[0] => Array
(
[name] => Hall
[Code] => Array
(
[name] => A
)
)
[1] => Array
(
[name] => Mike
[Code] => Array
(
[name] => B
)
)
[2] => Array
(
[name] => George
[Code] => Array
(
[name] => C
)
)
)
)
The sorting does not take in effect.how can i do this? any idea?
Using only Set::sort() its no doable. You can you this:
$result = array();
foreach($a as $arr) {
$res = Set::sort($arr['Member'], '{n}.Code.name', 'asc');
$result[] = array(
'Group' => $arr['Group'],
'Member' => $res
);
}
pr($result);

Categories