Hello,
I have API results that look something like that:
Illuminate\Support\Collection {#1282
#items: array:35 [
"id" => "05c327f4-5sdb-498c-b641-337ecf66ed57"
"type" => 320
"number" => 60013
"documentDate" => "2021-03-11"
"creationDate" => 1615482203
"amountDueVat" => 170
"amountExemptVat" => 0
"vat" => 28.9
"amount" => 198.9
"amountOpened" => 0
"client" => array:12 [
"id" => "55d63e90-cf7e-4bb8-9b1f-7400as6094f"
"name" => "Jow D"
"department" => ""
"address" => "Address"
"city" => "City"
"zip" => "1234567"
"country" => "IL"
"phone" => "7237918237"
"fax" => ""
"mobile" => ""
"emails" => array:1 [
0 => "[email protected]"
]
"self" => false
],
"payment" => array:1 [
0 => array:18 [
"id" => "af5f80d8-e882-4668-bc11-df75958a9323e"
"date" => "2021-03-11"
"type" => 3
"status" => 1
"price" => 198.9
"currency" => "ILS"
"currencyRate" => 1
"paymentStatus" => 0
"ref" => []
"cancellable" => false
"cardType" => 2
"cardNum" => "0000"
"dealType" => 1
"numPayments" => 1
"payment" => 1
"name" => "כרטיס אשראי"
"description" => "ויזה 0000 / רגיל"
"amount" => 198.9
]
}
And I would like to map over this collection and convert let's say
"type" => 320
to
"type" => 'Some String'
Based on an array of conversation:
return [
'320' => 'Some String'
];
And with one level from the results, I can accomplish that, but then if an item from the map function is an array, it can get a bit tricky. Any suggestion on how to go about it?
$results->map(function($item, $key) use ($array){
foreach($array as $typeKey => $typeValue){
return $typeKey === $item ? $typeValue : $item;
}
})->toArray();
I was thinking of deligating to a separate function to check for each item from the map function , and then check is that item is an array, in that case, call the same function again and collapse the array to one dimension.