0

I have an associative array with indexed sub-arrays each containing associative arrays which contain content and an index. So like this (in PHP):

$assoc_arr = 
array("second" => array(
                       array("position" => 4,
                             "content" => "Valiant"),
                       array("position" => 5,
                             "content" => "Hail")
                      ),
      "first" =>  array(
                       array("position" => 0,
                             "content" => "Hail"),
                       array("position" => 3,
                             "content" => "Victors"),
                       array("position" => 2,
                             "content" => "the"),
                       array("position" => 1,
                             "content" => "to")
                      )
);

I want to put all of those into an indexed array where their index is their "position" in the associative array. So final array should be:

Array ( [0] => Hail [1] => to [2] => the [3] => Victors [4] => Valiant [5] => Hail ) 

Currently, I'm merging all of the arrays inside the highest level array, then sorting it by each of those sub-array's positions, then creating an indexed array by pushing the content in order onto a new array. As so:

$pos_arr = array_merge($assoc_arr["second"], $assoc_arr["first"]);
usort($pos_arr, function($a, $b) {
  return $a["position"] >= $b["position"] ? 1 : -1;
});

$indexed_arr = array();

foreach ($pos_arr as $elem) {
    array_push($indexed_arr, $elem["content"]);
}

It seems like there must be a better way to do this! Can anyone think of one?

The data is coming from a poorly structured XML document which I can't change.

2 Answers 2

1
$idxar=array();
foreach ($assoc_arr as $subar)
  foreach ($subar as $item)
    $idxar[$item['position']+0]=$item['content'];

will work, if your input data is flawless.

Edit

If you need the keys not only to be numerically correct, but also in the correct order, you must suffix this with ksort($idxar)

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

Comments

1

here is another solution:

<?php

$assoc_arr = 
    array("second" => array(
        array("position" => 4,
              "content" => "Valiant"),
        array("position" => 5,
              "content" => "Hail")
        ),
    "first" =>  array(
        array("position" => 0,
              "content" => "Hail"),
        array("position" => 3,
              "content" => "Victors"),
        array("position" => 2,
              "content" => "the"),
        array("position" => 1,
              "content" => "to")
      )
);

$order = array('first','second'); // this helps you create your own sort. E.g. position1, position2, etc
$arr = array();

foreach($order as $ord) {
    if(isset($assoc_arr[$ord])) {
       $arr = array_merge($arr, $assoc_arr[$ord]);
    }
}

$finalArray = array();

foreach($arr as $a) {
    $finalArray[$a['position']] = $a['content'];
}

ksort($finalArray);

print_r($finalArray);

and a fiddle that works for you here

Comments

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.