1

I have an array $myArray

Array ( [0] => Apple [1] => Orange [2] => Grape [3] => Plum )

This array is being dynamically pulled, but i need the array content to be in a certain order. for example Grape will always be first, plum will always be second, apple will always be third and orange will need to be last

Array ( [0] => Grape [1] => Plum [2] => Apple [3] => Orange )

And lets say when this is being dynamically made and there are no "Grapes" plum needs to become first then apple, orange. ex

Array ( [0] => Plum [1] => Apple [2] => Orange )

I am not aware how this can be done in php

jumpman8947
  • 531
  • 5
  • 24
  • 1
    If this is a custom sorting logic, there is no choice but to implement it with custom code. Otherwise please explain better what you want to achieve, maybe with a more realistic example – Calimero Sep 08 '17 at 13:53
  • If you want such a specific order, you have to know what values there will be. Jus make an array with the order of values, loop your result array and compare with the order-array. – Michel Sep 08 '17 at 13:55
  • Possible duplicate of [How can I sort arrays and data in PHP?](https://stackoverflow.com/questions/17364127/how-can-i-sort-arrays-and-data-in-php), specifically *Sorting into a manual, static order* – iainn Sep 08 '17 at 13:56
  • @Calimero i want my array to always be in the order i specified above. and if there isn't one element, it will keep the same order minus the missing element – jumpman8947 Sep 08 '17 at 14:01
  • @jumpman8947 if this is the only way you can explain it, then go ahead and translate that to php code. Litteraly. it will be ugly, but it will do the job. – Calimero Sep 08 '17 at 14:03
  • @Calimero it is explained above. The first array is what i currently have. The second array is the order i always want them to be in. The third array is what i want if an element is not present. – jumpman8947 Sep 08 '17 at 14:06
  • @jumpman9847 Great. Time to do it now. If you can explain it, you can do it as well. – Calimero Sep 08 '17 at 14:17
  • @Calimero do you have any suggestions/ php methods on how to start? – jumpman8947 Sep 08 '17 at 14:18
  • @jumpman8947 actually it's not unlike explaining it, at all. Start with a new blank array and set the key/value pairs in the order you want them to be. There's nothing more to it than that. The code will certainly be specific and ugly, but you will get the array you want in the end. – Calimero Sep 08 '17 at 14:20
  • @Like i said above the data is being pulled dynamically, and it will always be at most those 4 fields. Where are you going with making a new array, will i need to copy elements over? – jumpman8947 Sep 08 '17 at 14:32
  • I have two questions for you: Are you ever gonna work with those 4 elements and nothing more? and if not, what should happen to other elements? – William Perron Sep 08 '17 at 18:30

3 Answers3

2
$array = array('Apple', 'Orange','Grape','Plum' );
 // order of array
$order = array('Grape', 'Plum', 'Apple','Orange');
 // testing no 'Grapes'
$array2_missing = array('Apple', 'Orange','','Plum' ); 


$result = array_intersect($order, $array);
print_r($result);

Output: Array ( [0] => Grape [1] => Plum [2] => Apple [3] => Orange )
Output2 : Array ( [1] => Plum [2] => Apple [3] => Orange )
mwweb
  • 5,751
  • 4
  • 15
  • 21
0

Are you simply looking for this? http://php.net/array_shift

Otherwise, if you want to set a specific index, just do it like this:

$myArray = [];
$myArray[2] = new Plum();

This would set index 2 (3rd element) of your array.

Daniel Bunte
  • 132
  • 2
0

Lets say your array is

$array = array();

and the value you are putting into the array is $fruits and $fruits changes. just do

array_push($array,$fruit)

if $furits came as

$fruits = "Grape";
$fruits = "Plum";
$fruits = "Apple";
$fruits = "Orange";

if you do array_push with a loop

you get

$array[0] = "Grape";
$array[1] = "Plum";
$array[2] = "Apple";
$array[3] = "Orange";

but if you user array_push in a loop with $fruits coming as

    $fruits = "Plum";
    $fruits = "Apple";
    $fruits = "Orange";

You get

  $array[0] = "Plum";
    $array[1] = "Apple";
    $array[2] = "Orange";

I guess thats what you are trying to do

UniQue
  • 96
  • 8