-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)`
Luiz
  • 129
  • 9
  • You can use good answer here https://stackoverflow.com/a/13921203/6850994 – Poldo Nov 09 '18 at 03: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. – Ethan Nov 09 '18 at 03: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. – Luiz Nov 09 '18 at 03:34
  • Oh man... you´re right David, i´ll test right now. – Luiz Nov 09 '18 at 03:35
  • Unfortunately, it didn´t solve the problema Paul, i already correct the code example but it still has the same results – Luiz Nov 09 '18 at 03:40
  • `'img'+($pointer+1)` what are you wanting to do here, math? with the first `+` sign. @Luiz That looks more like a JS concatenate method or C+. – Funk Forty Niner Nov 09 '18 at 03:41
  • @Luiz Paul may not have seen your comment to him. You need to use `@membername` like I did for you. To which, I asked about something. – Funk Forty Niner Nov 09 '18 at 03:45
  • The current result was not produced with the code you have in the question because you have `'nomatter'` and in the result you have `'10_2.jpg'`. You also have invalid PHP as FunkFortyNiner mentions. Then in the desired result you have multiple entries with the same key, which is not going to work in any computing language. What would you expect to happen if you reference `$only_filled['img']`? Are you sure you don't want the keys in the desired output to be like `'img' . ($pointer+1)`? – Mike Nov 09 '18 at 03:57
  • Thank you @FunkFortyNiner, i really missed it, i´m sleepy right now, and i´m using my last forces to type a understandable english. Lol. I complemented my question for another member whose excluded his answer. Thank you everyone, tomorrow we´ll see how to solve it. Good night!! – Luiz Nov 09 '18 at 03:59
  • @Mike i just fixed it like you guys told me. I wrote 'nomatter' just for don´t get too long. All the names isn´t repeating anymore. – Luiz Nov 09 '18 at 04:01
  • @Luiz Don't confuse things any more than you have to. Put real code and real results. We can't guess what you've made up and what is real. – Mike Nov 09 '18 at 04:02

1 Answers1

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';
    }
}
Danish Ali
  • 2,354
  • 3
  • 15
  • 26