需要帮助将数组分配给php的数组元素吗

ztyzrc3y  于 2021-10-10  发布在  Java
关注(0)|答案(1)|浏览(554)

我创建了一个数组,用php将其转换为json文件。但是根据要求,我需要为这个数组元素分配一个数组。它是数组中的一个数组。

  1. "data" => array( // data array
  2. array(
  3. "event_name" => "Purchase",
  4. "event_time" => time(),
  5. "event_id" => $order_id,
  6. "user_data" => array(
  7. "client_ip_address" => $ip,
  8. "client_user_agent" => $browser,
  9. "em" => $email,
  10. "ph" => $phone,
  11. "fbc" =>$fbp,
  12. "fbp" =>$fbc,
  13. "fn" => $fn,
  14. "ct" => $ct,
  15. "st" => $st,
  16. "zp" => $zp,
  17. "country" => $country,
  18. "external_id" => $email
  19. ),
  20. "contents" => $items,
  21. // "id" => $item_ids,
  22. // "quantity" => count($item_qty),
  23. // // "delivery_category"=> "home_delivery",
  24. // "order_id" => $order_id
  25. // ),
  26. "custom_data" => array(
  27. "currency" => "GBP",
  28. "value" => $order_total,
  29. ),
  30. "action_source" => "website",
  31. "event_source_url" => $fullURL,
  32. ),
  33. )
  34. );

但是我的$item数组在回显时是这样的

  1. {
  2. "id": 814,
  3. "order_id": 36956,
  4. "name": "Pearson BTEC Level 7 Certificate in Strategic Management and Leadership (RQF)",
  5. "product_id": 19555,
  6. "variation_id": 0,
  7. "quantity": 2,
  8. "tax_class": "",
  9. "subtotal": "1158",
  10. "subtotal_tax": "0",
  11. "total": "1158",
  12. "total_tax": "0",
  13. "taxes": {
  14. "total": [],
  15. "subtotal": []
  16. },
  17. "meta_data": []
  18. },
  19. {
  20. "id": 815,
  21. "order_id": 36956,
  22. "name": "Pearson BTEC Level 7 Diploma in Strategic Management and Leadership (RQF)",
  23. "product_id": 19107,
  24. "variation_id": 0,
  25. "quantity": 1,
  26. "tax_class": "",
  27. "subtotal": "999",
  28. "subtotal_tax": "0",
  29. "total": "999",
  30. "total_tax": "0",
  31. "taxes": {
  32. "total": [],
  33. "subtotal": []
  34. },
  35. "meta_data": []
  36. }

当我分配这个$items并转换成json时,我只能看到数组的IDE

  1. "user_data": {
  2. "client_ip_address": "2402:d000:a200:a4a8:f06d:40a:cf85:e26d",
  3. "client_user_agent": "",
  4. "em": "b99526fedef7fe1da29e27d6f09cb64efew536978778e6cde3d8277d71a04398d1a4",
  5. "ph": "db80005a39b11372054b7708cac76gsaaww47af6ed3fe4b37b9a8b2be435e2fcff04a7",
  6. "fbc": "",
  7. "fbp": "",
  8. "fn": "83cd072a16ddb3d793c2f5bcd7d0basdree5a04af680279cf6a26fb8dffe57ce3b51a",
  9. "ct": "17a0730b80e55f4bee9569be3f7e6d0wqw500c5568ecb8d785759d7151ef40f5bbd",
  10. "st": "e3b0c44298fc1c149afbf4c8996fb9sas2427ae41e4649b934ca495991b7852b855",
  11. "zp": "ac7bb74de6884ffce919c15fef4193749sdsd4588026c643973946713ac46da540d",
  12. "country": "c820736841e811c96d30b14eb998feb6bsddc2c9d0764b7731267438dc7d6bb5b59",
  13. "external_id": "b99526fedef7fe1da29e27d6f09ssscb64536978778e6cde3d8277d71a04398d1a4"
  14. },
  15. "contents": [
  16. {
  17. "814": {},
  18. "815": {}
  19. }
  20. ],

在“centents”元素中,结果应该是这样的

  1. "contents" : [{'id':'ABC123','quantity' :2,'item_price':5.99}, {'id':'XYZ789','quantity':2, 'item_price':9.99, 'delivery_category': 'in_store'}]
zrfyljdw

zrfyljdw1#

我认为问题在于$items实际上是一个“字符串”(json)数组
在将这些$items添加到内容之前,请尝试对其执行json_decode()。
无论如何,当你问一个问题时,试着去掉所有不必要的东西,用最少的代码做一个例子,在这种情况下,你需要大量的阅读材料才能理解这个问题。

相关问题