I have a 2d array called $myarray and I use var_dump($myarray), it gives me the following:
array(4) {
[0]=> array(3) {
[0]=> string(11) "PAY000003RV"
[1]=> string(3) "EUR"
[2] => string(9) "43,543.23"
}
[1]=> array(3) {
[0]=> string(11) "PAY000002PE"
[1]=> string (3) "USD"
[2]=> string(9) "13,432.34"
} [2]=> array(3) {
[0]=> string(11) "PAY000001YB"
[1] => string(3) "GBP"
[2]=> string(8) "3,432.21"
} [3]=> array(3) {
[0]=> string(11) "PAY000004TS"
[1]=> string(3) "CAD"
[2]=> string(8) "2,321.34"
}
}
I want to get the following output:
GBP 3,432.21
USD 13,432.34
EUR 43,543.23
CAD 2,321.34
so I try to use substr($myarray[0][0], 8, 1), substr($myarray[1][0], 8, 1), substr($myarray[2][0], 8, 1), substr($myarray[3][0], 8, 1) to get the value 3,2,1,4 in order to use it to sort the array to the above order and then delete $myarray[0][0], $myarray[1][0], $myarray[2][0], $myarray[3][0] which are the "PAY0000.." elements in every row, but I am not sure how exactly to implement that, any experts could help me with that? any help will be greatly appreciated!
//Extract sort key
$tmp=array();
foreach($myarray as $m) $tmp[$m[0]]=array($m[1],$m[2]);
//Sort array
ksort($tmp);
//Create output
foreach($tmp as $m) echo $m[0].' '.$m[1];
Some comments:
You can sort by PAY00000nXY without extracting n
The code assumes you need the array for more than printing it out, else it might be inefficient
Let's say your array is assigned to variable $foo
$values = array();
foreach ( $foo as $bar ) {
foreach ( $bar as $val ) {
$values[] = $val[1] . ' ' . $val[2];
}
}
Then you will have the array $values with the values you need.
Related
I have a big array which looks like this:
array(2) {
["Final Fantasy VII"]=>
array(5) {
["rows"]=>
array(2) {
[0]=>
array(6) {
["price"]=>
string(5) "11.69"
["price_old"]=>
string(4) "4.66"
["currency"]=>
string(4) "euro"
["portal"]=>
string(0) ""
["link"]=>
string(77) "https://de.gamesplanet.com/game/final-fantasy-vii-download--1001-1?ref=gmkeys"
["shop"]=>
string(4) "9507"
}
[1]=>
array(6) {
["price"]=>
string(5) "14.99"
["price_old"]=>
...
}
}
}
["Battlefield 1"]=>
array(3) {
["rows"]=>
array(2) {
[0]=>
array(6) {
["price"]=>
...
}
[1]=>
array(6) {
["price"]=>
...
}
}
}
}
And I want to get only certain parts of this array where the name is matching my searched title. So, I use this code for that:
function createACFRepeater($title){
$repeater = array();
if(searchForGameXML($title)){
$count = count($GLOBALS["productsXML"][$title]['rows']);
for($i = 0; $i < $count; $i++){
array_push($repeater, $GLOBALS["productsXML"][$title]['rows'][$i]);
}
return $repeater;
}else{
return $repeater;
}
}
My problem now is that the the $repeater array looks like this:
array(2) {
[0]=>
array(6) {
["price"]=>
string(5) "19.98"
["price_old"]=>
...
}
[1]=>
array(6) {
["price"]=>
string(4) "7.99"
["price_old"]=>
...
}
}
There is a numeric key which is pointing to the array [0] => .... But what I want is simply an array in a array without any associative relations...
How can I create an array which looks like this:?
array(2) {
array(6) {
["price"]=>
string(5) "19.98"
["price_old"]=>
...
}
array(6) {
["price"]=>
string(4) "7.99"
["price_old"]=>
...
}
}
Greetings and Thank You!
According to the array definition it is impossible. Any array item must have key and value, documentation for array starts from:
An array in PHP is actually an ordered map. A map is a type that associates values to keys
You will always have numeric keys. As #lubart already said: it's impossible to have an array without keys. Btw., all of the the follwing arrays are completely equal:
$array1 = array([0] => array([0] => 'hi', [1] => array([0] => '23.5')));
$array2 = array(array('hi', array('23.5')));
$array3 = [['hi', ['23.5']]];
$array4 = [ [0] => [ [0] => 'hi', [1] => [ [0] => '23.5' ] ] ];
I have the following array:
array(5) {
[0]=> array(3) {
[0]=> string(10) "2013-09-18"
[1]=> string(75) "Ready For Retina HD: Create Pixel-Perfect Assets For Multiple Scale Factors"
[2]=> string(74) "ready-for-retina-hd-create-pixel-perfect-assets-for-multiple-scale-factors"
}
[1]=> array(3) {
[0]=> string(10) "2010-10-20"
[1]=> string(40) "Taking A Closer Look At Tech Conferences"
[2]=> string(40) "taking-a-closer-look-at-tech-conferences"
}
[2]=> array(3) {
[0]=> string(10) "2014-10-19"
[1]=> string(29) "Wayfinding For The Mobile Web"
[2]=> string(29) "wayfinding-for-the-mobile-web"
}
[3]=> array(3) {
[0]=> string(10) "2014-05-15"
[1]=> string(24) "Freebie: Icons Of Autumn"
[2]=> string(23) "freebie-icons-of-autumn"
}
[4]=> &array(1){
[0]=> string(0) ""
}
}
How would I go about assigning key names to each part of the inner array? E,g date, title, pathname.
I understand you can do something like this to create an array with certain keys, but how does this work with multiple nested arrays? And how can it be assigned after array creation?
$keys = array('Date', 'Title', 'Filepath');
Assuming $array1 is your main array (with 5 values).
foreach($array1 as $a)
{
if (len($a) == 3)
$array2[] = array("Date" => $a[0], "Title" => $a[1], "Filepath" => $a[2]);
}
$array1 = $array2;
Use a foreach.
$new = [];
foreach ($origArray as $inner) {
$new[] = [
"date" => $inner[0],
"title" => $inner[1],
"filepath" => $inner[2]
];
}
$origArray = $new;
This doesn't handle cases where the item does not conform to the "standard" (e.g item 4) but this should get you started.
I have an array that contains 4 arrays with one value each.
array(4) {
[0]=>
array(1) {
["email"]=>
string(19) "test01#testmail.com"
}
[1]=>
array(1) {
["email"]=>
string(19) "test02#testmail.com"
}
[2]=>
array(1) {
["email"]=>
string(19) "test03#testmail.com"
}
[3]=>
array(1) {
["email"]=>
string(19) "test04#testmail.com"
}
}
What is the best (=shortest, native PHP functions preferred) way to flatten the array so that it just contains the email addresses as values:
array(4) {
[0]=>
string(19) "test01#testmail.com"
[1]=>
string(19) "test02#testmail.com"
[2]=>
string(19) "test03#testmail.com"
[3]=>
string(19) "test04#testmail.com"
}
In PHP 5.5 you have array_column:
$plucked = array_column($yourArray, 'email');
Otherwise, go with array_map:
$plucked = array_map(function($item){ return $item['email'];}, $yourArray);
You can use a RecursiveArrayIterator . This can flatten up even multi-nested arrays.
<?php
$arr1=array(0=> array("email"=>"test01#testmail.com"),1=>array("email"=>"test02#testmail.com"),2=> array("email"=>"test03#testmail.com"),
3=>array("email"=>"test04#testmail.com"));
echo "<pre>";
$iter = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr1));
$new_arr = array();
foreach($iter as $v) {
$new_arr[]=$v;
}
print_r($new_arr);
OUTPUT:
Array
(
[0] => test01#testmail.com
[1] => test02#testmail.com
[2] => test03#testmail.com
[3] => test04#testmail.com
)
I got an $actions array, and $actions_used array.
$actions_used looks like this:
array(1) {
[2]=>
string(1) "18"
[5]=>
string(1) "33"
}
$actions looks like this:
array(3) {
[1]=>
string(9) "Withdraw"
[2]=>
string(13) "Deposit"
[5]=>
string(10) "Blabla"
}
I would like to sort $actions based on the value that is in $actions_used.
The correct output would be for $actions:
array(3) {
[5]=>
string(9) "Blabla"
[2]=>
string(13) "Deposit"
[1]=>
string(10) "Withdraw"
}
Why? Because array key 5, "Blabla" has the biggest value "33" and then comes array key "2" which has value 18 and then at last comes array key 1, "Withdraw" which have 0 (no value)
How can this be done?
This should do the trick.
$sorted_actions = array();
asort($actions_used);
foreach($actions_used AS $key => $amount) {
$sorted_actions[] = array('amount' => $amount, 'action' => $actions[$key]);
unset($actions[$key]);
}
$sorted_actions = $sorted_actions + $actions;
How would something like this work?
arsort($action_used);
foreach ($action_used as $k => $v) {
$newArray[$k] = $actions[$k];
unset($action_used[$k];
}
$newArray = array_merge($newArray, $action_used);
I think you could sort the second array with uksort and actually compare the values from the first array in the custom comparer
e.g.:
uksort($arr2, function($i1, $i2) {
return $arr1[$i1] - $arr1[$i2];
});
I have got a 2d array called $myarray, and when I using var_dump($myarray), it gives me the following:
array(4) { [0]=> array(2) { [0]=> string(3) "EUR" [1]=> string(9) "43,543.23" } [1]=> array(2) { [0]=> string(3) "USD" [1]=> string(9) "13,432.34" } [2]=> array(2) { [0]=> string(3) "GBP" [1]=> string(8) "3,432.21" } [3]=> array(2) { [0]=> string(3) "CAD" [1]=> string(8) "2,321.34" } }
But I want the output to be the follwing format:
Totals
GBP 3,432.21
USD 13,432.34
EUR 43,543.23
CAD 2,321.34
I assume that I need to sort the array to be:
GBP 3,432.21
USD 13,432.34
EUR 43,543.23
CAD 2,321.34
and add "Totals", "" into the array, I might be wrong, soanybody could help me with that, any help will be greatly appreciated! I want it to be done in a programmatic way! how to sort the array $myarray to the expected output?
You're almost right.
Don't add the 'Totals' into the array. It is not data, mere decoration. For the rest, I wouldn't 'dump' the array, but e.g. for a string from it. As a rule, don't change your data especially for output formatting reasons, you'll find yourself removing the 'Totals' entry in the next function processing the data...:
Also, you want to control the iteration order; therefore you can iterate over the desired keys instead of over the array itself:
$output="Totals:\n";
foreach( $currency in array("GBP","USD","EUR","CAD") ) {
$entry=$data[$currency];
$output.=$currency." ".$entry[1]."\n";
}
dump($output);
EDIT - added the bit about ordering
Firstly, do not add anything non-data related to your array. You the string "Totals" should not be included in your array. If you just want the output, I suggest a better way to solve your problem :
<?php
class MyMoney {
var $type;
var $value;
public function __toString() {
return $type." ".$value."\n";
}
}
$output="Totals:\n";
// $data is an array of MyMoney objects
foreach( $entry in $data ) {
$output.= (string) $entry;
}
dump($output);
I hope this helps.
I'm not sure if this is what you're describing but perhaps it can be of use in your situation:
$myArray = array(
"title" => "Totals",
"data" => array(
"GBP" => "3,432.21",
"USD" => "13,432.34",
"EUR" => "43,543.23",
"CAD" => "2,321.34"
)
);
var_dump($myArray);
Output:
array(2) {
["title"]=>
string(6) "Totals"
["data"]=>
array(4) {
["GBP"]=>
string(8) "3,432.21"
["USD"]=>
string(9) "13,432.34"
["EUR"]=>
string(9) "43,543.23"
["CAD"]=>
string(8) "2,321.34"
}
}
<?php
$output = "Totals<br>";
foreach ($myarray as $value)
{
$output .= $value[0].' '.$value[1].'<br>';
}
echo $output;
?>