This question already has answers here:
How do I retrieve a single column from mysqli results
(5 answers)
Closed 6 months ago.
I am currently using a mysql_query with the UNION function. This is the array that I get:
Array
(
[0] => bob
[added] => bob
)
Array
(
[0] => test1
[added] => test1
)
Is there a way that I can take this array, merge it, remove the added values, place the data in numerical order and make it look like this?:
Array
(
[0] => bob
[1] => test1
)
I know somebody'll ask me what have I done so far. Honestly, I have no idea where to go from here.
array_reduce(
array_map(function($i) {
return $i[0];
}, $data),
function($result, $item) {
$result[] = $item;
return $result;
},
array()
);
or
call_user_func_array('array_merge',
array_map(function($i) {
return $i[0];
}, $data)
);
$array1=array_unique($array1);
$array2=array_unique($array2);
$result = array_merge ($array1,$array2);
When you are fetching the data you can create your array eg:
while($row = mysqli_fetch_array($result, MYSQLI_NUM){
$newArray[] = $row[0];
}
and from your current array you can do
$newArray = array();
foreach($array as $value){
$newArray = array_push($newArray,$value[0]);
}
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:
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:
Sorting XML with SimpleXML/XPath?
(3 answers)
Closed 9 years ago.
I am having a little trouble with an xml feed (atom). I am running a for each loop to return prices using simple xml and converting them too arrays which works fine below :-
foreach ($dc->departures->departure as $price)
{
$lowest = $price->prices->price[5]->asXML();
$lowestval = array($lowest);
print_r($lowestval);
}
Which is returning :-
Array ( [0] => 2289 )
Array ( [0] => 2207 )
Array ( [0] => 2369 )
Array ( [0] => 2229 )
My goal is to return only the lowest price, so I can display a Prices From: area. From what I understand I need to use the min() function, however this only works with one array with several values. I've tried array_merge which doesn't seem to work and just returns the same as above. I am a PHP newbie so there maybe something obvious. A kick in the correct direction would be appreciated.
Try this. Its working fine
<?php
foreach ($dc->departures->departure as $price)
{
$lowest = $price->prices->price[5]->asXML();
$lowestval[] = $lowest;
}
$min = min($lowestval);
echo $index = array_search($min, $array);
?>
$data = array();
$data[] =Array (0 => 2289 ) ;
$data[] = Array ( 0 => 2207 ) ;
$data[] = Array ( 0 => 2369 ) ;
$data[] = Array ( 0 => 2229 );
array_multisort($data);
$first = array_shift($data);
var_dump($first); // 2207
You can also use 'sort()' function to sort an array value.
Here is an example with some extra value as well merge array.
$arry1 = array(
array(5),
array(10000),
array(2289),
array(2288),
array(2207),
array(2369),
array(2229),
array(5421),
array(541)
);
$arry2 = array(
array(456789),
array(54564)
);
$arry1 = array_merge($arry1,$arry2);
sort($val);
echo '<pre>';
print_r($val);
echo '</pre>';
then you can use first element of an array as min value.
echo $arry1[0][0];
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'];
}
This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 5 months ago.
I have array like
Array (
[608665839] => Array ( [score] => 2 )
[1756044141] => Array ( [score] => 5 )
[523536777] => Array ( [score] => 2 )
)
and I want to sore this array by score. How can I do?
I would use uasort
I think [uasort()]1 function is helpful for sorting this array()
If multiple array then use array_[multisort()]2 functions
From PHP.net:
<?php
function order_array_num ($array, $key, $order = "ASC")
{
$tmp = array();
foreach($array as $akey => $array2)
{
$tmp[$akey] = $array2[$key];
}
if($order == "DESC")
{arsort($tmp , SORT_NUMERIC );}
else
{asort($tmp , SORT_NUMERIC );}
$tmp2 = array();
foreach($tmp as $key => $value)
{
$tmp2[$key] = $array[$key];
}
return $tmp2;
}
?>
$order = "ASC" will sort the array in an ascending order while $order = "DESC" will sort the array in a descending order.
Hope this helps.