0

This is the return result of my two variables:

$resultPayment = [
    [72875500, 8187000],
    [14343],
    [44419200,
    ["12332000"],
    [28700250]
  ]
$resultId = [
    ["461", "462"],
    [6462],
    [8771],
    [8461],
    [5461]
]

This is the code for get the data:

for ($iii=0; $iii<count($dataInvoice); $iii++) {
      $resultId[] = unserialize($dataInvoice[$iii]->invoiceid);
      $resultPayment[] = unserialize($dataInvoice[$iii]->jumlahbayarid);
}

These two pieces of data are the same length and array structure, and I want to combine $id and $payment and create an object. Here are the results I expect :

[
    { "461": 72875500 },
    { "462": 8187000 },
    { 6462: 14343 },
    { 8771: 44419200 },
    { 8461: "12332000" },
    { 5461: 28700250 }
]
3
  • Have a look at array_combine. Commented Apr 23, 2021 at 6:36
  • Do you want to keep the values as text and integers ("461", "462", 6462, ...)? Commented Apr 23, 2021 at 6:45
  • No Mr. @Zhorov , i want to change to number Commented Apr 23, 2021 at 6:48

1 Answer 1

2

One possible approach is the following example:

<?php

$payment = [
    [72875500, 8187000],
    [14343],
    [44419200],
    ["12332000"],
    [28700250]
];
$id = [
    ["461", "462"],
    [6462],
    [8771],
    [8461],
    [5461]
];

$payment = call_user_func_array('array_merge', $payment);
$id      = call_user_func_array('array_merge', $id);
$result  = json_encode(array(array_combine($id, $payment)));

echo $result;
?>

Result:

[{"461":72875500,"462":8187000,"6462":14343,"8771":44419200,"8461":"12332000","5461":28700250}]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.