0

Is it possible without using foreach to add key-value to the every array element? I need to add at the end of all arrays type => image. I have array like this:

array(5) {
  [0]=>
  array(7) {
    ["idfile"]=>
    string(2) "71"
    ["iduser"]=>
    string(1) "1"
    ["name"]=>
    string(60) "11745552_873843849371628_3215645663726390853_n-57335133dee48"
    ["extension"]=>
    string(3) "jpg"
    ["isImage"]=>
    string(1) "1"
    ["is_deleted"]=>
    string(1) "0"
    ["add_date"]=>
    string(19) "2016-05-11 15:35:15"
  }
  [1]=>
  array(7) {
    ["idfile"]=>
    string(2) "72"
    ["iduser"]=>
    string(1) "1"
    ["name"]=>
    string(19) "black-57335133e1ded"
    ["extension"]=>
    string(3) "jpg"
    ["isImage"]=>
    string(1) "1"
    ["is_deleted"]=>
    string(1) "0"
    ["add_date"]=>
    string(19) "2016-05-11 15:35:15"
  }
  [2]=>
  array(7) {
    ["idfile"]=>
    string(2) "73"
    ["iduser"]=>
    string(1) "1"
    ["name"]=>
    string(21) "Canon_1-57335133e4394"
    ["extension"]=>
    string(3) "jpg"
    ["isImage"]=>
    string(1) "1"
    ["is_deleted"]=>
    string(1) "0"
    ["add_date"]=>
    string(19) "2016-05-11 15:35:15"
  }
}
1
  • can you edit and show what you want to be the result array. also you are saying without foreach - is this exclude for loop as well? Commented May 11, 2016 at 16:01

1 Answer 1

2

You can use array_walk() and define the first argument of your callback as reference, but I assume foreach is a bit faster.

array_walk($array, function (&$value) {
    $value['type'] = 'image';
});
Sign up to request clarification or add additional context in comments.

1 Comment

what is the reaaon to avoid foreach?

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.