0

While building a multi-dimensional array as shown below, I need to use a loop to retrieve some value from $this_fulfillment_record to be assigned to "amount" =>. Can a if statement be used while defining the array "[trackingInfo]" => array( If so, please show me an example.

 // Array to be built to match JSON structure.
 $fulfillment_array['orderShipment']['orderLines']['orderLine'] = [];
 foreach ($fulfillment_records as $this_fulfillment_record) {
 // Assemble the fulfillment array for Walmart.
 $fulfillment_array['orderShipment']['orderLines']['orderLine'][] = array
 [trackingInfo] => Array( 
 [shipDateTime] => 1580821866000
 [carrierName] => Array(
 
 otherCarrier] => 
 [carrier] => FedeX
 )
 [methodCode] => Standard
 [trackingNumber] => 22344
 // Can if statment be used like this?
 if ($this_fulfillment_record['carrier'] == "FedeX") {
 [trackingURL] => http://walmart/tracking/ups?&type=MP&seller_id=12345&promise_date=03/02/2020&dzip=92840&tracking_numbers=92345
 } else {
 [trackingURL] => =""
 }
 
 )
asked Jul 27, 2020 at 8:25
0

1 Answer 1

0

You can use a function/method or an array_map to define the content on an attribute. For example:

// Array to be built to match JSON structure.
$amount_values = [1, 2, 3]; // some sample amount values to interact 
$fulfillment_array['orderShipment']['orderLines']['orderLine'] = [];
foreach ($fulfillment_records as $this_fulfillment_record) {
 // Assemble the fulfillment array for Walmart.
 $fulfillment_array['orderShipment']['orderLines']['orderLine'][] = array(
 "status" => "Shipped",
 "statusQuantity" => array(
 "unitOfMeasurement" => "Each",
 // interact with some other value
 "amount" => array_map(
 function(value) {
 // do your logic/interaction here as a normal method to prepare the content to be set
 return value_to_be_set; 
 },
 $amount_values
 )
 )
 );

Or, you can use an anonymous function If you need to process an return a content that is very specific:

// Array to be built to match JSON structure.
$interactor = function($yourParameter) {
 // do your code interaction here
 return $value_to_be_returned;
};
$fulfillment_array['orderShipment']['orderLines']['orderLine'] = [];
foreach ($fulfillment_records as $this_fulfillment_record) {
 // Assemble the fulfillment array for Walmart.
 $fulfillment_array['orderShipment']['orderLines']['orderLine'][] = array(
 "status" => "Shipped",
 "statusQuantity" => array(
 "unitOfMeasurement" => "Each",
 // interact with some other value
 "amount" => $interactor($someObjectToInteract)
 )
 );
answered Jul 27, 2020 at 9:31
2
  • Marcel, I wonder if "if statement" is possible to use around the element ""amount" =>". Commented Jul 28, 2020 at 6:58
  • 1
    You cannot use around the "amount" key, but you can try to create an interaction (with array_map or anonymous function) to generate the correct array for the "statusQuantity", and then, you can generate the correct content (with or without the "amount" key) Commented Jul 28, 2020 at 9:47

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.