-1

I have two different arrays

$dates =  [
  0 => "2019-01-17",
  1 => "2019-01-16",
  2 => "2019-01-15",
  3 => "2019-01-14",
  4 => "2019-01-13",
  5 => "2019-01-12",
  6 => "2019-01-11",
  7 => "2019-01-10"
]

and below is dynamic which can different but let see an example

$Fresh_Record =  [
    "date" => array:2 [
        0 => "2019-01-10"
        1 => "2019-01-14"
    ]
    "counter" => array:2 [
        0 => 1000.0
        1 => 500.0
    ]
]

As you can see above array which has date and counter. As you know the counter 1000.0 is for 2019-01-10 and 500 for 2019-01-14 .

These are only records which are available but I need last 7 days records. So I want to add 0 if no record available for any date.

I am trying and tried a lot to achieve but still failed to achieve it.

First Attempt

$ARR_1 = array();

        foreach($result as $AA){
            $data['counter'][] = 0;
        }

        $MERGE = array_merge($data['counter'],$yAxis_ARR);

        $MAIN = [
            'date' => $d,
            'counter' => $MERGE 
        ];

Other attempts

$index = 0;
foreach($d as $single){

    if(!in_array($single,$Fresh_Record['date'])){
        if(count($Fresh_Record['date']) >= $index){
            $map_array['date'] = $Fresh_Record['date'][$index];
            $map_array['counter'] = 0;
        }
    }

    $index++;
}

Help

I really need guidance to resolve it. Kindly guide me please. Thank you so much.

I want to make is like below

$map_array =  [
        "date" => [
          0 => "2019-01-17",
          1 => "2019-01-16",
          2 => "2019-01-15",
          3 => "2019-01-14",
          4 => "2019-01-13",
          5 => "2019-01-12",
          6 => "2019-01-11""
        ]
        "counter" => [
            0 => 1000.0,
            1 => 500.0,
            2 => 0,
            3 => 0,
            4 => 0,
            5 => 0,
            6 => 0
        ]
    ]
Script Lover
  • 331
  • 2
  • 6
  • 15

2 Answers2

2

You can use array_search to check if the index exists for date and use that same index for the counter in Fresh_Record:

<?php
$dates =  [
  0 => "2019-01-17",
  1 => "2019-01-16",
  2 => "2019-01-15",
  3 => "2019-01-14",
  4 => "2019-01-13",
  5 => "2019-01-12",
  6 => "2019-01-11",
  7 => "2019-01-10"
];

$Fresh_Record =  [
    "date" =>  [
        0 => "2019-01-10",
        1 => "2019-01-14"
    ],
    "counter" =>  [
        0 => 1000.0,
        1 => 500.0
    ]
];

foreach($dates as $date){
    $map_array['date'][] = $date;
    $index = array_search($date, $Fresh_Record['date']);
    if($index !== false){
        $map_array['counter'][] = $Fresh_Record['counter'][$index];
    } else {
        $map_array['counter'][] = 0;
    }
}

print_r($map_array);

Will output:

Array
(
    [date] => Array
        (
            [0] => 2019-01-17
            [1] => 2019-01-16
            [2] => 2019-01-15
            [3] => 2019-01-14
            [4] => 2019-01-13
            [5] => 2019-01-12
            [6] => 2019-01-11
            [7] => 2019-01-10
        )

    [counter] => Array
        (
            [0] => 0
            [1] => 0
            [2] => 0
            [3] => 500
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 1000
        )

)
Laurens
  • 2,596
  • 11
  • 21
0

Below snippet should work

<?php
$dates =  [
    0 => "2019-01-17",
    1 => "2019-01-16",
    2 => "2019-01-15",
    3 => "2019-01-14",
    4 => "2019-01-13",
    5 => "2019-01-12",
    6 => "2019-01-11",
    7 => "2019-01-10"
];

$Fresh_Record =  [
    "date" => [
        0 => "2019-01-10",
        1 => "2019-01-14"
    ],
    "counter" => [
        0 => 1000.0,
        1 => 500.0
    ]
];

$arrayMap = [
    'date' => [],
    'counter' => []
];

foreach($dates as $index => $date){
    $count = 0;
    $foundIndex = array_search($date, $Fresh_Record['date']);

    if ($foundIndex !== false) {
        $count = $Fresh_Record['counter'][$foundIndex];
    }
    $arrayMap['date'][$index] = $date;
    $arrayMap['counter'][$index] = $count;
}

var_dump($arrayMap);
Duy Nguyen
  • 985
  • 5
  • 9
  • 2
    No need to both use in_array and array_search. With array_search you can also check if the value exists. – Laurens Jan 17 '19 at 11:22