Reputation: 2570
I have an array of posts similar to below
$arr = array(
day => 2,
day => 7,
day => 8
)
I have 10 numbers and I want to have a new array to look like below. I need to loop through the array and assign it to the iterated numbers if they are equal.
1 - ""
2 - 2
3 - ""
4 - ""
5 - ""
6 - ""
7 - 7
8 - 8
9 - ""
10 - ""
How can I achieve this with php loop?
I'm thinking of but it gives me 30 records.
for ($x=0; $x < 10; $x++)
foreach($arr as $a) {
....
}
}
Upvotes: 0
Views: 64
Reputation: 170
You have defined array in wrong way. You can try the below code -
$arr = array('day' => array(2,7,8));
$temp_arr = array_fill(1, 10, "");
foreach($arr['day'] as $value){
$temp_arr [$value] = $value;
}
echo "<pre>";
print_r($temp_arr);
Upvotes: 1
Reputation: 4041
You can try this too :
for ($x=1; $x <= 10; $x++) {
$newArr[$x] = in_array($x, $arr) ? $x : "";
}
Upvotes: 2
Reputation: 3507
Try this:
//create an array
$array2 = array_fill(1, 10, "");
foreach($array as $value){
$array2[$value] = $value;
}
Upvotes: 0