I have an array like this $items():
我有一个类似$items()的数组:
Array
(
[0] => Array
(
[id] => 1
[first_name] => fname1
[laste_name] => lname1
[Age] => 20
)
[1] => Array
(
[id] => 2
[first_name] => fname2
[laste_name] => lname2
[Age] => 22
)
)
And I want to create another array only with [id] and [Age]
我想要创建另一个数组只使用[id]和[Age]
Array
(
[0] => Array
(
[id] => 1
[Age] => 20
)
[1] => Array
(
[id] => 2
[Age] => 22
)
)
So I have tried looping like this :
我试过这样循环
$array = array();
foreach($items as $item) {
$array['id']= $item['id'];
$array['age']= $item['age'];
}
But It gives only values of the last item
但它只给出最后一项的值
3 个解决方案
#1
2
Try with -
试一试,
$array = array();
foreach($items as $item) {
$temp['id']= $item['id'];
$temp['age']= $item['age'];
$array[] = $temp;
}
#2
2
Try with .It'll work for you.
试试,你会有用的。
$array = array();
foreach($items as $key => $item) {
$array[$key]['id']= $item['id'];
$array[$key]['age']= $item['age'];
}
#3
1
use this
使用这个
$array = array();
$new_array = array();
foreach($items as $item) {
$array['id']= $item['id'];
$array['age']= $item['age'];
$new_array[] = $array;
}
print_r($new_array);
Your result is :
你的结果是:
Array
(
[0] => Array
(
[id] => 1
[Age] => 20
)
[1] => Array
(
[id] => 2
[Age] => 22
)
)
#1
2
Try with -
试一试,
$array = array();
foreach($items as $item) {
$temp['id']= $item['id'];
$temp['age']= $item['age'];
$array[] = $temp;
}
#2
2
Try with .It'll work for you.
试试,你会有用的。
$array = array();
foreach($items as $key => $item) {
$array[$key]['id']= $item['id'];
$array[$key]['age']= $item['age'];
}
#3
1
use this
使用这个
$array = array();
$new_array = array();
foreach($items as $item) {
$array['id']= $item['id'];
$array['age']= $item['age'];
$new_array[] = $array;
}
print_r($new_array);
Your result is :
你的结果是:
Array
(
[0] => Array
(
[id] => 1
[Age] => 20
)
[1] => Array
(
[id] => 2
[Age] => 22
)
)