Skip to content

Instantly share code, notes, and snippets.

@jcnevado
Created July 15, 2013 01:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcnevado/5996868 to your computer and use it in GitHub Desktop.
Save jcnevado/5996868 to your computer and use it in GitHub Desktop.
Inserting items in a certain position of an array.
<?php
$rows = $data = array();
for ($i=0; $i < 20; $i++) {
$rows[] = (object) array(
'id' => $i + 1,
'title' => 'test ' . rand() * 200,
'format' =>'nota',
);
}
$banners = array(
'0' => (object) Array('format' =>'banner', 'index' => 0),
'1' => (object) Array('format' =>'banner', 'index' => 1),
'4' => (object) Array('format' =>'banner', 'index' => 4),
);
foreach ($banners as $key => $value) {
array_splice($rows, $key, 0, array($banners[$key]));
}
$rows = array_slice($rows, 0, 5, TRUE);
print_r($rows);
print count($rows);
/*
0 1 2 3 4 5 6
1 2 3 4 5 6 7
1 * 2 3 4 5 6 7
1 * 2 * 3 4 5 6 7
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment