0

I have an array with these entries:

$row['10']['something'] = "21";
$row['5']['something'] = "42";

I want to make a string from index 0 to 10. But only index "5" and "10" are set. However I want a string like this:

0, 0, 0, 0, 42, 0, 0, 0, 0, 21 

Currently I am using this:

for($i=0; $i <= 10; $i++) {
        if (!array_key_exists($i, $row))
            $row[$i]['something'] = 0;
        if ($i == 10)
            $string .= $row['10']['something'];
        else
            $string .= $row[$i]['something'].', ';
    }

But I need it more efficient. I could do array_map and implode but I don't know how to add the 0, 0, 0,... I would only get 21, 42

To clarify:

I can't overwrite single indexes the array called $row in this example is filled from a sql query. It will completely overwrite an existing array.

yoshi
  • 1,287
  • 3
  • 15
  • 28

4 Answers4

1

Sounds like you want array_fill() and array_merge()

http://us1.php.net//manual/en/function.array-fill.php

http://www.php.net//manual/en/function.array-merge.php

  • Create a new array
  • Use array_fill() to fill it with 0's
  • Overwrite the indexes in the new array with the values from the old ones in the appropriate indexes

$newarray = array_fill(0, 23, 0);
$newarray = array_merge($row, $newarray);

rroberts
  • 74
  • 3
  • Why's that? I can go back and fill out the answer more, if that's what ettiquette demands - I don't post here much, so my apologies if I've misunderstood proper ettiquette – rroberts Jun 10 '14 at 20:45
  • 3
    Yes, you should explain and show how to use `array_fill` to solve the problem. – Barmar Jun 10 '14 at 20:46
  • My apologies for the abrupt response. Yes, it would be better if you filled this out into a complete answer. As it stands, its more of a suggestion rather than an answer. – War10ck Jun 10 '14 at 20:47
  • I can't overwrite the indexes the array called `$row` in this example is from a query. It will completely overwrite an existing array. – yoshi Jun 10 '14 at 20:49
  • 1
    Might be obvious but ... Is the `for` loop better for performance or `array_merge()`? – yoshi Jun 10 '14 at 20:55
  • 1
    Hmmm... it seems array_merge() is not the most efficient method out there. I had no idea - I've always opted for code legibility. http://ocportal.com/site/news/view/chris_grahams_blog/php-performance-array.htm – rroberts Jun 10 '14 at 21:04
  • $row is a multidimensional array, therefore if you use array_merge, the result would be something like: {0=>array('something'=>'21'), 1=>array('something')=>'42', 2=>0, 3=>0, ...} what it's very different of the OP expected output. – Victor Henriquez Jun 10 '14 at 21:46
1

just a little shorter

for($i=0; $i<=10; $i++) {
  if ($i>0) $string .= ', ';
  $string .= isset($row[$i]['something']) ? $row[$i]['something'] : '0';
}
Fabricator
  • 12,722
  • 2
  • 27
  • 40
1

array_fill to initialize your new array, foreach to override the selected indexes and join to create the final string, it's an acceptable way to go.

$newArray=array_fill(0, 10, 0);
foreach($row as $key => $value) $newArray[$key] = array_shift($value);
echo join(' ',$newArray);
Victor Henriquez
  • 1,399
  • 15
  • 26
0

With array_fill and Array Operand +

$row['10']['something'] = "21";
$row['5']['something'] = "42";

echo "<p>row:<p>" ;
print_r( $row );

$maxIdx = max(array_keys($row)) + 1;
$newarray = array_fill(0,  $maxIdx , 0 );

echo "<p>newarray:<p>" ;
print_r( $newarray );

$newrow = $row + $newarray;
ksort($newrow);
echo "<p>newrow:<p>" ;
print_r( $newrow );

Result

row:

Array ( [10] => Array ( [something] => 21 ) [5] => Array ( [something] => 42 ) )

newarray:

Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 [8] => 0 [9] => 0 [10] => 0 )

newrow:

Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => Array ( [something] => 42 ) [6] => 0 [7] => 0 [8] => 0 [9] => 0 [10] => Array ( [something] => 21 ) )
lbolanos
  • 21
  • 1
  • 2