I am working on a API project where i get an XML Object as a response. The response can contain one or more products in the NewOrder object(below).However when i try to display the info using a foreach loop it breaks if the only has one entry. i guess it is because the index [0] does not exist in the object.how can i through the xml object and display since there is no [0] i the object. OR how do i add the index [0] in the object.
object(stdClass)#49 (1) {
["NewOrder"] => object(stdClass)#50 (12) {
["BTN"] => string(10) "XXXXXXXXXXXXXXXxx"
["PreOrderTransactionId"] => string(22) "XXXXXXXX"
["PartnerOrderId"] => string(17) "XXXXXXXXXXX"
["QwestOrderId"] => string(9) "N57395699"
["SalesCode"] => string(7) "XXXXXXXX"
["OrderStatus"] => string(7) "Pending"
["OrderStatusCode"] => string(4) "OPEN"
["OrderStatusSourceSystem"] => string(5) "CPLUS"
["OrderStatusMessage"] => string(0) ""
["OrderStatusDate"] => string(10) "2013-12-09"
["OrderStatusTime"] => string(8) "08:02:30"
["ProductFamily"] => array(3) {
[0] => object(stdClass)#51 (2) {
["ProductFamilyName"] => string(7) "BUNDLE+"
["ProductLines"] => object(stdClass)#52 (3) {
["WTN"] => string(10) "3033689919"
["AppointmentDate"] => string(10) "2013-12-20"
["Products"] => object(stdClass)#53 (5) {
["ProductName"] => string(36) "INTERNET 100+ MBPS & HOME PHONE PLUS"
["Usoc"] => string(5) "BBBVC"
["Quantity"] => string(1) "1"
["Action"] => string(1) "I"
["Status"] => string(4) "OPEN"
}
}
}
}
}
}
I have tried the following but it didn't work:
if (!is_array($this->Orders->NewOrder)) {
$this->Order->NewOrder = array($this->Orders->NewOrder["NewOrder"]);
}
foreach ($this->Orders->NewOrder as $order){?>
I am getting the following error:
Fatal error: Cannot use object of type stdClass as array in
I think your NewOrder is only an array if it contains more than one object. Use something like this before your loop:
if (!is_array(yourObject["NewOrder"])) {
yourObject["NewOrder"] = array(yourObject["NewOrder"]);
}
The SoapClient has an option that always creates the array, even if here is only one element.
return new SoapClient(
'...',
array(
'location' => '...',
/.../
'features' => SOAP_SINGLE_ELEMENT_ARRAYS
)
);
Related
I have a multidimensional array
output_querys = array(2) { [0]=> array(5) { ["ID"]=> string(1) "2" ["Code"]=> string(6) "AWS001" ["BCode"]=> string(4) "B001" ["Des"]=> string(4) "loan" ["subCat"]=> string(3) "SWA" } [1]=> array(5) { ["ID"]=> string(1) "4" ["Code"]=> string(6) "AWS002" ["BCode"]=> string(4) "B002" ["Des"]=> string(3) "tax" ["subCat"]=> string(3) "PJA" } }
I want to get this to a variable and then save into dB.I have tried all the methods that has mentioned in related problems nothing worked.
Errors I got
1.trying non-object
2.Illegal string offset
what I was trying to do is
foreach($output_querys as $output_query){
$Pv_ID = $output_query->ID;
$Pv_Code = $output_query->Code;
$Pv_Description = $output_query->Des;
$data_final = array(id => $Pv_ID, code => $Pv_Code, des => $Pv_Description);
}
db->insert(abc, $data_final);
what is the problem here??.even i tried with json encode and still shows the "Illegal string offset"
$Pv_ID= json_decode($output_query['ID']);
$Pv_Code = json_encode($output_query['Code'],true);
$Pv_Description = json_encode($output_query['Des'],true);
I tried your given array and code
<?php
$output_querys = array(
0 => array(
"ID" => "2",
"Code" => "AWS001",
"BCode" => "B001",
"Des" => "loan",
"subCat" => "SWA"
),
1 => array(
"ID" => "4",
"Code" => "AWS002",
"BCode" => "B002",
"Des" => "tax",
"subCat" => "PJA"
)
);
foreach($output_querys as $output){
print_r($output['Code']); echo "\n";
}
?>
It is working fine on my end. Here's the working example
I think you need to do this as well, if you want to insert 1 by 1 then move the insert in foreach loop
foreach($output_querys as $output_query){
$Pv_ID = $output_query->ID;
$Pv_Code = $output_query->Code;
$Pv_Description = $output_query->Des;
$data_final = array(id => $Pv_ID, code => $Pv_Code, des => $Pv_Description);
db->insert(abc, $data_final);
}
I have this array:
Array ( ["id"] => 2015020052 ["gs"] => 5 ["ts"] => "THURSDAY 10/15"
["tsc"] => "final" ["bs"] => "FINAL" ["bsc"] => "final"
["atn"] => "Chicago" ["atv"] => "blackhawks" ["ats"] => "1"
["atc"] => "" ["htn"] => "Washington" ["htv"] => "capitals"
["hts"] => "4" ["htc"] => "winner" ["pl"] => true ["rl"] => true
["vl"] => true ["gcl"] => true ["gcll"] => true ["ustv"] => ""
["catv"] => "" )
I am trying to get particular values, like home team and away team and the scores, but I cannot get the values.
I am trying this:
echo "away team is ". $array['atv'];
But i just get
away team is
What am i missing????
var_dump gives me this:
Array vs array(21) { [""id""]=> string(10) "2015020051"
[""gs""]=> string(1) "5" [""ts""]=> string(16) ""THURSDAY 10/15"
[""tsc""]=> string(7) ""final"" [""bs""]=> string(7) ""FINAL""
[""bsc""]=> string(7) ""final"" [""atn""]=> string(8) ""Ottawa""
[""atv""]=> string(10) ""senators"" [""ats""]=> string(3) ""0""
[""atc""]=> string(2) """" [""htn""]=> string(12) ""Pittsburgh""
[""htv""]=> string(10) ""penguins"" [""hts""]=> string(3) ""2""
[""htc""]=> string(8) ""winner"" [""pl""]=> string(4) "true"
[""rl""]=> string(4) "true" [""vl""]=> string(4) "true"
[""gcl""]=> string(4) "true" [""gcll""]=> string(4) "true"
[""ustv""]=> string(2) """" [""catv""]=> string(2) """" }
I was also facing the same problem.
Problem:
The array was retrieved in Database [saved as a JSON encoded variable].
I was not getting the array element by key like $arr['key']
Solution:
Tried everything, no success.
Lastly, tried json_decode() and json_encode().
$arr = json_decode(json_encode($arr), TRUE);
Note that second parameter TRUE is very important. Otherwise, you will get object returned.
And it worked like charm.
You are not doing a correct associative array, this must be like this:
<?php
$array = [
'id' => 2015020052,
'gs' => 5,
'ts' => "THURSDAY 10/15",
'tsc' => "final",
'bs' => "FINAL",
'bsc' => "final",
'atn' => "Chicago",
...
];
Now you can get the value:
echo "away team is ". $array['atv'];
Here is the Demo
I seem to be having mental issues when it comes to arrays in php. I am not sure why, How ever my array looks like this:
$elementOptions = array(
array(
'type' => 'Text',
'name' => 'test' ,
'isRequired' => true,
'attributes' => array(
'placeholder' => 'content'
),
'subFormName' => 'content'
);
I have a for each loop as such:
foreach ($options as $key => $value) {
if (is_array($value)) {
//do something else
} else {
//do something
}
}
The issue is, if I do a var dump inside the if(isarray()){} I get the following back:
array(1) {
["placeholder"]=>
string(7) "content"
}
array(4) {
["type"]=>
string(4) "Text"
["name"]=>
string(4) "test"
["isRequired"]=>
bool(true)
["attributes"]=>
array(1) {
["placeholder"]=>
string(7) "content"
}
}
now the issue is - I do not want the following in that var dump:
array(1) {
["placeholder"]=>
string(7) "content"
}
I am not sure, based on the "data structure" above, how this 'placeholder' => 'content' is considered an array.....in either case I do not want it as part of the arrays that are var dumped.... it should just be the second array in that var dump coming back.
And that's where you guys come in, why is the place holder coming back as an array when it shouldn't (TMK - to my knowledge).
"placeholder"=>"content" isn't considered an array or coming back as an array, it's a pair in an array that you've called "attributes"
array(4) {
["type"]=>
string(4) "Text"
["name"]=>
string(4) "test"
["isRequired"]=>
bool(true)
["attributes"]=> // this is your array
array(1) { // contents of this array are...
["placeholder"]=> // this is a key
string(7) "content" // this is a variable
} // array ends
}
I'm trying to accomplish something very similar to what this user was doing Here.
I followed the answer, but I could not get it working. Inside the Active directory, my memberOf field looks like this:
CN=$VPN Users,CN=Users,DC=iai,DC=pri,CN=$ITAR,CN=Users,DC=iai,DC=pri,CN=allsubscribers,CN=Users,DC=iai,DC=pri
My Filter that works is:
(&(objectCategory=person)(sAMAccountName=$p_username))
I'm trying to get the following to work:
(&(objectCategory=person)(sAMAccountName=$p_username)(memberOf=CN=$ITAR))
I have tried adding the full DN which is
CN=Users,DC=iai,DC=pri
to my filter as well, but I get:
array(1) { ["count"]=> int(0) }
as my response.
I'm using ldap 3
This is the partial Working authentication code written in php:
$login = ldap_bind( $url, "username#somedomain", $password );
$attributes = array("displayname", "mailnickname");
$filter = "(&(objectCategory=person)(sAMAccountName=$username))";
$result = ldap_search($url, "CN=Users,DC=iai,DC=pri", $filter, $attributes);
$entries = ldap_get_entries($url, $result);
What am I doing wrong?
Code Result From #DaveRandom
First Var dump:
string(49) "(&(objectCategory=person)(sAMAccountName=rmoser))"
array(2) {
["count"] => int(1)
[0] => array(8) {
["displayname"] => array(2) {
["count"] => int(1)
[0] => string(10) "Ryan Moser"
}
[0] => string(11) "displayname"
["memberof"] => array(4) {
["count"] => int(3)
[0] => string(36) "CN=$VPN Users,CN=Users,DC=iai,DC=pri"
[1] => string(31) "CN=$ITAR,CN=Users,DC=iai,DC=pri"
[2] => string(40) "CN=allsubscribers,CN=Users,DC=iai,DC=pri"
}
[1]=> string(8) "memberof"
["mailnickname"] => array(2) {
["count"] => int(1)
[0] => string(6) "rmoser"
}
[2] => string(12) "mailnickname"
["count"] => int(3)
["dn"] => string(36) "CN=Ryan Moser,CN=Users,DC=iai,DC=pri"
}
}
bool(false)
Second var_dump:
string(70) "(&(objectCategory=person)(sAMAccountName=rmoser)(memberof=*CN=$ITAR*))"
array(1) {
["count"] => int(0)
}
LDAP filters look for an exact match.
In order to match CN=$ITAR anywhere in a value, you will need to surround it with the filter wildcard character *.
Try this filter:
(&(objectCategory=person)(sAMAccountName=$p_username)(memberOf=*CN=$ITAR*))
Also don't forget that $ITAR is a valid variable name in PHP, so if you place that filter string in double quotes (which you would need to in order for $p_username to be interpolated) PHP will attempt to interpolate $ITAR as a variable as well, find that it (probably) doesn't exist and the end result will be that it gets stripped from the string.
$filter = "(&(objectCategory=person)(sAMAccountName=$p_username)(memberOf=*CN=\$ITAR*))";
A useful link for any question concerning a dynamic filter built with PHP is this.
How can I get the follow thing done.
The follow array I have:
array(2) { [0] => array(3) {
["id"] => string(1) "5"
["avatar"] => string(15) "4e0d886ee9ed3_n"
["username"] => string(5) "testuser1"}
[1] => array(3) {
["id"] => string(1) "1"
["avatar"] => string(15) "4e25bc58b6789_w"
["username"] => string(6) "testuser2"
}
}
I want to create a array with just one user in it, but it has to be random. It can be like
user with id=5, id=1 or a hole other user (when there are more users).
Did you try, something like:
$rand = array_rand($your_array);
http://fr2.php.net/manual/en/function.array-rand.php
Try using array_rand().
$randomKey = array_rand($yourArray);
$randomUserId = $yourArray[$randomKey]['id'];
Simple.
$rand_user = array_rand($your_array);
PHP Manual: array_rand