0

I have array in my PHP, for example:

array

Array
(
    [0] => Array 
        (
           [0] => 
           [1] => 
           [2] => 
           [3] => 
           [4] => 
           [5] => Grape
           [6] => Apple 
           [7] => Pineaple
           [8] => Avocado
           [9] => Banana
        )
)

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

newArray

Array
(
    [0] => Array 
        (
           [0] => Lemon
           [1] => Lime
           [2] => Mango
           [3] => Watermelon
           [4] => Starfruit
        )
)

so I can get a result like this:

finalArray

Array
(
    [0] => Array 
        (
           [0] => Lemon
           [1] => Lime
           [2] => Mango
           [3] => Watermelon
           [4] => Starfruit
           [5] => Grape
           [6] => Apple 
           [7] => Pineaple
           [8] => Avocado
           [9] => Banana
        )
)

Any help would be appreciated. Thanks

6
  • Simply loop and check if empty and set the value. Commented Dec 29, 2016 at 7:51
  • what did you try? Commented Dec 29, 2016 at 7:52
  • @SougataBose I've loop and check, but I'm not sure which method or function to set the value. Commented Dec 29, 2016 at 7:55
  • @Naga I've tried to use array_fill and array_combine but I didn't get result as my expectation. Commented Dec 29, 2016 at 7:56
  • did you try array_merge? It may help you. Commented Dec 29, 2016 at 8:24

4 Answers 4

3

You can loop through your array, check if the index is empty and then, set it's value.

<?php
foreach($array as $index=>$value)
{
  if(empty($array[$index]))
  {
    $array[$index] = $newValue;
  }

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

1 Comment

It's work :) foreach is the best way to do that. Thanks alot.
1
<?php
$array=array("0"=>"","1"=>"","2"=>"","5"=>"Grape","6"=>"Apple","7"=>"Pineaple","8"=>"Avocado","9"=>"Banana");
echo "<pre>";print_r($array);echo"</pre>";
$newarray= array();
foreach($array as $key => $value){
    if($value == ''){
        //echo $key."<br>";
        $value = 'Somevalue';
    }
    $newarray[] = $value;
    //echo "<pre>";print_r($value);echo"</pre>";
}
echo "<pre>";print_r($newarray);echo"</pre>";
?>

Link

1 Comment

It's work! Thanks, you right, foreach is the best way to do this.
1

There is already a function in a standard library called array_replace. If the new values in the other array have the same indices you can use it:

$result = array_replace($array1, $array2);

If you just need to set up default values for empty elements use array_map:

$defaultValue = 'Foo';
$result = array_map(function ($item) use ($defaultValue) {
    return $item ?: $defaultValue;
}, $array1);

Here is working demo.

Comments

0

Use only array_merge() function For Example

 print_r( array_merge($array, $newarray) );

1 Comment

array_merge set the data into new element, it didn't set into empty element

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.