Asked  7 Months ago    Answers:  5   Viewed   31 times

In php, I would like to have the ability to re-order an associative array by moving elements to certain positions in the array. Not necessary a sort, just a re-ordering of my choosing.

As an example, say i have an associative array as follows:

array(
 'a' => 'Element A',
 'b' => 'Element B',
 'c' => 'Element C',
);

and in one case i may want to move C before B and have the following result:

array(
 'a' => 'Element A',
 'c' => 'Element C',
 'b' => 'Element B',
);

or in another case i may want to move C before A and have the following result:

array(
 'c' => 'Element C',
 'a' => 'Element A',
 'b' => 'Element B',
);

What i am trying to show, is simply a method for saying "Hey, i want to move this array element before this other array element" or "Hey, id like to move this array element to make sure that it comes after this other array element'

Hopefully this makes sense!

Thank you to anyone in advance who cares to help me with this

 Answers

89

If you mean to swap two values you could make a function like this:

function array_swap($key1, $key2, $array) {
        $newArray = array ();
        foreach ($array as $key => $value) {
            if ($key == $key1) {
                $newArray[$key2] = $array[$key2];
            } elseif ($key == $key2) {
                $newArray[$key1] = $array[$key1];
            } else {
                $newArray[$key] = $value;
            }
        }
        return $newArray;
    }
Wednesday, March 31, 2021
 
jerrygarciuh
answered 7 Months ago
86

You should point to your vendor/autoload.php at Settings | PHP | PHPUnit when using PHPUnit via Composer.

This blog post has all the details (with pictures) to successfully configure IDE for such scenario: http://confluence.jetbrains.com/display/PhpStorm/PHPUnit+Installation+via+Composer+in+PhpStorm

Related usability ticket: http://youtrack.jetbrains.com/issue/WI-18388

P.S. The WI-18388 ticket is already fixed in v8.0

Wednesday, March 31, 2021
 
ojrac
answered 7 Months ago
45

You need a second foreach to achieve what you want.

Something like this:

foreach ($all_rows as $row) {
    if($row) {
        foreach($row as $key => $val) {
            if($key == "Food" && $val == "Apples") {
                echo "Apples: " . "<br>";
                echo $row["In_Stock"] . "<br>";
                echo $row["On_order"] . "<br>";
            }
            else if($key == "Food" && $val == "Oranges") {
                echo "Oranges: " . "<br>";
                echo $row["In_Stock"] . "<br>";
                echo $row["On_order"] . "<br>";
            }
        }
    }
}

If you just want to print the values on your page, this could do it.

But I since you've mentioned to just call the values based on food type, you can create a function out of this.

Saturday, May 29, 2021
 
DCD
answered 5 Months ago
DCD
79

On Mac OS X environment variables available in Terminal and for the normal applications can be different, check the related question for the solution how to make them similar.

Note that this solution will not work on Mountain Lion (10.8).

Saturday, May 29, 2021
 
Nate
answered 5 Months ago
27

This should work.

$dataArray = $fbfriendlikes[$data['id']]['data'];
$result = "";
foreach($dataArray as $item){
    $result .= " ".$item['name'];
}
Tuesday, July 6, 2021
 
devo
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :