1

I'm trying to manipulate the sorting order of an array. I would like to get the matching fruit - in this case the apple - as the third index on the array.

$array_fruit[] = array('fruit' => 'apple', 'color' => 'red');  
$array_fruit[] = array('fruit' => 'banana', 'color' => 'yellow');  
$array_fruit[] = array('fruit' => 'kiwi', 'color' => 'green');  
$array_fruit[] = array('fruit' => 'orange', 'color' => 'orange'); 
$array_fruit[] = array('fruit' => 'strawberry', 'color' => 'red'); 
$array_fruit[] = array('fruit' => 'lemon', 'color' => 'yellow');  

$i = 0;  

$array_inStock = array();  

foreach($array_fruit as $fruit)  
{  
    if($fruit['fruit'] == 'apple')  
    {  
        $array_inStock['3'] = array('fruit' => $fruit['fruit'], 'color' => $fruit['color']);  
    }  
    else  
    {  
        $array_inStock[$i] = array('fruit' => $fruit['fruit'], 'color' => $fruit['color']);  
    }  
    $i++;  
}  

asort($array_inStock);

print_r($array_inStock);

I don't understand what is going wrong here. Anybody an idea? Big regards.

2
  • So what is the result of current code? Commented Apr 20, 2011 at 9:32
  • Array ( [1] => Array ( [fruit] => banana [color] => yellow ) [2] => Array ( [fruit] => kiwi [color] => green ) [3] => Array ( [fruit] => orange [color] => orange ) ) Commented Apr 20, 2011 at 9:34

4 Answers 4

1

try this

although this is a lengthy way, but works perefect no matter where apple is currently

$array_stock =array();$i=0;
foreach($array_fruit as $k=>$v)
{
if($v['fruit'] =='apple')
    {
        $array_stock[3]=$v;
        if($k>3)
            array_push($array_stock,$array_fruit[3]);
        continue;
    }
if(array_key_exists($i,$array_stock))
{
    array_push($array_stock,$v);
}
else
{
    $array_stock[$i]=$v;
    $i++;
}
}

ksort($array_stock);
echo "<pre>";
print_r($array_stock);
echo "</pre>";

DEMO

Sign up to request clarification or add additional context in comments.

2 Comments

awesome, this solved the puzzle. Big thanks to all of you who have helped
@ian if apples come always on top [0] or before [3]`` then u can remove if($k>3) condition
0

Am I misreading this or do your lines

$array_fruit[] = array('fruit' => 'apple', 'color' => 'red');  
$array_fruit[] = array('fruit' => 'banana', 'color' => 'yellow');
etc.

rewrite the contents of the array each time? Instead you want

$array_fruit[0] = array('fruit' => 'apple', 'color' => 'red');  
$array_fruit[1] = array('fruit' => 'banana', 'color' => 'yellow');
etc.

2 Comments

No, the indexing adds up as your second example automatically
not necessary to assign index...it will automatically placed. read this : php.net/manual/en/language.types.array.php
0

Try this :::


$array_fruit = array();  
$array_fruit[] = array('fruit' => 'apple', 'color' => 'red');  
$array_fruit[] = array('fruit' => 'banana', 'color' => 'yellow');  
$array_fruit[] = array('fruit' => 'kiwi', 'color' => 'green');  
$array_fruit[] = array('fruit' => 'orange', 'color' => 'orange');  

print_r($array_fruit);

$i = 0;  

$array_inStock = array();  

foreach($array_fruit as $fruit)  
{  
    if($fruit['fruit'] == 'apple')  
    {  
        $array_inStock['3'] = $fruit;  
    }  
    else  
    {  
        $array_inStock[$i] = $fruit;
        $i++;  
    }      
}  

asort($array_inStock);
print_r($array_inStock);

1 Comment

the result is: Array ( [0] => Array ( [fruit] => banana [color] => yellow ) [1] => Array ( [fruit] => kiwi [color] => green ) [4] => Array ( [fruit] => lemon [color] => yellow ) [2] => Array ( [fruit] => orange [color] => orange ) [3] => Array ( [fruit] => strawberry [color] => red ) ) so there are no apples left
0

I suppose that going through the foreach loop has this events:

  1. $i = 0, array('fruit' => 'apple', 'color' => 'red') is processed and $array_inStock['3'] = array('fruit' => 'apple', 'color' => 'red') is created
  2. $i = 1, array('fruit' => 'banana', 'color' => 'yellow') is processed and $array_inStock['1'] = array('fruit' => 'banana', 'color' => 'yellow') is created
  3. $i = 2, array('fruit' => 'kiwi', 'color' => 'green') is processed and $array_inStock['2'] = array('fruit' => 'kiwi', 'color' => 'green') is created
  4. $i = 3 !!!, array('fruit' => 'orange', 'color' => 'orange') is processed and $array_inStock['3'] = array('fruit' => 'orange', 'color' => 'orange') is created that OVERWRITES the first processed array('fruit' => 'apple', 'color' => 'red')...

What You should do instead:

foreach($array_fruit as $fruit) {  
    if($fruit['fruit'] == 'apple') {  
        $array_inStock[3] = $fruit;
    } else if($i != 3) {
        $array_inStock[$i] = $fruit;
        $i++;
    }
}

This will end up with apple being the index 3...

11 Comments

When there are more fruits the result ends up being this: Array ( [0] => Array ( [fruit] => banana [color] => yellow ) [1] => Array ( [fruit] => kiwi [color] => green ) [4] => Array ( [fruit] => lemon [color] => yellow ) [2] => Array ( [fruit] => orange [color] => orange ) [3] => Array ( [fruit] => strawberry [color] => red ) )
so there will be no apple in the result
This is great, but now apple has an index of 4 instead of 3 (I have added a few more fruit to the first array in my post)
Hmm, OK, so apple shouldn't be last, but has an index 3? Why do You need this?
The matching fruit will be echoed to the screen as the 3rd result in the first row, so when I change the matching word to look for it will always end op the 3rd result and all the other results will go down (second row)
|

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.