PHP: last multidimensional array with end-function - php

I have a multidimensional array called $response - and the max count is not unqiue. Sometimes it's just 1 up to 4. I want the last array of the second "0":
$last_value = $response['name1'][0]['name2'][0]['name3']['name4'];
...so, if there are
$last_value = $response['name1'][0]['name2'][0]['name3']['name4'];
$last_value = $response['name1'][0]['name2'][1]['name3']['name4'];
$last_value = $response['name1'][0]['name2'][2]['name3']['name4'];
$last_value = $response['name1'][0]['name2'][3]['name3']['name4'];
choose the one with:
$last_value = $response['name1'][0]['name2'][3]['name3']['name4'];
I know, there is the php function end, but I don't get it with the example above.

$last_value = end($response['name1'][0]['name2'])['name3']['name4'];
PHP 5.4+ is required for the Dereferencing of Functions or Methods.
Otherwise it will have to be a two parter for pre PHP 5.4.
$pre = end($response['name1'][0]['name2']);
$last_value = $pre['name3']['name4'];
Testing Environment:
<?php
$response['name1'][0]['name2'][0]['name3']['name4'] = '1';
$response['name1'][0]['name2'][1]['name3']['name4'] = '2';
$response['name1'][0]['name2'][2]['name3']['name4'] = '3';
$response['name1'][0]['name2'][3]['name3']['name4'] = '4';
$myLastElement = end($response['name1'][0]); //Clément Andraud's Answer
$last_value = end($response['name1'][0]['name2'])['name3']['name4'];
var_dump($myLastElement); //Clément Andraud's Output
echo '<br />';
var_dump($last_value);
?>
Testing Results:
array(4) { [0]=> array(1) { ["name3"]=> array(1) { ["name4"]=> string(1) "1" } } [1]=> array(1) { ["name3"]=> array(1) { ["name4"]=> string(1) "2" } } [2]=> array(1) { ["name3"]=> array(1) { ["name4"]=> string(1) "3" } } [3]=> array(1) { ["name3"]=> array(1) { ["name4"]=> string(1) "4" } } }
string(1) "4"

$myLastElement = end($response['name1'][0]);
This doesn't work ?

Related

How to group a multidimensional array by multiple subarray values?

I checked this question and answers:
How to group a multidimensional array by a particular subarray value?
He wanted to group results by 'level'. But how would you do it to group it by 'level' first and then by 'type'?
Its pretty straight forward. Loop through $items array. Get each item's level and type and if they are not set yet, initialize them with an empty array. Then just push the "cust" value into the array.
I have given the code below.
I am assuming "$items" is an array which contains the input.
$g = [];
foreach($items as $k => $v) {
$l = $v["level"];
$t = $v["type"];
$c = $v["cust"];
if(!isset($g[$l])) {
$g[$l] = [];
}
if(!isset($g[$l][$t])) {
$g[$l][$t] = [];
}
$g[$l][$t][] = [
"cust" => $c
];
}
var_dump($g);
The output of this code would be like below:
array(3) {
[1]=>
array(1) {
["standard"]=>
array(2) {
[0]=>
array(1) {
["cust"]=>
string(6) "XT8900"
}
[1]=>
array(1) {
["cust"]=>
string(6) "XT8944"
}
}
}
[3]=>
array(1) {
["premier"]=>
array(2) {
[0]=>
array(1) {
["cust"]=>
string(6) "XT8922"
}
[1]=>
array(1) {
["cust"]=>
string(6) "XT8816"
}
}
}
[7]=>
array(1) {
["standard"]=>
array(1) {
[0]=>
array(1) {
["cust"]=>
string(6) "XT7434"
}
}
}
}
[P.S.]: You can also use sort to solve this problem easily. That's another way of solving this problem.

Reserve php foreach. array_reverse doesn't works [duplicate]

array(7) {
[0]=> array(2) { ["id"]=> string(1) "9" ["roi"]=> float(0) }
[1]=> array(2) { ["id"]=> string(1) "1" ["roi"]=> float(0) }
[2]=> array(2) { ["id"]=> string(2) "10" ["roi"]=> float(0) }
[3]=> array(2) { ["id"]=> string(2) "14" ["roi"]=> float(0) }
[4]=> array(2) { ["id"]=> string(1) "4" ["roi"]=> float(0) }
[5]=> array(2) { ["id"]=> string(1) "5" ["roi"]=> float(141) }
[6]=> array(2) { ["id"]=> string(1) "6" ["roi"]=> float(2600) }
}
I would just like to reverse this, so id 6 (with roi of 2600) comes first in the array etc.
How can I do this? array_reverse() and rsort() does not work in this case
http://php.net/manual/en/function.array-reverse.php:
$newArray = array_reverse($theArray, true);
The important part is the true parameter, which preserves the keys.
Not convinced? You can see it in action on this codepad exampole.
foreach($array as $arr){
array_unshift($array, $arr);
array_pop($array);
}
$res = array(
0=>array("id"=>9, "roi"=>0),
1=>array("id"=>1,"roi"=>0),
2=>array("id"=>10,"roi"=>0),
3=>array("id"=>14,"roi"=>0),
4=>array("id"=>4,"roi"=>0),
5=>array("id"=>5,"roi"=>141),
6=>array("id"=>6,"roi"=>2600));
$res4 = array();
$count = count($res);
for($i=$count-1;$i>=0;$i--){
$res4[$i] =$res[$i];
}
print_r($res4);
You can use an usort() function, like so
$arr = array('......'); // your array
usort($arr, "my_reverse_array");
function my_reverse_array($a, $b) {
if($a['roi'] == $b['roi'])
{
return 0;
}
return ($a['roi'] < $b['roi']) ? -1 : 1;
}
This will make sure the item with the highest roi is first in the array.
$res = array(
0=>array("id"=>9, "roi"=>0),
1=>array("id"=>1,"roi"=>0),
2=>array("id"=>10,"roi"=>0),
3=>array("id"=>14,"roi"=>0),
4=>array("id"=>4,"roi"=>0),
5=>array("id"=>5,"roi"=>141),
6=>array("id"=>6,"roi"=>2600));
$count = count($res);
for ($i=0, $j=$count-1; $i<=floor($count/2); $i++, $j--) {
$temp = $res[$j];
$res[$j] = $res[$i];
$res[$i] = $temp;
}
echo '<pre>';
print_r($res);
echo '</pre>';
It's easy. May be use usort function of php like:
usort($arr, function($a, $b) {
return $b['roi'] - $a['roi'];
});
Just swap position $a and $b that is correct.

PHP: Combining data with a shared key from a single array into new array

I have this array:
array(5) {
[0]=>
array(4) {
["productCode"]=>
string(4) "X001"
["productUPC"]=>
string(3) "261"
["productTextSeq"]=>
string(1) "1"
["productTxtVal"]=>
string(5) "Text1"
}
[1]=>
array(4) {
["productCode"]=>
string(4) "X001"
["productUPC"]=>
string(3) "261"
["productTextSeq"]=>
string(1) "2"
["productTxtVal"]=>
string(5) "Text2"
}
[2]=>
array(4) {
["productCode"]=>
string(4) "X001"
["productUPC"]=>
string(3) "261"
["productTextSeq"]=>
string(1) "3"
["productTxtVal"]=>
string(5) "Text3"
}
[3]=>
array(4) {
["productCode"]=>
string(4) "X002"
["productUPC"]=>
string(3) "262"
["productTextSeq"]=>
string(1) "1"
["productTxtVal"]=>
string(5) "Text1"
}
[4]=>
array(4) {
["productCode"]=>
string(4) "X002"
["productUPC"]=>
string(3) "262"
["productTextSeq"]=>
string(1) "2"
["productTxtVal"]=>
string(5) "Text2"
}
}
With the above input, I want the output array to look like this:
array(2) {
[0]=>
array(3) {
["productCode"]=>
string(4) "X001"
["productUPC"]=>
string(3) "261"
["productTxtVal"]=>
string(17) "Text1 Text2 Text3"
}
[1]=>
array(3) {
["productCode"]=>
string(4) "X002"
["productUPC"]=>
string(3) "262"
["productTxtVal"]=>
string(11) "Text1 Text2"
}
}
The resulting array does not need the productTextSeq key, just the combined values of productTextVal, when the productCode is the same. I've searched SO for examples of this but it seems every example I've found are based on multiple input arrays. I know I can brute force this with nested foreach functions but would love a more elegant solution.
I ended up just doing it the brute force method, here is my solution if anyone's interested:
$productData = array();
$sortedData = array();
$comments = '';
$saveKey = '';
$appendComment = false;
$idx = 0;
foreach ($data as $key=>$value) {
foreach ($value as $k=>$v) {
if ($k == 'productCode') {
if ($v == $saveKey) {
$appendComment = true;
} else {
$appendComment = false;
$saveKey = $v;
if ($idx !== 0) { // Don't write to array on first iteration!
$productData['productTxtVal'] = $comments;
$sortedData[] = $productData;
}
}
}
if ($k == 'productTxtVal') {
if ($appendComment == true) {
$comments .= ' ' . trim($v);
} else {
$comments = trim($v);
}
}
}
$productData = $value;
$idx++;
}
Not "elegant" but it works. I also have a check after this logic in case only one productCode is in the original array, as it won't be written to the $sortedData array since the key never changes.
The following code assumes you control the contents of the original data array (due to risk of injection using extract() function) and that no 2 items with the same productCode have the same productTextSeq.
$products = [];
foreach ($data as $item) {
// extract contents of item array into variables
extract($item);
if (!isset($products[$productCode])) {
// create product array with code, upc, text as array
$products[$productCode] = compact('productCode', 'productUPC') + ['productTxtVal' => []];
}
// add text value to array with sequence as index
$products[$productCode]['productTxtVal'][$productTextSeq] = $productTxtVal;
}
$products = array_values( // ignore array keys
array_map(function($product) {
ksort($product['productTxtVal']); // sort text as array by index/ sequence
$product['productTxtVal'] = implode(' ', $product['productTxtVal']); // implode into string
return $product;
}, $products)
);
You can run the code here: https://repl.it/BWQL

find / select specified ID in array php

i have array with database, and have to select only this items what have "tid" = 1
array(3) {
[1]=>
array(4) {
["tid"]=> "1"
["title"]=> "Google"
["url"]=> "http://google.com/"
["description"]=> "A very efficient search engine."
}
[2]=>
array(4) {
["tid"]=> "2"
["title"]=> "Facebook"
["url"]=> "http://facebook.com/"
["description"]=> "Trade securities, currently supports nearly 1000 stocks and ETFs"
}
[3]=>
array(4) {
["tid"]=> "1"
["title"]=> "Yandex"
["url"]=> "http://yandex.ru/"
["description"]=> "Another efficient search engine popular in Russia"
}
}
how can i select only this items from array what have "tid" = 1?
<?php
$final_arr = array();
foreach($tid_arrs as $tid_arr){
if($tid_arr['tid'] == 1){
$final_arr[] = $tid_arr;
}
}
print_r($final_arr);
?>
$filteredArray = array();
for($i = 0, $end = count($array);$i < $end;i++)
{
if($array[$i]["tid"] === "1")
{
$filderedArray[] = $array[$i];
}
}
That way $filteredArray will contain solely the items with tid 1;
Try array_filter function: http://php.net/manual/en/function.array-filter.php this should help.
print_r(array_filter($array, "filter_function"));
function filter_function($element){
return (int)$element['tid'] === 1;
}
let's say you starting array is $arr.
$result = array();
foreach ($arr as $arrItem) {
if ((array_key_exists('tid', $arrItem)) && ($arrItem['tid'] == "1")){
$result[] = $arrItem;
}
}
$result should be what you are excepted.

Reverse array in php

array(7) {
[0]=> array(2) { ["id"]=> string(1) "9" ["roi"]=> float(0) }
[1]=> array(2) { ["id"]=> string(1) "1" ["roi"]=> float(0) }
[2]=> array(2) { ["id"]=> string(2) "10" ["roi"]=> float(0) }
[3]=> array(2) { ["id"]=> string(2) "14" ["roi"]=> float(0) }
[4]=> array(2) { ["id"]=> string(1) "4" ["roi"]=> float(0) }
[5]=> array(2) { ["id"]=> string(1) "5" ["roi"]=> float(141) }
[6]=> array(2) { ["id"]=> string(1) "6" ["roi"]=> float(2600) }
}
I would just like to reverse this, so id 6 (with roi of 2600) comes first in the array etc.
How can I do this? array_reverse() and rsort() does not work in this case
http://php.net/manual/en/function.array-reverse.php:
$newArray = array_reverse($theArray, true);
The important part is the true parameter, which preserves the keys.
Not convinced? You can see it in action on this codepad exampole.
foreach($array as $arr){
array_unshift($array, $arr);
array_pop($array);
}
$res = array(
0=>array("id"=>9, "roi"=>0),
1=>array("id"=>1,"roi"=>0),
2=>array("id"=>10,"roi"=>0),
3=>array("id"=>14,"roi"=>0),
4=>array("id"=>4,"roi"=>0),
5=>array("id"=>5,"roi"=>141),
6=>array("id"=>6,"roi"=>2600));
$res4 = array();
$count = count($res);
for($i=$count-1;$i>=0;$i--){
$res4[$i] =$res[$i];
}
print_r($res4);
You can use an usort() function, like so
$arr = array('......'); // your array
usort($arr, "my_reverse_array");
function my_reverse_array($a, $b) {
if($a['roi'] == $b['roi'])
{
return 0;
}
return ($a['roi'] < $b['roi']) ? -1 : 1;
}
This will make sure the item with the highest roi is first in the array.
$res = array(
0=>array("id"=>9, "roi"=>0),
1=>array("id"=>1,"roi"=>0),
2=>array("id"=>10,"roi"=>0),
3=>array("id"=>14,"roi"=>0),
4=>array("id"=>4,"roi"=>0),
5=>array("id"=>5,"roi"=>141),
6=>array("id"=>6,"roi"=>2600));
$count = count($res);
for ($i=0, $j=$count-1; $i<=floor($count/2); $i++, $j--) {
$temp = $res[$j];
$res[$j] = $res[$i];
$res[$i] = $temp;
}
echo '<pre>';
print_r($res);
echo '</pre>';
It's easy. May be use usort function of php like:
usort($arr, function($a, $b) {
return $b['roi'] - $a['roi'];
});
Just swap position $a and $b that is correct.

Categories