PHP Array combine using one column value - php

I am trying to combine two Arrays with one common column.But, not getting exactly what I want. Please check the below requirement.
Array 1
Array
(
[0] => stdClass Object
(
[fieldLabel] => fname
[uuid] => 27478
)
[1] => stdClass Object
(
[fieldLabel] => Lname
[uuid] => 6103
)
[2] => stdClass Object
(
[fieldLabel] => Country
[uuid] => 7350
)
[3] => stdClass Object
(
[fieldLabel] => check1
[uuid] => 23155
)
[4] => stdClass Object
(
[fieldLabel] => radio1
[uuid] => 15664
)
)
Array 2
Array
(
[0] => stdClass Object
(
[uuid] => 27478
[value] => sai1
)
[1] => stdClass Object
(
[uuid] => 6103
[value] => sai2
)
[2] => stdClass Object
(
[uuid] => 7350
[value] => USA
)
[3] => stdClass Object
(
[uuid] => 23155
[value] => USA|India
)
)
I need output like the below. Both arrays 'UUID' is common. If value not there in second array it should be empty.
Array
(
[0] => stdClass Object
(
[fieldLabel] => fname
[uuid] => 27478
[value] =>sai1
)
[1] => stdClass Object
(
[fieldLabel] => Lname
[uuid] => 6103
[value] =>sai2
)
[2] => stdClass Object
(
[fieldLabel] => Country
[uuid] => 7350
[value] =>USA
)
[3] => stdClass Object
(
[fieldLabel] => check1
[uuid] => 23155
[value] =>USA|India
)
[4] => stdClass Object
(
[fieldLabel] => radio1
[uuid] => 15664
)
)
Please provide your suggestions. How can I achieve.

You can traverse those arrays and make changes like below :
foreach ($array1 as &$a1val) {
$value = 0;
foreach ($array2 as $a2val) {
if($a1val->uuid == $a2val->uuid) {
$value = $a2val->value;
break;
}
}
$a1val->value = $value;
}
P.S. :
& is used to update $array1 by reference.

foreach($arr1 as $key1 => val1){
foreach($arr2 as $key2 => $val2){
if($val1['uuid'] == $val2['uuid']){
$arr1[$key1]['value'] = $val2['value'];
}
}
}
print_r($arr1); // this should give desire output

Related

PHP parse soapClient response transfer to a table

I'm just on the hose and dont get on.
I am not a professional programmer but have so far with much reading and over numerous attempts everything so far created what I have undertaken, only with this I have probably found my masterpiece.
I have a response of a SOAP query and would like to display the values in a table for each powerUnitidentifier. What is the best way to do this?
(
[RawData] => stdClass Object
(
[from] => 2022-05-10T01:00:00+02:00
[to] => 2022-05-10T01:20:00+02:00
[dataRecords] => stdClass Object
(
[record] => Array
(
[0] => stdClass Object
(
[powerUnitIdentifier] => abc123
[time] => 2022-05-10T01:00:00+02:00
[fields] => stdClass Object
(
[field] => Array
(
[0] => stdClass Object
(
[identifier] => 100
[value] => 0
)
[1] => stdClass Object
(
[identifier] => 101
[value] => 3.27
)
[2] => stdClass Object
(
[identifier] => 102
[value] => 70.00
)
)
)
)
[1] => stdClass Object
(
[powerUnitIdentifier] => zyx321
[time] => 2022-05-10T01:00:00+02:00
[fields] => stdClass Object
(
[field] => Array
(
[0] => stdClass Object
(
[identifier] => 100
[value] => 0
)
[1] => stdClass Object
(
[identifier] => 101
[value] => 3.19
)
[2] => stdClass Object
(
[identifier] => 102
[value] => 70.00
)
)
)
)
[2] => stdClass Object
(
[powerUnitIdentifier] => abc123
[time] => 2022-05-10T01:10:00+02:00
[fields] => stdClass Object
(
[field] => Array
(
[0] => stdClass Object
(
[identifier] => 100
[value] => 0
)
[1] => stdClass Object
(
[identifier] => 101
[value] => 3.15
)
[2] => stdClass Object
(
[identifier] => 102
[value] => 70.00
)
)
)
)
[3] => stdClass Object
(
[powerUnitIdentifier] => zyx321
[time] => 2022-05-10T01:10:00+02:00
[fields] => stdClass Object
(
[field] => Array
(
[0] => stdClass Object
(
[identifier] => 100
[value] => 0
)
[1] => stdClass Object
(
[identifier] => 101
[value] => 3.09
)
[2] => stdClass Object
(
[identifier] => 102
[value] => 70.00
)
)
)
)
)
)
)
)```
You loop over the section of the data, starting the foreach loop at the right level of your data structure
foreach($theName->RawData->DataRecords->record as $obj) {
echo $obj->powerUnitIdentifier;
}
Or if you ment to process the sub array of that
foreach($theName->RawData->DataRecords->record as $obj) {
echo $obj->powerUnitIdentifier . '<br>';
foreach( $obj->fields as $field) {
echo $field->identifier . ',' . $field->value . '<br>';
}
}

i have an array with 8 values in it i want to print 4 in each row dynamically

this is the array
Array (
[0] => Array (
[0] => stdClass Object (
[cat_count] => 1
)
)
[1] => Array (
[0] => stdClass Object (
[cat_count] => 0
)
)
[2] => Array (
[0] => stdClass Object (
[cat_count] => 0
)
)
[3] => Array (
[0] => stdClass Object (
[cat_count] => 1
)
)
[4] => Array (
[0] => stdClass Object (
[cat_count] => 0
)
)
[5] => Array (
[0] => stdClass Object (
[cat_count] => 1
)
)
[6] => Array (
[0] => stdClass Object (
[cat_count] => 1
)
)
[7] => Array (
[0] => stdClass Object (
[cat_count] => 0
)
)
)
Since you haven't given very much information about your problem, it's not easy to give a 'correct' answer. But i think you can go with this:
x = your array
foreach(array_chunk($x, 4) AS $chunk){
foreach($chunk AS $value){
echo $value->catcount.' - ';
}
echo "<br />";
}
thx to #smartpal for the array_chunk idea

PHP array to take out array value only one field

I have an array where i want only the one Field text from all array which is text only. i want all text from there.
stdClass Object
(
[language] => en
[textAngle] => 0
[orientation] => Up
[regions] => Array
(
[0] => stdClass Object
(
[boundingBox] => 81,63,1340,1055
[lines] => Array
(
[0] => stdClass Object
(
[boundingBox] => 321,63,855,117
[words] => Array
(
[0] => stdClass Object
(
[boundingBox] => 321,63,174,94
[text] => Set
)
[1] => stdClass Object
(
[boundingBox] => 529,87,126,69
[text] => an
)
[2] => stdClass Object
(
[boundingBox] => 693,65,483,115
[text] => example.
)
)
)
[1] => stdClass Object
(
[boundingBox] => 218,182,1059,116
[words] => Array
(
[0] => stdClass Object
(
[boundingBox] => 218,182,271,92
[text] => Treat
)
[1] => stdClass Object
(
[boundingBox] => 521,203,504,95
[text] => everyOne
)
[2] => stdClass Object
(
[boundingBox] => 1054,182,223,91
[text] => With
)
)
)
I want take out from here like [text]=>Set,[text]=>an,[text]=>example.
eg set an example.
Output should be eg. only like set an example
Given your example class above, I would try something like this.
$text = '';
foreach ($class->regions[0]->lines as $line){
foreach ($line->words as $word){
$text = $text." ".$word->text;
}
}
print $text;

Having problems with recursive array objects (stdClass Object)

I'm hoping someone can help me with my syntax. I think that's the issue. I'm trying to have the loop go through display the chosen object, but it's not working. Here is my code.
stdClass Object
(
[result_count] => 2
[total_count] => 2
[next_offset] => 2
[entry_list] => Array
(
[0] => stdClass Object
(
[id] => da2b0ab5-873b-d9a8-0fcd-5180871e57eb
[module_name] => Project
[name_value_list] => stdClass Object
(
[description] => stdClass Object
(
[name] => description
[value] => soccer is fun!!!
)
[priority] => stdClass Object
(
[name] => priority
[value] => Primary
)
[total_years_played_c] => stdClass Object
(
[name] => total_years_played_c
[value] => 5
)
[sport_c] => stdClass Object
(
[name] => sport_c
[value] => Soccer
)
[high_school_exp_c] => stdClass Object
(
[name] => high_school_exp_c
[value] => 2
)
[varsity_exp_c] => stdClass Object
(
[name] => varsity_exp_c
[value] => default
)
[hand_right_c] => stdClass Object
(
[name] => hand_right_c
[value] => 0
)
[hand_left_c] => stdClass Object
(
[name] => hand_left_c
[value] => 1
)
[foot_right_c] => stdClass Object
(
[name] => foot_right_c
[value] => 1
)
[foot_left_c] => stdClass Object
(
[name] => foot_left_c
[value] => 0
)
[specialskills_c] => stdClass Object
(
[name] => specialskills_c
[value] =>
)
[varsity_starter_c] => stdClass Object
(
[name] => varsity_starter_c
[value] => 0
)
)
)
[1] => stdClass Object
(
[id] => 2e805552-c887-2b2e-a9e5-518087f3f87a
[module_name] => Project
[name_value_list] => stdClass Object
(
[description] => stdClass Object
(
[name] => description
[value] => I bleed for hockey... I will teach your kid to put them on the boards and take no prisoners!
)
[priority] => stdClass Object
(
[name] => priority
[value] => Secondary
)
[total_years_played_c] => stdClass Object
(
[name] => total_years_played_c
[value] => 7
)
[sport_c] => stdClass Object
(
[name] => sport_c
[value] => Hockey
)
[high_school_exp_c] => stdClass Object
(
[name] => high_school_exp_c
[value] => 3
)
[varsity_exp_c] => stdClass Object
(
[name] => varsity_exp_c
[value] => 2
)
[hand_right_c] => stdClass Object
(
[name] => hand_right_c
[value] => 0
)
[hand_left_c] => stdClass Object
(
[name] => hand_left_c
[value] => 1
)
[foot_right_c] => stdClass Object
(
[name] => foot_right_c
[value] => 1
)
[foot_left_c] => stdClass Object
(
[name] => foot_left_c
[value] => 0
)
[specialskills_c] => stdClass Object
(
[name] => specialskills_c
[value] =>
)
[varsity_starter_c] => stdClass Object
(
[name] => varsity_starter_c
[value] => 0
)
)
)
)
[relationship_list] => Array
(
)
)
Here is the PHP that I am using. For now, I'm just wanting to echo the name of sport_c (one should be Hockey, the other should be Soccer)
<?
foreach($response->entry_list as $sports) {
if(!empty($sports->name_value_list->sport_c)) {
$sport_list = $sports->name_value_list->sport_c;
$sport_name = $sport_list->value;
}
}
echo $sport_name;
?>
Put the echo statement within the foreach loop:
foreach($response->entry_list as $sports) {
if(!empty($sports->name_value_list->sport_c)) {
$sport_list = $sports->name_value_list->sport_c;
$sport_name = $sport_list->value;
}
echo $sport_name;
}

PHP - Objects and Arrays - How to access an stdClass object "name" "value" pair inside an array?

I would like to know how to return a value of an object with a name value pair that's inside an array. I've been trying all sorts of methods and frankly I realized I may be way over my head on this. I'd like some assistance trying to get the AirportsInformation_DataExtension value inside the property array.
stdClass Object
(
[OverallStatus] => OK
[RequestID] => 19e41b46-df68-47ba-8858-d728f3a92036
[Results] => stdClass Object
(
[PartnerKey] =>
[ObjectID] =>
[Type] => DataExtensionObject
[Properties] => stdClass Object
(
[Property] => Array
(
[0] => stdClass Object
(
[Name] => CampaignName
[Value] => 20130107_FlightDealsHotelPricePoints
)
[1] => stdClass Object
(
[Name] => StartDate
[Value] => 1/7/2013 12:00:00 AM
)
[2] => stdClass Object
(
[Name] => EndDate
[Value] => 1/15/2013 5:59:59 AM
)
[3] => stdClass Object
(
[Name] => CampaignType
[Value] => FlightDeals
)
[4] => stdClass Object
(
[Name] => LandingPage_ExpireDate
[Value] => 1/15/2013 5:59:59 AM
)
[5] => stdClass Object
(
[Name] => LandingPage_AutoRedirectOnExpire
[Value] => True
)
[6] => stdClass Object
(
[Name] => LandingPage_ExpireTargetURL
[Value] => test
)
[7] => stdClass Object
(
[Name] => BookByDate
[Value] => 1/22/2013 12:00:00 AM
)
[8] => stdClass Object
(
[Name] => TravelStartDate
[Value] =>
)
[9] => stdClass Object
(
[Name] => TravelEndDate
[Value] =>
)
[10] => stdClass Object
(
[Name] => FlightDeals_DataExtension
[Value] => 20130107_DestinationFlightDeals
)
[11] => stdClass Object
(
[Name] => FlightDeals_SortOrder_DataExtension
[Value] => FlightDeals_DestinationSortOrder
)
[12] => stdClass Object
(
[Name] => HotelDeals_DataExtension
[Value] => 20130107_FlightDealsHotelPricePoints
)
[13] => stdClass Object
(
[Name] => HotelDeals_All_DataExtension
[Value] => 20130107_HotelPackageDeals_ALL
)
[14] => stdClass Object
(
[Name] => HotelInformation_DataExtension
[Value] => EmailHotelInformation
)
[15] => stdClass Object
(
[Name] => AirportsInformation_DataExtension
[Value] => Airports
)
[16] => stdClass Object
(
[Name] => RoutesInformation_DataExtension
[Value] => Routes
)
[17] => stdClass Object
(
[Name] => DFP_DataExtension
[Value] => ET_DestinationIframeSrc
)
[18] => stdClass Object
(
[Name] => DeepLinkConnectorURL
[Value] => http://www.somewebsite/BookingConnector.html?mode=run
)
[19] => stdClass Object
(
[Name] => DefaultDestinationScenery
[Value] => LAS
)
[20] => stdClass Object
(
[Name] => DefaultHomeAirportCode
[Value] =>
)
[21] => stdClass Object
(
[Name] => FailSafeHomeAiportCode
[Value] =>
)
[22] => stdClass Object
(
[Name] => DFP_Campaign_Banner
[Value] => True
)
[23] => stdClass Object
(
[Name] => EmailID
[Value] => 44388
)
)
)
)
)
Using a foreach loop I was able to print out all lines with name/value sets
foreach ($results->Results->Properties->Property as $CurrentProp){
print('<br>');
print('Name: '.$CurrentProp->Name. ' Value: '.$CurrentProp->Value.'<br>');
};
Sadly I can't get passed that. I just need to retrieve the value. Thanks in advance.
In order to get the value, you could loop over them and test for the name matching AirportsInformation_DataExtension:
foreach ($results->Results->Properties->Property as $CurrentProp){
if($CurrentProp->Name == 'AirportsInformation_DataExtension')
{
echo 'The value is: ' . $CurrentProp->Value;
}
}
If you would need to be able to get all the values based on their name, it could be useful to turn it in to an associative array, like this
$results->Results->Properties->PropertyArray = array();
foreach($results->Results->Properties->Property as $arrCurrentProperty) {
$results->Results->Properties->PropertyArray[$arrCurrentProperty->Name] = $arrCurrentProperty->Value;
};
Then you can get the values later by indexing them directly, i.e.
echo 'The value is: ' . $results->Results->Properties->PropertyArray['AirportsInformation_DataExtension'];
Use the above solution by MrCode or, simply just use $results->Results->Properties->Property[15]->Value if the index of AirportsInformation_DataExtension is always 15. Since an array is an ordered list, it is very probably that the index does not change unless some of the items are removed/added from the array/
foreach ($results->Results->Properties->Property as $CurrentProp){
$tempArr[$CurrentProp->Name] = $CurrentProp->Value;
}
echo $tempArr['AirportsInformation_DataExtension'];
By this you can access any other key of that object.

Categories