-8

I have array in my PHP, for example:

Array
(
    [0] => Array 
        (
           [0] => Data 1 table 1
           [1] => 
           [2] => Data 3 table 1 
           [3] => 
           [4] => 
           [5] => Data 6 table 1
           [6] => 
           [7] => Data 8 table 1
           [8] => 
           [9] => Data 10 table 1
        )
)

and I need to fill the empty element with new value from an array or $variable. Maybe for example, I get the data from another array:

Array
(
    [0] => Array 
        (
           [0] => Data 1 table 2
           [1] => Data 2 table 2
           [2] => Data 3 table 2
           [3] => Data 4 table 2
           [4] => Data 5 table 2
        )
)

so I can have a result

Array
(
    [0] => Array 
        (
           [0] => Data 1 table 1
           [1] => Data 1 table 2
           [2] => Data 3 table 1 
           [3] => Data 2 table 2
           [4] => Data 3 table 2
           [5] => Data 6 table 1
           [6] => Data 4 table 2
           [7] => Data 8 table 1
           [8] => Data 5 table 2
           [9] => Data 10 table 1
        )
)

Any help would be appreciated. Thanks

Qirel
  • 25,449
  • 7
  • 45
  • 62
Galo
  • 1
  • 3
  • can you at least try something – Kevin May 06 '19 at 09:02
  • Please go read [ask]. We expect you to show us what you have already tried. This is not a site where you just drop off your requirement and someone will make it for you. – 04FS May 06 '19 at 09:02
  • This has been answered multiple times in the StackOverflow, so searching the matter would have helped you. [array_merge](https://www.php.net/manual/en/function.array-merge.php) is what you looking for. You can see the examples/usage on the PHP page, or [here is a thread with the examples](https://stackoverflow.com/questions/16391848/merge-defaults-array-with-input-array-php-which-function)* on stackoverflow. *Examples in the thread even have recursive array if need. – Maximus Light May 06 '19 at 09:07

5 Answers5

1

All you need to do, is to loop over the first array, check each value if it contains an null value and if so, take the first element of the second array, remove its first item and insert this where the null value is.

<?php
$arr1 = [
    0 => 'Data 1 Table 1',
    1 => null,
    2 => 'Data 2 Table 1',
    3 => null,
    4 => null,
    5 => 'Data 3 Table 1',
];

$arr2 = [
    0 => 'Data 1 Table 2',
    1 => 'Data 2 Table 2',
    2 => 'Data 3 Table 2',
];

foreach ($arr1 as $index => $value) {
    if (is_null($value)) {
        $arr1[$index] = array_shift($arr2);
    }
}

print_r($arr1);
Manuel Mannhardt
  • 2,191
  • 1
  • 17
  • 23
0

i think the best way to solve that is to use a for loop and key_exists

$counter = 0;
end($array);
$lastIndex = key($array); //has to be int val
for ($i = 0; $i < $lastIndex; $i++) {
    if (!key_exists($i, $array)) {
        if (key_exists($counter, $array2)) {
            $array[$i] = $array2[$counter];
            $counter++;}
    }
}
TheBlueOne
  • 486
  • 5
  • 13
0

You can map over your array, find empty values, then shift the first value from the filter-array into that position. This assumes that there are at least the same number of elements in your filter, as is missing/empty in your primary array.

$array = array_map(function($v) use ($filter) {
    if (empty($v)) {
        $v = array_shift($filter);
    }
    return $v;
}, $array);
Qirel
  • 25,449
  • 7
  • 45
  • 62
0

You can do by shifting above two arrays

$arr  = array_shift($arr); // shifting to first index
$arr1 = array_shift($arr1);
$temp = array_filter($arr, function ($var) { // fetching empty values
    return empty($var);
});
$arr    = array_filter($arr); // now getting not empty values
$result = $arr + array_combine(array_keys($temp), $arr1); // adding array to maintain index
ksort($result); // sorting by key
echo "<pre>";
print_r($result);

Working demo.

Rahul
  • 18,271
  • 7
  • 41
  • 60
0

You actually only need a simple one liner.
This may not be the most efficient solution but it's one line ;-)

First array_intersect the null values and grab the keys from the intersect.
Use the keys and combine them with arr2.
This will give you:

$arr2 = [
    1 => 'Data 1 Table 2',
    3 => 'Data 2 Table 2',
    4 => 'Data 3 Table 2',
];

Now replace this in the $arr1 and you got your end result.

$result = array_replace($arr1,array_combine(array_keys(array_intersect($arr1, [null])), $arr2));
var_dump($result);

output:

array(6) {
  [0]=>
  string(14) "Data 1 Table 1"
  [1]=>
  string(14) "Data 1 Table 2"
  [2]=>
  string(14) "Data 2 Table 1"
  [3]=>
  string(14) "Data 2 Table 2"
  [4]=>
  string(14) "Data 3 Table 2"
  [5]=>
  string(14) "Data 3 Table 1"
}

https://3v4l.org/iZm32

Andreas
  • 23,610
  • 6
  • 30
  • 62