Reputation: 2400
I have array 1 and it should be 2
Does anyone have an idea / solution?
Do i need foreach or for loop?
1.
Array
(
[0] => Array
(
[category_id] => 5
[category] => Pages
)
)
Must be:
Array
(
[0] => Array
(
[5] => Pages
)
)
I have this but this doent work...
for($x = 0; $x <= $counter; $x++){
foreach ($Categories[$x] as $key => $value){
echo $key.' '. $value.'<br>';
}
$test[$x]['category_id'] .= $Categories[$x]['category'];
}
Thanks for all the help!
Upvotes: 0
Views: 718
Reputation: 23490
As you said you will need a foreach loop to manupulate you array.
Example
$array = array
(
'0' => array
(
'category_id' => '5',
'category' => 'Pages'
)
);
$new_array = array();
foreach($array as $val)
{
$new_array[$val['category_id']] = $val['category'];
}
var_dump($new_array);
this will output
array(1) { [5]=> string(5) "Pages" }
Upvotes: 1
Reputation: 3852
$output=array();
foreach($array as $k=>$v)
{
$output[$v['category_id']]=$v['category'];
}
echo "<pre />";
print_r($output);
for multidimensinal result :
$output=array();
foreach($array as $k=>$v)
{
$output[][$v['category_id']]=$v['category'];
}
echo "<pre />";
print_r($output);
Upvotes: 2
Reputation: 5754
Code:
<?php
$arr = array(
array(
'category_id' => 5 ,
'category' => 'Pages',
),
);
$new = array();
foreach ($arr as $item) {
$new[] = array(
$item['category_id'] => $item['category']
);
}
print_r($new);
Result:
Array
(
[0] => Array
(
[5] => Pages
)
)
Upvotes: 2