PHP Combine Two Arrays Into One With Common ID as Text - php

I have two arrays that I want to combine:
Here is the input of my two arrays (notice Factor1 and Factor2 are different, but YearMonth is the same in both arrays):
Array
(
[0] => stdClass Object
(
[YearMonth] => 2022-01
[Factor1] => 627
)
[1] => stdClass Object
(
[YearMonth] => 2021-12
[Factor1] => 3006
)
[2] => stdClass Object
(
[YearMonth] => 2021-11
[Factor1] => 2456
)
[3] => stdClass Object
(
[YearMonth] => 2021-10
[Factor1] => 1482
)
[4] => stdClass Object
(
[YearMonth] => 2021-09
[Factor1] => 1266
)
[5] => stdClass Object
(
[YearMonth] => 2021-08
[Factor1] => 1981
)
)
Array
(
[0] => stdClass Object
(
[YearMonth] => 2022-01
[Factor2] => 380
)
[1] => stdClass Object
(
[YearMonth] => 2021-12
[Factor2] => 1773
)
[2] => stdClass Object
(
[YearMonth] => 2021-11
[Factor2] => 769
)
[3] => stdClass Object
(
[YearMonth] => 2021-10
[Factor2] => 700
)
[4] => stdClass Object
(
[YearMonth] => 2021-09
[Factor2] => 608
)
[5] => stdClass Object
(
[YearMonth] => 2021-08
[Factor2] => 859
)
)
Here is what I would like the output to be:
Array
(
[0] => stdClass Object
(
[YearMonth] => 2022-01
[Factor1] => 627
[Factor2] => 380
)
[1] => stdClass Object
(
[YearMonth] => 2021-12
[Factor1] => 3006
[Factor2] => 1773
)
[2] => stdClass Object
(
[YearMonth] => 2021-11
[Factor1] => 2456
[Factor2] => 769
)
[3] => stdClass Object
(
[YearMonth] => 2021-10
[Factor1] => 1482
[Factor2] => 700
)
[4] => stdClass Object
(
[YearMonth] => 2021-09
[Factor1] => 1266
[Factor2] => 608
)
[5] => stdClass Object
(
[YearMonth] => 2021-08
[Factor1] => 1981
[Factor2] => 859
)
)
I've tried array_merge() but it just appends the second array to the end of the first.
And when I try:
$array3 = [];
foreach (array_merge($array1, $array2) as $row) {
$array3[$row['YearMonth']] = ($array3[$row['YearMonth']] ?? []) + $row;
}
I receive this error:
<b>Fatal error</b>: Uncaught Error: Cannot use object of type stdClass as array in /var/www/html/wp-content/plugins/ad-inserter-pro/class.php(606) : eval()'d code:23
Any help you can provide is appreciated. Thank you.

Try something like this
// loop thru 1st array using & to treat value as a reference, so that we can modify values in that array
foreach ($array1 as &$value) {
// loop through 2nd array
foreach ($array2 as $value2) {
// look for matching YearMonth (use -> because they are objects)
if ($value->YearMonth === $value2->YearMonth) {
// when a match is found, copy the Factor2 value
$value->Factor2 = $value2->Factor2
// for efficiency exit the 2nd loop, because there is no point checking any more rows if we have already found the match
break;
}
}
// after exiting the 2nd loop, code will carry on here, and keep checking values in the 1st loop
}

Related

How can I convert an object to an array manually without using the json_decode($object, true) function?

For some reason, the json_decode($a) function works fine in my work, but not when it has the true parameter json_decode($a, true) that allows me to convert the object to an array. So I ask, how can I convert the next object to something like this manually.
stdClass Object
(
[23] => stdClass Object
(
[1] => stdClass Object
(
[idTempHorario] => 2306
[idEmpleado] => 23
)
[2] => stdClass Object
(
[idTempHorario] => 2307
[idEmpleado] => 23
)
)
[2123] => stdClass Object
(
[1] => stdClass Object
(
[idTempHorario] => 2292
[idEmpleado] => 2123
)
[2] => stdClass Object
(
[idTempHorario] => 2293
[idEmpleado] => 2123
)
)
)
To something like this:
Array
(
[23] => Array
(
[1] => Array
(
[idTempHorario] => 2306
[idEmpleado] => 23
)
[2] => Array
(
[idTempHorario] => 2307
[idEmpleado] => 23
)
)
[2123] => Array
(
[1] => Array
(
[idTempHorario] => 2292
[idEmpleado] => 2123
)
[2] => Array
(
[idTempHorario] => 2293
[idEmpleado] => 2123
)
)
)
I don't see why json_decode($o, true) would not work for you. However another way to do the conversion is with a recursive function like this, which casts an object and its children to arrays:
function convert($obj) {
$arr = (array)$obj;
foreach ($arr as &$v) {
if (is_object($v)) $v = convert($v);
}
return $arr;
}
Demo on 3v4l.org
Note that the demo also includes an example of json_decode producing exactly the same result...

How to Print response in csv file using PHP

I have response array in variable $listing array .Here is $listing Value:
Array
(
[0] =>
[1] => stdClass Object
(
[status] => failure
[response] => Array
(
[0] => stdClass Object
(
[ProId] => 1
[status] => failure
[errors] => Array
(
[0] => stdClass Object
(
[errorCode] => CONTENT_NOT_FOUND
[message] => Content not found.
[attributeName] =>
)
)
)
[1] => stdClass Object
(
[ProId] => 2
[status] => failure
[errors] => Array
(
[0] => stdClass Object
(
[errorCode] => CONTENT_NOT_FOUND
[message] => Content not found.
[attributeName] =>
)
)
)
[2] => stdClass Object
(
[ProId] => 3
[status] => updated
[errors] => Array
(
)
)
[3] => stdClass Object
(
[ProId] => 4
[status] => updated
[errors] => Array
(
)
)
[4] => stdClass Object
(
[ProId] => 5
[status] => updated
[errors] => Array
(
)
)
[5] => stdClass Object
(
[ProId] => 6
[status] => updated
[errors] => Array
(
)
)
[6] => stdClass Object
(
[ProId] => 7
[status] => updated
[errors] => Array
(
)
)
[7] => stdClass Object
(
[ProId] => 8
[status] => updated
[errors] => Array
(
)
)
[8] => stdClass Object
(
[ProId] => 9
[status] => updated
[errors] => Array
(
)
)
[9] => stdClass Object
(
[ProId] => 10
[status] => updated
[errors] => Array
(
)
)
)
)
[2] => stdClass Object
(
[status] => failure
[response] => Array
(
[0] => stdClass Object
(
[ProId] => 11
[status] => failure
[errors] => Array
(
[0] => stdClass Object
(
[errorCode] => CONTENT_NOT_FOUND
[message] => Content not found.
[attributeName] =>
)
)
)
[1] => stdClass Object
(
[ProId] => 12
[status] => failure
[errors] => Array
(
[0] => stdClass Object
(
[errorCode] => CONTENT_NOT_FOUND
[message] => Content not found.
[attributeName] =>
)
)
)
[2] => stdClass Object
(
[ProId] => 13
[status] => failure
[errors] => Array
(
[0] => stdClass Object
(
[errorCode] => CONTENT_NOT_FOUND
[message] => Content not found.
[attributeName] =>
)
)
)
[3] => stdClass Object
(
[ProId] => 14
[status] => updated
[errors] => Array
(
)
)
[4] => stdClass Object
(
[ProId] => 15
[status] => updated
[errors] => Array
(
)
)
[5] => stdClass Object
(
[ProId] => 16
[status] => updated
[errors] => Array
(
)
)
[6] => stdClass Object
(
[ProId] => 17
[status] => updated
[errors] => Array
(
)
)
[7] => stdClass Object
(
[ProId] => 18
[status] => updated
[errors] => Array
(
)
)
[8] => stdClass Object
(
[ProId] => 19
[status] => updated
[errors] => Array
(
)
)
[9] => stdClass Object
(
[ProId] => 20
[status] => updated
[errors] => Array
(
)
)
)
)
)
I want to create one csv file with above response in below format .Please see attached image
For achieving this result i am using this code:
$fp = fopen('output.csv',"w");
$rowcounts=0;
$resultResponse=$listing->response;
while($rowcounts<$inputfielscount) {
$result=$resultResponse[$rowcounts];
$resultProId=$result->ProId;
$resultStatus=$result->status;
$resultErrors=$result->errors;
$errorMsgArray=$resultErrors[0];
$ErrorsMessage=$errorMsgArray->message;
$ErrorCode=$errorMsgArray->errorCode;
if($resultStatus=='failure'){
$list = array ($resultProId,$resultStatus,$ErrorCode,$ErrorsMessage);
}else {
$list = array ($resultProId,$resultStatus,"successfully");
}
fputcsv($fp,$list);
$rowcounts++;
}
$listing is an response array what i already mansioned at first.
I am not getting the desired result in csv file.If anyone has solution please share.
This I think will shove it into an flat multi-diamential array, not too much trouble to then get it into a csv.
foreach($array as $key => $value) {
if(isset($array[$key]) && count($array[$key]) > 0) {
foreach($array[$key] as $inner) {
$error = $inner['errors'][0];
$new_array[] = array($inner->ProId,$inner->status,$error->errorCode,$error->message);
}
}
}

Array - How to retrieve value

Example 1:
$string = stdClass Object ( [product_id] => 25 )
$result= $string->product_id
$result will get 25
Example 2:
$string = Array ( [30] => stdClass Object ( [product_id] => 44 ))
$result= $string[30]->product_id
$result will get 44
How about this one? I want to retrieve first name.
$string = [addresses] => Array ( [shipping] =>
AddressesAddress Object (
[addressBook:AddressesAddress:private] => AddressesAddressBook Object (
[performanceHint:AddressesAddressBook:private] => 1
[uid:AddressesAddressBook:private] => 1
[addresses:AddressesAddressBook:private] => Array (
[-1] => AddressesAddress Object *RECURSION*
[-8] => AddressesAddress Object (
[addressBook:AddressesAddress:private] => AddressesAddressBook Object *RECURSION*
[schemaAddress:AddressesSchemaAddress:private] => stdClass Object (
[first_name] => Sir))))
I only know this:
$result= $string["shipping"]
and the result is
AddressesAddress Object (
[addressBook:AddressesAddress:private] => AddressesAddressBook Object (
[performanceHint:AddressesAddressBook:private] => 1
[uid:AddressesAddressBook:private] => 1
[addresses:AddressesAddressBook:private] => Array (
[-1] => AddressesAddress Object *RECURSION*
[-8] => AddressesAddress Object (
[addressBook:AddressesAddress:private] => AddressesAddressBook Object *RECURSION*
[schemaAddress:AddressesSchemaAddress:private] => stdClass Object (
[first_name] => Sir))))
I need help.

Pull data from multidimension array if a string value is defined

I want to pull the string value of [totalPrice] , [boardType] , [roomCategory] if the [hotelCode] value is known.
The array print_r is (there only one hotel,)
Array (
[0] => stdClass Object (
[processId] => H5-84752260
[hotelCode] => GRSCDS
[availabilityStatus] => InstantConfirmation
[totalPrice] => 40 [totalTax] => 0
[totalSalePrice] => 0
[currency] => EUR
[boardType] => Room and Breakfast (American Buffet Breakfast)
[rooms] => Array (
[0] => stdClass Object (
[roomCategory] => Twin Room
[paxes] => Array (
[0] => stdClass Object (
[paxType] => Adult
[age] => 30 )
[1] => stdClass Object (
[paxType] => Adult
[age] => 30 ) )
[totalRoomRate] => 40
[ratesPerNight] => Array (
[0] => stdClass Object (
[date] => 2015-03-11 [amount] => 40 ) ) ) ) ) )
You can iterate array, check item with if condition and grab values you need. Like this:
<?php
foreach ($array as $item) {
if ($item->hotelCode === 'YOUR_CODE') {
// get your data here using $item->totalPrice, etc
break;
}
}

Pull data from a PHP object

I am trying to pull data from a PHP object, but I can't get all the data which is needed.
Object $hotel1 is:
stdClass Object (
[processId] => HH-24896940
[hotelCode] => TRN367
[availabilityStatus] => InstantConfirmation
[totalPrice] => 398
[totalTax] => 0
[totalSalePrice] => 0
[currency] => EUR
[boardType] => Half Board
[rooms] => Array ( [0] => stdClass Object (
[roomCategory] => Double Standard [paxes] => Array ( [0] => stdClass Object ( [paxType] => Adult [age] => 30 )
[1] => stdClass Object (
[paxType] => Adult [age] => 30 ) )
[totalRoomRate] => 398
[ratesPerNight] => Array ( [0] => stdClass Object (
[date] => 2015-01-27
[amount] => 398 ) ) ) ) )
Code used is :
<?php
foreach ($availHotels as $hotel1) {
if ($hotel1->hotelCode === $code1) {
break;
}
}
?>
And I pull data with:
$hotel1->boardType , $hotel1->totalPrice
But when I am trying to get roomCategory is not working with
$hotel1->roomCategory
"roomCategory" is nested in "room".
Here is an example how to get what you expect:
$obj = (object) array(
'boardType' => 'foo',
'room' => array(
(object) array(
'roomCategory' => 'bar'
)
)
);
print_r($obj);
echo $obj->boardType;
echo $obj->room[0]->roomCategory;
$obj would be $hotel1 in your case.

Categories