0

I have first array of ids:

$ids = [10,12,8];

And I have array of data:

$arr = [
        12=>[
            'content'=>'test1'
        ],
        10=>[
            'content'=>'test2'
        ],
        8=>[
            'content'=>'test3'
        ]
    ];

How can I sort array $arr by $ids values, so as result I need have this:

 $arr = [
        10=>[
            'content'=>'test2'
        ],
        12=>[
            'content'=>'test1'
        ],
        8=>[
            'content'=>'test3'
        ]
    ];
1
  • $sorted = array_replace(array_flip($ids), $arr); Commented Sep 17, 2018 at 12:03

1 Answer 1

0
<?php
$ids = [10,12,8];
$arr = [
    "12" => ['content' => 'test1'],
    "10" => ['content' => 'test2'],
    "8" => ['content' => 'test3']
];
$result = array();
foreach($ids as $key){
    if(array_key_exists($key,$arr)){
        $result[$key] = $arr[$key];
    }
}
print_r($result);
?>

Happy coding

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.