This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 6 months ago.
A simple thing to do, but I forgot how to convert
Array
(
[0] => Array
(
[hashcode] => 952316176c1266c7ef1674e790375419
)
[1] => Array
(
[hashcode] => 5b821a14c98302ac40de3bdd77a37ceq
)
)
into this:
Array (952316176c1266c7ef1674e790375419, 5b821a14c98302ac40de3bdd77a37ceq)
I know this is premature but since this is coming soon I figured I throw this out there. As of (the not yet released) PHP 5.5 you can use array_column():
$hashcodes = array_column($array, 'hashcode');
Try this :
$array = array(array("test"=>"xcxccx"),array("test"=>"sdfsdfds"));
$result = call_user_func_array('array_merge', array_map("array_values",$array));
echo "<pre>";
print_r($result);
Output:
Array
(
[0] => xcxccx
[1] => sdfsdfds
)
A good ol' loop solves :)
<?php
$array = array(
array( 'hashcode' => 'hash' ),
array( 'hashcode' => 'hash2' ),
);
$flat = array();
foreach ( $array as $arr ) {
$flat[] = $arr['hashcode'];
}
echo "<pre>";
print_r( $flat );
?>
$source = array(
array(
'hashcode' => '952316176c1266c7ef1674e790375419'
),
array(
'hashcode' => '5b821a14c98302ac40de3bdd77a37ceq'
)
);
$result = array();
array_walk($source, function($element) use(&$result){
$result[] = $element['hashcode'];
});
echo '<pre>';
var_dump($result);
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:
Return single column from a multi-dimensional array [duplicate]
(7 answers)
How to implode array indexes?
(1 answer)
Closed 3 years ago.
I have an array that I am trying to convert into two strings, one with dates and one with the data values. This is a sample array:
[$output Array below]
Array
(
[2019-03-19] => Array
(
[data_values] => 1566
)
[2019-03-18] => Array
(
[data_values] => 1542
)
[2019-03-17] => Array
(
[data_values] => 786
)
[2019-03-16] => Array
(
[data_values] => 756
)
)
A desired output would be something like:
$dates = '2019-03-19,2019-03-18,2019-03-17,2019-03-16';
$data_values = '1566,1542,786,756';
I've tried this, which will give me the data_values but I can't get the dates, I assume because its the array key?
function implode_r($g, $p) {
return is_array($p) ?
implode($g, array_map(__FUNCTION__, array_fill(0, count($p), $g), $p)) :
$p;
}
$data_values = implode_r(',', $output);
echo $data_values;
You can just use array_keys and array_column:
$dates = implode(',', array_keys($output));
$data_values = implode(',', array_column($output, 'data_values'));
Demo on 3v4l.org
This will give you the answer you expect:
<?php
// Build strings based on array
function implode_r($output) {
$dates = $data_values = "";
if(is_array($output)){
$dates=implode(',',array_keys($output));
$data_values=implode(',',array_column($output, 'data_values'));
}
return compact('dates', 'data_values');
}
// Example usage
$output = [
"2019-03-17" => ["data_values" => 1],
"2019-03-18" => ["data_values" => 2],
"2019-03-19" => ["data_values" => 3],
"2019-03-20" => ["data_values" => 4]
];
$result = implode_r($output);
echo $result['dates'];
echo $result['data_values'];
This question already has answers here:
Merge two arrays as key value pairs in PHP
(3 answers)
Closed 7 years ago.
Given these two array:
$name=array("alice","ken","wendy");
$frequent=array(3,6,9);
I try to combine it like
$data = array($name=>$frequent);
but it fails. Anyone can help?
I want this:
$data = array(
'alice' => 3,
'ken' => 6,
'wendy' => 9,
);
You can use array_combine
$combined_array = array_combine($name, $frequent);
Documentation here: http://php.net/manual/en/function.array-combine.php
You can use array_combine function as
Syntax:
array_combine ( array $keys , array $values )
So yours is like
$name=array("alice","ken","wendy");
$frequent=array(3,6,9);
$result = array_combine($name,$frequent);
Output
Array
(
[alice] => 3
[ken] => 6
[wendy] => 9
)
[akshay#localhost tmp]$ cat test.php
<?php
$name=array("alice","ken","wendy");
$frequent=array(3,6,9);
// One easy way is
print_r( array_combine($name, $frequent) );
// Another lengthy way
while ( ($key = array_shift($name)) && ($value = array_shift($frequent)) )
{
$combined[$key] = $value;
}
print_r( $combined );
?>
Output
[akshay#localhost tmp]$ php test.php
Array
(
[alice] => 3
[ken] => 6
[wendy] => 9
)
Array
(
[alice] => 3
[ken] => 6
[wendy] => 9
)
write like this $combined_array = array_combine($name, $frequent);
If you want to do it manually.
<?php
$name=array("alice","ken","wendy");
$frequent=array(3,6,9);
$combined=array();
for($i=0; $i<3; $i++)
{
$combined[$name[$i]]=$frequent[$i];
}
var_dump($combined);
?>
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];
I have this multidimensional array:
Array (
[0] => Array (
[id] => 1
[list_name] => List_Red
)
[1] => Array (
[id] => 2
[list_name] => List_Blue
)
)
...and i would like to create a new array containing only the [id]'s from it.
I would appreciate it alot if you guys could help me with that ^^
Thanks in advance.
#fabrik Your solution indeed does work but it is also incorrect as PHP will throw a E_WARNING telling you that you're appending to an array that did not yet exist. Always initialise your variables before you use them.
$newList = array();
foreach($myList as $listItem) {
$newList[$listItem['id']] = $listItem['list_name'];
}
This is now a list of all your list_names in the following format.
Array (
1 => List_Red
2 => List_Blue
)
Much easier for you to work with and you can now iterate over it like so..
foreach($newList as $itemID => $itemName) {
echo "Item ID: $itemID - Item Name: $itemName<br>";
}
You could use array_map like this:
$new_array = array_map( function( $a ) { return $a['id']; }, $orig_array );
That's assuming PHP 5.3, for PHP < 5.3 you have to use create_function:
$new_array = array_map( create_function( '$a', 'return $a["id"];' ), $orig_array );
foreach($array as $label => $data)
{
$final[] = $data['id'];
}