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

11
  • 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 Commented Sep 8, 2017 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. Commented Sep 8, 2017 at 13:55
  • Possible duplicate of How can I sort arrays and data in PHP?, specifically Sorting into a manual, static order Commented Sep 8, 2017 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 Commented Sep 8, 2017 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. Commented Sep 8, 2017 at 14:03

3 Answers 3

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 )
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

Comments

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.