-3

I already saw old questions here on stackoverflow but i have no success.

i´m trying to use a foreach as this:

$arr = [];
$id = '10';
    foreach($ext as $e){
        if(!empty($e)){
           $only_filled[] = array_merge($arr, ['img'+($pointer+1) => $id.'_'.$e]);
        }
    }

Result i got:

array (size=2)
     0 => 
        array (size=1)
            'img1' => string '10_1.jpg' (length=8)
     1 => 
        array (size=1)
            'img3' => string '10_3.jpg' (length=8)

Result i need:

array (size=2)
  'img1' => string '10_1.jpg' (length=8),
  'img3' => string '10_3.jpg' (length=8)`
11
  • You can use good answer here stackoverflow.com/a/13921203/6850994 Commented Nov 9, 2018 at 3:22
  • 1
    Your second array is invalid because there are two keys with the same name. That can't exist in PHP, because they'd just override each other. Commented Nov 9, 2018 at 3:30
  • Thank you Paul, but i still have no success. I just tried what they explained on that question but my case is a little differente, i guess. Two reasons, 1- it´s inside a foreach and 2- the final result shouldn´t be a indexed array. Commented Nov 9, 2018 at 3:34
  • Oh man... you´re right David, i´ll test right now. Commented Nov 9, 2018 at 3:35
  • Unfortunately, it didn´t solve the problema Paul, i already correct the code example but it still has the same results Commented Nov 9, 2018 at 3:40

1 Answer 1

1

You need an associative array and in your code, you are creating a simple array

Try code like this

foreach($ext as $e){
    if(!empty($e)){
       $only_filled['img'.($pointer+1)] = 'Your Value';
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

No words to thank you @Danish Ali, seeing it now, it seems just so simple. I´m really thankful. And thanks to all community who tried help me too.
@Luiz Glad to help you :)

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.