0

I have an array data which each array obj has different ordering

$array = array(
    type1 => array(
        'id' => 'w12',
        'name' => 'John Doe',
        'email' => '[email protected]',
        'fname' => 'john',
        'phone' => '111',
        'age' => '22'
    ),
    type2 => array(
        'id' => 'w13',
        'name' => 'Jane Doe',
        'email' => '[email protected]',
        'age' => '22',
        'phone' => '111',
        'fname' => 'dsd'
    ),
);

I want to order them according to below order of below array key order

$array2 = [
    'fname' => 'fname',
    'phone' => 'phone111',
    'age' => 'age11',
    'email' => 'email11'
];

id and name will always in right order i want to set rest according to $array2 ordering. Please advice me how to proceed?

UPDATE

object(SimpleXMLElement)#179 (2) {
  ["type1"]=>
  object(SimpleXMLElement)#110 (3) {
    ["id"]=>
    string(3) "333"
    ["name"]=>
    string(7) "#c32c2c"
    ["email"]=>
    object(SimpleXMLElement)#172 (0) {
    }
  }
  ["type2"]=>
  object(SimpleXMLElement)#64 (3) {
    ["id"]=>
    string(4) "w2we"
    ["phone"]=>
    string(7) "#98bb3e"
    ["name"]=>
    object(SimpleXMLElement)#172 (0) {
    }
  }
}
1
  • Cosmetic surgery or is there a point to this? Commented Oct 30, 2018 at 17:46

1 Answer 1

1

Here's one way to do it.

First create a template array that defines the order you want (including the id and name keys, even though they're in the right order already.)

$order = [
    'id' => null,
    'name' => null,
    'fname' => null,
    'phone' => null,
    'age' => null,
    'email' => null
];

Then merge each row in the array onto the template. The empty values of the template will be replaced with the values from the array row, but the keys will keep the order from the template.

foreach ($array as $key => $row) {
    $ordered[$key] = array_merge($order, $row);
}

I showed how to create a new array here with the reordered rows. If you want, you could just replace the original value by assigning the merged result to $array[$key] instead.

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

4 Comments

according to my case $array should be my exisitng array and $order will be the ordering template right? btw im not getting correct out put.
Yeah, that's right. Not sure why it wouldn't work. Here's a working demo you can compare it to: 3v4l.org/gSfTP
I've update my question with adding the var_dump of my exisiting array. Can you check and let me know is there any thing where you identified of no coming the results.
Well, hmm, that isn't an array, so it's quite a bit different than what the question asked originally. Can you edit the question to show an example of how you're getting the SimpleXMLElement, and give an idea of what you're trying to do with it that you need to change the order? I may still be able to help.

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.