I have tried to set "Other" value at last of array without using any looping. The array looks like:
array
(
[15] => Business Copywriting & Press Releases
[11] => Business Plans
[20] => Languages
[17] => Legal Writing
[19] => **Other**
[10] => Presentation Design
[13] => Proof reading & Editing
[16] => RESEARCH & SUMMARIES
[12] => RESUMES & COVER LETTERS
[18] => Transcriptions
[14] => Translating
)
from what i'm understand, you want to move **Other**
to the end of an array.
Maybe this can help.
$key = array_search ('**Other**', $arr);
unset($arr[$key]); // remove **other**
array_push($arr, "**Others**"); // put **Other** in the end of array
but if you want to keep your array key, you cannot using array_push()
but this will do;
$key = array_search ('**Other**', $arr);
unset($arr[$key]); // remove **other**
$arr[$key] = '**Other**';
**Other**
value (value of the array at index 19)I don't understand your question. What I understood is that you want to concate the value of the array on the index 19 to the end of the array. If I am right, you may want to do so:
<?php
$arr = [
15 => 'Business Copywriting & Press Releases',
11 => 'Business Plans',
20 => 'Languages',
17 => 'Legal Writing',
19 => '**Other**',
10 => 'Presentation Design',
13 => 'Proof reading & Editing',
16 => 'RESEARCH & SUMMARIES',
12 => 'RESUMES & COVER LETTERS',
18 => 'Transcriptions',
14 => 'Translating',
];
$arr[] = $arr[19];
var_dump($arr);
// array (size=12)
// 15 => string 'Business Copywriting & Press Releases' (length=37)
// 11 => string 'Business Plans' (length=14)
// 20 => string 'Languages' (length=9)
// 17 => string 'Legal Writing' (length=13)
// 19 => string '**Other**' (length=9)
// 10 => string 'Presentation Design' (length=19)
// 13 => string 'Proof reading & Editing' (length=23)
// 16 => string 'RESEARCH & SUMMARIES' (length=20)
// 12 => string 'RESUMES & COVER LETTERS' (length=23)
// 18 => string 'Transcriptions' (length=14)
// 14 => string 'Translating' (length=11)
// 21 => string '**Other**' (length=9)
**Other**
after searching (variable indexes)If the index is variable:
<?php
$arr = [...];
$key = array_search('**Other**', $arr);
$arr[] = $arr[$key];
var_dump($arr);
// array (size=12)
// ...
// 21 => string '**Other**' (length=9)
You may want to remove $arr[$key]
after concate:
<?php
$arr = [...];
$key = array_search('**Other**', $arr);
$arr[] = $arr[$key];
unset($arr[$key]);
var_dump($arr);
// array (size=11)
// 15 => string 'Business Copywriting & Press Releases' (length=37)
// 11 => string 'Business Plans' (length=14)
// 20 => string 'Languages' (length=9)
// 17 => string 'Legal Writing' (length=13)
// 10 => string 'Presentation Design' (length=19)
// 13 => string 'Proof reading & Editing' (length=23)
// 16 => string 'RESEARCH & SUMMARIES' (length=20)
// 12 => string 'RESUMES & COVER LETTERS' (length=23)
// 18 => string 'Transcriptions' (length=14)
// 14 => string 'Translating' (length=11)
// 21 => string '**Other**' (length=9)
notice: the initial
**Other**
is no longer existing
Other
Otherwise, if you want to concate the string Other
:
<?php
$arr = [...];
$arr[] = 'Other';
var_dump($arr);
// array (size=12)
// ...
// 21 => string 'Other' (length=5)
Suppose you have an array -
$array = array("one", "two", "three");
Now you want to add a new element i.e. Others in the array without using the loop, then you can use array_push function to achieve your desired output -
array_push($array, "others");
Try this code,
$arr = [
15 => 'Business Copywriting & Press Releases',
11 => 'Business Plans',
20 => 'Languages',
17 => 'Legal Writing',
19 => '**Other**',
10 => 'Presentation Design',
13 => 'Proof reading & Editing',
16 => 'RESEARCH & SUMMARIES',
12 => 'RESUMES & COVER LETTERS',
18 => 'Transcriptions',
14 => 'Translating',
];
$key = array_search('**Other**', $arr); // $key = 19;
unset($arr[$key]);
array_push($arr, "Other");
print_r($arr);