Reputation: 199
I currently have an Array:
[1] => Array
(
[0] => 100011
[1] => 1
)
[2] => Array
(
[0] => 100013
[1] => 1
)
[3] => Array
(
[0] => 100022
[1] => 1
)
[4] => Array
(
[0] => 100025
[1] => 1
I want to take the first child item (meaning [0]) of each array (1,2,3,4,etc) and put it into a new array. I know I need to loop through and assign the value to new array. Just not sure how to do it.
End result would be:
$final (name of new array) has values 100013,100022,100025,etc.
My real end result:
I need it kept in the same order, because I am then going to use array array_combine ( array $keys , array $values )
to create 100013 as the key and 1 as the value, 100022 as the key, 1 as the value, 100025 as the key, 1 as the value.
If you know a quicker way to accomplish, it is appreciated.
Thanks.
Upvotes: 2
Views: 5098
Reputation: 38502
if (PHP 5 >= 5.5.0)
$first_names = array_column($records, 0);
print '<pre>';
print_r($first_names);
Another way,
function one_dimension($n)
{
return $n[0];
}
$result =array_map("one_dimension", $records);
print '<pre>';
print_r($result);
Upvotes: 0
Reputation: 173562
If I understand you right, the final result can be obtained by doing:
array_combine(array_column($arr, 0), array_column($arr, 1));
Or, in a more traditional way:
$result = [];
foreach ($arr as list($key, $value)) {
$result[$key] = $value;
}
Upvotes: 10
Reputation: 466
<?php
$results = array();
foreach($array as $item)
{
$results[] = $item[0];
}
?>
Upvotes: 0