This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 11 months ago.
I have an array called $friends with names and ids, gotten from the Facebook graph API.
If I print it, it look something like this:
Array (
[0] => Array (
[name] => Tom
[id] => 21)
[1] => Array (
[name] => Bob
[id] => 22)
)
How do I retrieve all Keys (ids) and create a new array like this one?
Array ( [0] => 21 [1] => 22 )
$ids = array_map(function ($friend) { return $friend['id']; }, $friends);
Note, uses PHP 5.3+ anonymous function syntax.
You can use a simple foreach.
$ids = array();
foreach($friends as $friend)
$ids[] = $friend['id'];
foreach ($friends as $key => $value) {
$newFriends[$key] = $value['id'];
}
$arFinal = array();
foreach ($friends as $key => $val){
$arFinal[$key] = $val['id'];
}
Related
This question already has answers here:
Filter/Remove rows where column value is found more than once in a multidimensional array
(4 answers)
Closed 9 months ago.
Is it possible to remove sub arrays, if the array item at position 0 of that sub array matches subsequent items?
For example;
Array ( [0] => Array ( [0] => 1234 [1] => XX000 )
[1] => Array ( [0] => 1234 [1] => XX001 )
[2] => Array ( [0] => 1234 [1] => XX002 )
)
Would be adjusted to output;
Array ( [0] => Array ( [0] => 1234 [1] => XX000 ) )
function my_array_filter($arr){
$exist = [];
return array_filter($arr, function($element){
if( ! array_key_exist($element[0], $exist)){
$exist[$element[0]] = true;
return true;
}
return false;
});
}
Maybe it is possible with some arcane usage of array functions and callback, but I prefer keeping things simple whenever possible, so I understand my own solutions years later.
So why not program it?
$ORG = [ [ 1234, 'XX000' ],
[ 1234, 'XX001' ],
[ 1234, 'XX002' ],
[19987, 'XX000'] ];
$NEW = [];
$USEDKEYS = [];
foreach ($ORG as $one){
if (in_array($one[0], $USEDKEYS)) {
// skip.
} else {
$NEW[] = $one;
$USEDKEYS[] = $one[0];
}
}
unset ($USEDKEYS, $ORG);
var_dump ($NEW);
Always the way, I've found out the solution after posting this ($query being the multi-dimensional array);
$newArr = array();
foreach ($query as $val) {
$newArr[$val[0]] = $val;
}
$query = array_values($newArr);
This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 6 years ago.
I have an array that looks like this:
Array (
[0] => Array ([order_variable_key] => surname [order_variable_value] => Hudsons )
[1] => Array ( [order_variable_key] => number [order_variable_value] => 13 )
[2] => Array ( [order_variable_key] => firstname [order_variable_value] => Dave )
)
I want to convert it to an array that looks like this:
Array(
'surname' => Hudsons,
'number' => 13,
'firstname' => Dave);
I managed to isolate the values but was not able to pair them with eachother.
I want to pair the values of the nested array with eachother.
$new_array= array();
foreach($array_name1 as $key=>$val){
$new_array[$val['order_variable_key']] = $val['order_variable_value'];
}
you can use
$newArray = array();
foreach($array as $obj)
{
$newArray[$obj['order_variable_key']] = $obj['order_variable_value']
}
Try this:
$arr = // your array;
$data = array();
foreach($arr as $val)
{
$arr[$val['order_variable_key']] = $val['order_variable_value'];
}
print_r($arr);
will give your desired format.
This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
Closed 9 years ago.
I have an array like the following:
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => 3
)
[2] => Array
(
[0] => 4
)
)
Now I want each array value into one single array. How do I do it?
Thanks in advance.
// PHP >= 5.3:
function array_value_recursive($key, array $arr){
$val = array();
array_walk_recursive($arr, function($v, $k) use($key, &$val){
if($k == $key) array_push($val, $v);
});
return $val;
}
You can recursively parse the array with a function:
$multiDimArr = array(...);
function getSingleArray( $multiDimArr ) {
$singleArray = array();
foreach($multiDimArr as $row) {
if( is_array($row) ) {
getSingleArray($row); // recursive call -> row it cand be also multi dimensional
}
else {
$singleArray[] = $val;
}
}
return $singleArray;
}
I really hope this will help!!
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php values of one array to key of another array
i have given two array here.
Array-1
(
[0] => 6
[1] => 11
[2] => 13
)
Array-2
(
[0] => 13.339066309
[1] => 0
[2] => 100
)
I want to replace value of one array to key of another array. something like this:
Array
(
[6] => 13.339066309
[11] => 0
[13] => 100
)
Use array_combine:
$new_array = array_combine($array1, $array2);
take a look at array_combine()
$result = array_combine(array_values($firstArr), array_values($secondArr));
Try this:
$result = array();
foreach ($array1 as $key => $value) {
$result[$value] = $array2[$key];
}
Try something like this
$t = array();
$keys = array_keys($arr1);
for ($i = 0; $i < min(count($keys), count($arr2)); $i++) {
$t[$keys[$i]] = $arr2[$i];
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
In PHP, how do you change the key of an array element?
This the array
Array
(
[0] => Array
(
[id] => 1
[due_date] => 2011-09-23
[due_amount] => 10600.00
)
[1] => Array
(
[id] => 2
[due_date] => 2011-10-23
[due_amount] => 10600.00
)
[2] => Array
(
[id] => 3
[due_date] => 2011-11-23
[due_amount] => 10600.00
)
)
how to change id to u_id in this array?
Regards
array_walk_recursive($array, 'changeIDkey');
function changeIDkey($item, &$key)
{
if ($key == 'id') $key = 'u_id';
}
PHP Manual: array_walk_recursive
EDIT
This will not work for the reason #salathe gave in the Comments below. Working on alternative.
ACTUAL ANSWER
function changeIDkey(&$value,$key)
{
if ($value === "id") $value = "u_id";
}
$new = array();
foreach ($array as $key => $value)
{
$keys = array_keys($value);
array_walk($keys,"changeIDkey");
$new[] = array_combine($keys,array_values($value));
}
var_dump($new); //verify
Where $array is your input array. Note that this will only work with your current array structure (two-dimensions, changes keys on only second dimension).
The loop iterates through the inner arrays changing "id" to "u_id" in the keys and then recombines the new keys with the old values.
foreach( $array as &$element ) {
$element['u_id'] = $element['id'];
unset( $element['id'] );
}