0

I have an array which contains list of color values. Consider below array:

Array
    (
      [value] => 4
      [label] => red
    )
Array
    (
        [value] => 5
        [label] => pink
    )
Array
    (
        [value] => 6
        [label] => blue
    )
Array
    (
        [value] => 7
        [label] => yellow
    )
Array
    (
        [value] => 8
        [label] => white
    )
Array
    (
        [value] => 9
        [label] => black
    )

I want to shift/move color with value Blue at the end of the array.

Array
(
    [value] => 6
    [label] => blue
)

Should it be on last index of array? Note: The array key is unknown.

1
<?php $array = array(array
    (
        "value" => 4,
        "label" => "red",
    ),
    array
    (
        "value" => 5,
        "label" => "pink",
    ),
    array
    (
        "value" => 6,
        "label" => "blue",
    ),
    array
    (
        "value" => 7,
        "label" => "yellow",
    ),
    array
    (
        "value" => 8,
        "label" => "white",
    ),
    array
    (
        "value" => 9,
        "label" => "black",
    ),
);

foreach ($array as $key => $val) {
    if ($val["label"] == 'blue') {
        $item = $array[$key];
        unset($array[$key]);
        array_push($array, $item);
        break;
    }
}

print_r($array);

$val["label"] change it on your need

Thanks

0
$result = array_filter($array,function($item){return $item['label']!="blue";})+$array;

This will merge two arrays non-blue & blue ones. Thereby, moving blue one to the last.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.