1

The array below should have a child array for every "point" value from 1 - 7, but 2, 4 and 6 are missing. How can I insert an array for each of those missing values, with a "value" of NULL?

I tried a for($i = 1; $i <= 7; $i++) and inside foreach'ing over my array to see if 'point' == $i or not but that doesn't work.

This is already in a nested foreach of a fairly large array so performance is somewhat important.

Code so far (incomplete and obviously not working)

foreach($measurements as $measurement_id => $set)
        {
            for($i = 1; $i <= 7; $i++)
            {
                foreach($set as $key => $value)
                {
                    $fill = array('value' => null);

                    if($value['point'] == $i)
                        $output[$measurement_id][$key] = $value;
                    else
                        $output[$measurement_id][$key] = $fill;
                }
            }
        }

The array:

            [0] => Array
                (
                    [point] => 1
                    [value] => 1.0
                )

            [1] => Array
                (
                    [point] => 3
                    [value] => 2.0
                )

            [2] => Array
                (
                    [point] => 5
                    [value] => 3.0
                )

            [3] => Array
                (
                    [point] => 7
                    [value] => 4.0
                )

The result should be

             [0] => Array
                (
                    [point] => 1
                    [value] => 1.0
                )

            [1] => Array
                (
                    [point] => 2
                    [value] => NULL
                )

            [2] => Array
                (
                    [point] => 3
                    [value] => 2.0
                )

            [3] => Array
                (
                    [point] => 4
                    [value] => NULL
                )
            [4] => Array
                (
                    [point] => 5
                    [value] => 3.0
                )

            [5] => Array
                (
                    [point] => 6
                    [value] => NULL
                )

            [6] => Array
                (
                    [point] => 7
                    [value] => 4.0
3
  • 2
    what code do you have so far? Commented Dec 7, 2012 at 17:02
  • 1
    You really want to do this as part of the code for creating the initial array. Where are you getting the data from initially? Commented Dec 7, 2012 at 17:03
  • Data is coming from a DB. Added code. Commented Dec 7, 2012 at 17:06

2 Answers 2

1

Try this:

$oldArr = [ ..your incomplete array.. ];
$newArr = array();

for ($i = 1; $i < 8; ++$i) {
  $new = array(
    'point' => $i,
    'value' => NULL,
  );

  foreach($oldArr as $old) {
    if ($old['point'] == $i) {
      $new['value'] = $old['value'];
    }
  }

  $newArr[] = $new;
}

now $newArr should contain all the values as you want them.

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

Comments

1

If you original array is ordered by point as in your example, you can do it in one loop only:

$result = array();
for ($i = 1; $i <= 7; $i++)
{
  // get the first element of your set
  $current = reset($set);
  if ($current['point'] === $i)
  {
    // remove the first element of your set and assign it to the result array
    $result[$i - 1] = array_shift($set);
  }
  else
  {
    // create a new, empty entry
    $result[$i - 1] = array('point' => 1, 'value' => NULL);
  }
}

Note that I am creating a new array and removing values from your original one. If you really are worried about resources / performance, you could use the same principle and loop backwards (from 7 to 1), moving the last element of your array to the correct position and filling the empty spaces the same way.

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.