1

I have two array that in json something like this (label) :

[
    {
      "dates": "2019-07-01",
      "c_job": 0
    },
    {
      "dates": "2019-07-02",
      "c_job": 0
    },
    {
      "dates": "2019-07-03",
      "c_job": 0
    },
    {
      "dates": "2019-07-04",
      "c_job": 0
    }
  ]

and the others data from database something like this (data) :

{
    "EXPORT": [
      {
        "dates": "2019-07-01",
        "c_job": 12
      },
      {
        "dates": "2019-07-02",
        "c_job": 8
      },
      {
        "dates": "2019-07-04",
        "c_job": 11
      }
    ],
    "IMPORT": [
      {
        "dates": "2019-07-03",
        "c_job": 11
      }
    ]
}

after that i try array replace to make data precision with label with this code :

foreach($data as $key => $value) {
            $datafinal[$key] = array_replace($label,$value);
        }

and the output is (datafinal) :

{
    "EXPORT": [
      {
        "dates": "2019-07-01",
        "c_job": 12
      },
      {
        "dates": "2019-07-02",
        "c_job": 8
      },
      {
        "dates": "2019-07-04",
        "c_job": 11
      },
      {
        "dates": "2019-07-04",
        "c_job": 0
      }
    ],
    "IMPORT": [
      {
        "dates": "2019-07-03",
        "c_job": 11
      },
      {
        "dates": "2019-07-02",
        "c_job": 0
      },
      {
        "dates": "2019-07-03",
        "c_job": 0
      },
      {
        "dates": "2019-07-04",
        "c_job": 0
      }
    ]
  }

what i want is something like this :

{
    "EXPORT": [
      {
        "dates": "2019-07-01",
        "c_job": 12
      },
      {
        "dates": "2019-07-02",
        "c_job": 8
      },
      {
        "dates": "2019-07-03",
        "c_job": 0
      },
      {
        "dates": "2019-07-04",
        "c_job": 11
      }
    ],
    "IMPORT": [
      {
        "dates": "2019-07-01",
        "c_job": 0
      },
      {
        "dates": "2019-07-02",
        "c_job": 0
      },
      {
        "dates": "2019-07-03",
        "c_job": 11
      },
      {
        "dates": "2019-07-04",
        "c_job": 0
      }
    ]
  }

i have try array reduce and replace

1
  • “[I] have [tried] array reduce and replace” — please edit your question and share your code. Commented Aug 25, 2019 at 6:03

2 Answers 2

1

Basically what you need to do is for each top-level array in $data, go through the 0 value array (I've called it $blank) and see if the date exists in the current $data array. If it does, copy that value, otherwise use the blank value:

$datafinal = array();
foreach ($data as $key => $value) {
    foreach ($blank as $bkey => $bvalue) {
        if (($dkey = array_search($bvalue['dates'], array_column($value, 'dates'))) !== false) {
            $datafinal[$key][$bkey] = $value[$dkey];
        }
        else {
            $datafinal[$key][$bkey] = $bvalue;
        }
    }
}
echo json_encode($datafinal, JSON_PRETTY_PRINT);

Output:

{
    "EXPORT": [
        {
            "dates": "2019-07-01",
            "c_job": 12
        },
        {
            "dates": "2019-07-02",
            "c_job": 8
        },
        {
            "dates": "2019-07-03",
            "c_job": 0
        },
        {
            "dates": "2019-07-04",
            "c_job": 11
        }
    ],
    "IMPORT": [
        {
            "dates": "2019-07-01",
            "c_job": 0
        },
        {
            "dates": "2019-07-02",
            "c_job": 0
        },
        {
            "dates": "2019-07-03",
            "c_job": 11
        },
        {
            "dates": "2019-07-04",
            "c_job": 0
        }
    ]
}

Demo on 3v4l.org

Sign up to request clarification or add additional context in comments.

Comments

0

I've gone for the old and simple nested foreach loops, about as simple and quick as you can get (comments in code)...

$template = json_decode('[
    {
      "dates": "2019-07-01",
      "c_job": 0
    },
    {
      "dates": "2019-07-02",
      "c_job": 0
    },
    {
      "dates": "2019-07-03",
      "c_job": 0
    },
    {
      "dates": "2019-07-04",
      "c_job": 0
    }
  ]', true);

$update = json_decode('{
    "EXPORT": [
      {
        "dates": "2019-07-01",
        "c_job": 12
      },
      {
        "dates": "2019-07-02",
        "c_job": 8
      },
      {
        "dates": "2019-07-04",
        "c_job": 11
      }
    ],
    "IMPORT": [
      {
        "dates": "2019-07-03",
        "c_job": 11
      }
    ]
}', true);

And the main code...

// Create blank output array with all entries
$output = ["EXPORT" => $template, "IMPORT" => $template];

// Loop over update data (from database) - you may need to tweak this for your use case
foreach ( $update  as $type => $updateItem )   {
    // Loop over each set of update values (a row of dates and c_job)
    foreach ( $updateItem  as $updateItem )   {
        // Locate in empty output array
        foreach ( $output[$type] as &$item )   {
            // Same date - update
            if ( $updateItem['dates'] == $item['dates']) {
                $item['c_job'] = $updateItem['c_job'];
                // Stop looking as already updated
                break;
            }
        }
    }
}

A shorter, but involves a lot more array_ functions, this creates the template arrays with the date as the key, so then you can use the dates from the input data to directly update the data...

// Create template array using dates as the array key
$template = array_column($template, null, 'dates');
$output = ["EXPORT" => $template, "IMPORT" => $template];

foreach ( $update  as $type => $export )   {
    foreach ( $export  as $export )   {
        // Use the combination of type (EXPORT) and dates from the update data
        // to directly update the output (assumes empty value created)
        $output[$type][$export['dates']]['c_job'] = $export['c_job'];
    }
}

// Re-index data to remove dates as keys
$output = ["EXPORT" => array_values($output['EXPORT']), 
    "IMPORT" => array_values($output['IMPORT'])];

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.