I have this $record array
array(4) {
[0]=> array(2) { ["ROLE_ID"]=> string(1) "2" ["SUBFUNCTION_ID"]=> string(3) "904" }
[1]=> array(2) { ["ROLE_ID"]=> string(1) "2" ["SUBFUNCTION_ID"]=> string(3) "903" }
[2]=> array(2) { ["ROLE_ID"]=> string(1) "2" ["SUBFUNCTION_ID"]=> string(3) "902" }
[3]=> array(2) { ["ROLE_ID"]=> string(1) "2" ["SUBFUNCTION_ID"]=> string(3) "901" }
}
How can i manipulate it so it will become like this?
array("901","902","903","904");
Thanks in advance
$subfunctionIds = array();
foreach($record as $values) {
$subFunctionIds[] = $values['SUBFUNCTION_ID'];
}
// If you want them reversed like in your example output...
$subFunctionIds = array_reverse($subFunctionIds);
var_dump($subFunctionIds);
function fetch($row) {
return $row["SUBFUNCTION_ID"];
}
$result = array_map("fetch", $record);
sort($result);
var_dump($result);
in 5.3+ you could do better:
$result = array_map(function ($row) { return $row["SUBFUNCTION_ID"]; }, $record);
sort($result);
var_dump($result);
Try doing this:
foreach ($array as $row ) {
$response[] = $row["SUBFUNCTION_ID"];
}
print_r($response);
Related
I have recently started studying PHP and I'm using Zend Framework v1.12.
I can't print the elements obtained by using FetchAll().
$stmt = $db->query('SELECT * FROM team');
$obj = $stmt->fetchAll();
foreach ($obj as $value) {
echo $value;
}
This is the notice (repeated x7):
Notice: Array to string conversion
Using var_dump I have this:
array(7) {
[0]=> array(2) {["id_team"]=> string(1) "1" ["nameteam"]=> string(10) "Ac Picchia" }
[1]=> array(2) { ["id_team"]=> string(1) "2" ["nameteam"]=> string(4) "test" }
[2]=> array(2) { ["id_team"]=> string(1) "3" ["nameteam"]=> string(4) "ciao" }
[3]=> array(2) { ["id_team"]=> string(1) "4" ["nameteam"]=> string(2) "oi" }
[4]=> array(2) { ["id_team"]=> string(1) "5" ["nameteam"]=> string(3) "xtz" }
[5]=> array(2) { ["id_team"]=> string(1) "6" ["nameteam"]=> string(3) "123" }
[6]=> array(2) { ["id_team"]=> string(1) "7" ["nameteam"]=> string(1) "x" } }
Teh object returned by the FetchAll is a Array of Arrays. So when you do the foreach and try to print every object in the array you got that error.
To fix it you should do something like:
$stmt = $db->query('SELECT * FROM team');
//I renamed this variable to make the code more readable.
$teams = $stmt->fetchAll();
foreach ($teams as $team) {
print_r($team); //This will print the array
}
I found the solution, even if it was a trivial problem, I write my solution to the problem maybe it will help someone.
$stmt = $db->query('SELECT * FROM team');
$obj = $stmt->fetchAll();
foreach ($obj as $value) {
echo $value['id_team'] , $value['nometeam'];
}
use loop like this :
$stmt = $db->query('SELECT * FROM team');
$obj = $stmt->fetchAll();
foreach ($obj as $key => $value) {
echo $value['id_team'].','. $value['nometeam'];
}
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.
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
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.
I am calling a webservice and I am getting a complex object back. I need to display variables reg_no, opening_inventory_weight.
RESULT:-
object(stdClass)#12 (3) {
["TxnErrors"]=> object(stdClass)#13 (0) { }
["TxnStatus"]=> bool(true)
["headers"]=> object(stdClass)#14 (1) {
["RPMHeader"]=> array(1) {
[0]=> object(stdClass)#15 (7) {
["opening_inventory_weight"]=> int(1001)
["prepared_by"]=> string(5) "James"
["reg_no"]=> string(7) "5000005"
["reporting_period"]=> string(19) "2010-02-01T00:00:00"
["rsid"]=> int(49) ["status"]=> string(1) "D"
["web_user_id"]=> string(1) "0" } } } }
I am calling it like
$result = call_search_existing_manufacturer();
$rows = array();
foreach ($result->RPMHeader as $data)
{
$rows[] = array(
$data->reg_no,
$data->opening_inventory_weight,
$data->status
);
}
But its not working. Any Idea what am I missing? Thank you in advance
I think it should be
$result = call_search_existing_manufacturer();
$rows = array();
foreach ($result->headers->RPMHeader as $data)
{
$rows[] = array(
$data->reg_no,
$data->opening_inventory_weight,
$data->status
);
}
Your result dump isn't esay too read, so I may be wrong, but it looks like RPMHeader is part of headers field, so you should access it like
$result->headers->RPMHeader