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']);
}
Related
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
I have following array from google api and wanted to get id only. How do i get id's from following. when i try to get size it gives me size zero.
Google_Service_Drive_FileList Object ( [collection_key:protected] => items
[internal_gapi_mappings:protected] => Array ( ) [etag] => [itemsType:protected]
=> Google_Service_Drive_DriveFile [itemsDataType:protected] => array [kind] =>
[nextLink] => [nextPageToken] => [selfLink] => [modelData:protected] => Array (
[items] => Array ( [0] => Array ( [id] => 0B0OnHwH_cQckeWZPdXFyRU5aMGs ) [1] =>
Array ( [id] => 0B0OnHwH_cQckaUVORkZaM2NoRXM ) [2] => Array ( [id] =>
1kCQLhEgzgeKO-L57ISWjQL4ctkxT4Gq2wrdzDFbrcac ) [3] => Array ( [id] =>
0B0OnHwH_cQckc3RhcnRlcl9maWxl ) [4] => Array ( [id] => 1-Yhs92vZnvUNArwAcZJZ9xa-
fXZ7ZgRrADyF-ikG1gU ) ) ) [processed:protected] => Array ( ) ) Array ( )
if you mean the object attributes, you can do
$values = array();
$keys = array();
foreach($object as $key=>$value)
{
array_push($keys,$key);
array_push($values,$value);
}
This will give you each object attribute and its corresponding value
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;
}
I have an multidimensional array but need to make it smaller.
This is an easy question I believe. I need to remove 1 array in
jsonresult array, the first one but preserve the other in the next array. I have tried array_splice but it only keeps one.
Array
(
[searchword] => search word
[jsonresult] => Array
(
[0] => Array // THIS ONE, KEEP ITS CHILDREN MOVE UP
(
[0] => Array
(
[id] => 14889770
)
[1] => Array
(
[id] => 14389720
)
[2] => Array
(
[id] => 14869723
)
)
[1] => Array // THIS ONE, KEEP ITS CHILDREN MOVE UP
(
[0] => Array
(
[id] => 14889722
)
[1] => Array
(
[id] => 14389711
)
[2] => Array
(
[id] => 14869329
)
)
)
)
Would like to get:
Array
(
[searchword] => search word
[jsonresult] => Array
(
[0] => Array
(
[id] => 14889770
)
[1] => Array
(
[id] => 14389720
)
[2] => Array
(
[id] => 14869723
)
[3] => Array
(
[id] => 14889722
)
[4] => Array
(
[id] => 14389711
)
[5] => Array
(
[id] => 14869329
)
)
)
Try this code. This may not be the correct method but it gives what you need. (As I understand from your question)
//creating a sample array similar to one you given in question.
$arr_test['searchword'] = 'search word';
$arr_test['jsonresult'] = array(array(array('id'=>14889770),array('id'=>14889720)),array(array('id'=>14889780),array('id'=>14889790)));
//creating new array
$arr_new = array();
//formatting array as you needed it
foreach($arr_test['jsonresult'] as $arr_jsonresult){
foreach($arr_jsonresult as $jsonresult){
$arr_new['jsonresult'][] = $jsonresult;
}
}
//overwriting the specific array key
$arr_test['jsonresult'] = $arr_new['jsonresult'];
//checking output
echo '<pre>';
print_r($arr_test);
This code produces the following output
Array
(
[searchword] => search word
[jsonresult] => Array
(
[0] => Array
(
[id] => 14889770
)
[1] => Array
(
[id] => 14889720
)
[2] => Array
(
[id] => 14889780
)
[3] => Array
(
[id] => 14889790
)
)
)
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