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
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'];
}
I need some more help regarding PHP Arrays and the issue I am having. I have an array like this: -
array(2) {
[0]=>
array(2) {
[0]=>
array(2) {
["count"]=>
string(3) "100"
["id"]=>
int(46)
}
[1]=>
array(2) {
["count"]=>
string(3) "300"
["id"]=>
int(53)
}
}
[1]=>
array(1) {
[0]=>
array(2) {
["count"]=>
string(3) "200"
["id"]=>
int(46)
}
}
}
However, I would like it to look more like this as array: -
array(2) {
[0]=>
array(2) {
["count"]=>
string(3) "300" <--- This has been added from Array 1 and 2
["id"]=>
int(46)
}
[1]=>
array(2) {
["count"]=>
string(3) "300"
["id"]=>
int(53)
}
}
Basically if the same id is in both areas I want the count number to be added to each other but if it's not then it needs to just be left alone and included in the array.
I have used a number of array functions such as array_merge and array_push but I am running out of ideas of how this could work. I have also started working on a foreach with if statements but I just got myself completely confused. I just need a second pair of eyes to look at the issue and see howe it can be done.
Thanks again everyone.
Should work with something like this:
$idToCountArray = array(); //temporary array to store id => countSum
array_walk_recursive($inputArray, function($value,$key) { //walk each array in data structure
if(isset(value['id']) && isset($value['count'])) {
//we have found an entry with id and count:
if(!isset($idToCountArray[$value['id']])) {
//first count for id => create initial count
$idToCountArray[$value['id']] = intval($value['count']);
} else {
//n'th count for id => add count to sum
$idToCountArray[$value['id']] += intval($value['count']);
}
}
});
//build final structure:
$result = array();
foreach($idToCountArray as $id => $countSum) {
$result[] = array('id' => $id, 'count' => ''.$countSum);
}
Please note that I have not testet the code and there is probably a more elegant/performant solution.
You could use something like this:
$end_array = array();
function build_end_array($item, $key){
global $end_array;
if (is_array($item)){
if( isset($item["id"])){
if(isset($end_array[$item["id"]]))
$end_array[$item["id"]] = $end_array[$item["id"]] + $item["count"]*1;
else
$end_array[$item["id"]] = $item["count"]*1;
}
else {
array_walk($item, 'build_end_array');
}
}
}
array_walk($start_array, 'build_end_array');
Here is a fiddle.
Thank you ever so much everyone. I actually worked it by doing this: -
$fullArray = array_merge($live, $archive);
$array = array();
foreach($fullArray as $key=>$value) {
$id = $value['id'];
$array[$id][] = $value['count'];
}
$result = array();
foreach($array as $key=>$value) {
$result[] = array('id' => $key, 'count' => array_sum($value));
}
return $result;
I need to get the objects information for "label", "name" where value=true in a PHP variable and not were value=false.
How is this done with this JSON array?
If I make a var_dump of the JSON I get this:
array(8) {
[0]=>
object(stdClass)#8 (3) {
["label"]=>
string(4) "Name"
["name"]=>
string(7) "txtName"
["value"]=>
bool(true)
}
[1]=>
object(stdClass)#9 (3) {
["label"]=>
string(6) "E-mail"
["name"]=>
string(8) "txtEmail"
["value"]=>
bool(true)
}
[2]=>
object(stdClass)#10 (3) {
["label"]=>
string(12) "Phone Number"
["name"]=>
string(8) "txtPhone"
["value"]=>
bool(false)
}
[3]=>
object(stdClass)#11 (3) {
["label"]=>
string(19) "Mobile Phone Number"
["name"]=>
string(14) "txtMobilePhone"
["value"]=>
bool(false)
}
}
$arr = array();
$i = 0;
foreach($json as $key => $items) {
if($items->value == true) {
$arr[$i]['label'] = $items->label;
$arr[$i]['name'] = $items->name;
$i++;
}
}
You can decode it as an object or an array, in this example I use an array.
First you want to take the JSON encoded information and decode it into a PHP array, you can use json_decode() for this:
$data = json_decode($thejson,true);
//the Boolean argument is to have the function return an array rather than an object
Then you can loop through it as you would a normal array, and build a new array containing only elements where 'value' matches your needs:
foreach($data as $item) {
if($item['value'] == true) {
$result[] = $item;
}
}
You then have the array
$result
at your disposal.
Simplification of the suggestions proposed by users JohnnyFaldo and som:
$data = json_decode($thejson, true);
$result = array_filter($data, function($row) {
return $row['value'] == true;
});
This is one of those that should be easy. I'm not even sure if "subset" is the right way to describe it.
My initial array looks like this:
array(3) { [0]=> array(5) { ["id"]=> string(1) "1" ["claim_id"]=> string(1) "1" ["price"]=> string(2) "50" ["date"]=> string(19) "2013-05-15 01:58:48" ["created"]=> string(19) "2013-05-15 01:58:48" } [1]=> array(5) { ["id"]=> string(2) "11" ["claim_id"]=> string(1) "1" ["price"]=> string(2) "45" ["date"]=> string(19) "2013-05-15 03:34:59" ["created"]=> string(19) "2013-05-15 03:37:01" } [2]=> array(5) { ["id"]=> string(2) "25" ["claim_id"]=> string(1) "1" ["price"]=> string(2) "50" ["date"]=> string(19) "2013-05-15 22:47:46" ["created"]=> string(19) "2013-05-15 22:52:02" } }
I'd ultimately like to end up with just the date and price values, swap them so that date is first in the array, reformat the date, and convert it to a json array that looks something like this:
[{"date":"Mar. 15","price":"50"},{"date":"Mar. 15","price":"45"},{"date":"Mar. 15","price":"50"}]
I ran a foreach statement to get at the data and reformat the date, then went out of my league with splice, unset, and other functions that took me in the wrong direction. Any ideas?
Try this
$array = array();
for($i = 0 ; $i<count($your_array);$i++) {
$a=array();
$a['date'] = $your_array[$i]['date'];
$a['price'] = date("F .j",strtotime($your_array[$i]["date"]));
array_push($array,$a);
}
json_encode($array);
Output
[{"date":"Mar. 15","price":"50"},{"date":"Mar. 15","price":"45"},{"date":"Mar. 15","price":"50"}]
Codepad
This should get you there
$ret = array();
foreach ($source as $rec){
$ret[] = array("date"=>$rec["date"], "price"=>$rec["price"]);
}
$json = json_encode($ret);
Create a quick function to create another array with only selected elements:
function datePriceToJson($array) {
$a = array();
foreach($array as $i => $a) {
$a[] = array("date" => $a['date'], "price" => $a['price']); //Creae a new array based on the one you want
}
return json_encode($a);
}
datePriceToJson($array);
You need to reconstruct array using correct foreach and the use json_encode to get the json
$req_array = array();
foreach($youArray as $value)
{
$temp = array();
$temp['date'] = $value['date'];
$temp['price'] = $value['price'];
$req_array[] = $temp;
}
$json = json_encode($req_array);
echo $json;
see demo
Hope it helps you
$infos =array(
array("id" =>"1","price"=>"50","date"=>"2013-05-15 01:58:48"),
array("id" =>"2","price"=>"55","date"=>"2013-06-15 01:58:48")
);
$i=0;
foreach ($infos as $info )
{
$infos[$i]["date"]= date("F .j",strtotime($info["date"]));
$i++;
}
echo json_encode($infos);
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);