Access object without foreach - php

object(Zend_Paginator)#88 (11) {
["_cacheEnabled":protected]=>
bool(true)
["_adapter":protected]=>
object(Zend_Paginator_Adapter_Array)#89 (2) {
["_array":protected]=>
array(150) {
[0]=>
array(19) {
["location"]=>
string(15) "New York"
["id"]=>
string(3) "186"
["user_id"]=>
string(1) "2"
["date"]=>
string(10) "1367893034"
["attachment"]=>
string(1) "0"
["content"]=>
string(4) "nhzu"
["first_name"]=>
string(6) "John"
["last_name"]=>
string(6) "Doe"
["thumb_id"]=>
NULL
["thumb_user_id"]=>
NULL
["file_name"]=>
NULL
["folder_name"]=>
NULL
["server_key"]=>
NULL
["type"]=>
NULL
["photo_file_name"]=>
NULL
["photo_folder_name"]=>
NULL
["photo_server_key"]=>
NULL
["photo_id"]=>
NULL
["count_total"]=>
NULL
}
[1]=>
How do I acces the conectent of id without a foreach, I need only the first one 0 ? any ideas?

$item = $paginator->getItem(1);
See http://framework.zend.com/svn/framework/standard/tags/release-1.12.3/library/Zend/Paginator.php the public function getItem function definition

Related

Change an object array into multidimensional array and print

I have a multidimensional array. I am retrieving this from UPS RateInTransit API. I am receiving this in an object. Then i convert it into a simple array.
i have received the data into json from UPS. So i converted it into php array by Json_encode and json_decode.
After that from Object array to simple array, so i found this.
object(Ups\Entity\RateResponse)#81 (1) {
["RatedShipment"]=>
array(1) {
[0]=>
object(Ups\Entity\RatedShipment)#82 (12) {
["Service"]=>
object(Ups\Entity\Service)#83 (3) {
["Description"]=>
NULL
["code":"Ups\Entity\Service":private]=>
string(2) "03"
["description":"Ups\Entity\Service":private]=>
NULL
}
["RateShipmentWarning"]=>
string(56) "Your invoice may vary from the displayed reference rates"
["BillingWeight"]=>
object(Ups\Entity\BillingWeight)#89 (2) {
["UnitOfMeasurement"]=>
object(Ups\Entity\UnitOfMeasurement)#91 (4) {
["Code"]=>
string(3) "LBS"
["Description"]=>
NULL
["code":"Ups\Entity\UnitOfMeasurement":private]=>
string(3) "LBS"
["description":"Ups\Entity\UnitOfMeasurement":private]=>
NULL
}
["Weight"]=>
string(4) "32.0"
}
["TransportationCharges"]=>
object(Ups\Entity\Charges)#84 (5) {
["CurrencyCode"]=>
string(3) "USD"
["MonetaryValue"]=>
float(43.17)
["Code"]=>
NULL
["Description"]=>
NULL
["SubType"]=>
NULL
}
["ServiceOptionsCharges"]=>
object(Ups\Entity\Charges)#86 (5) {
["CurrencyCode"]=>
string(3) "USD"
["MonetaryValue"]=>
float(0)
["Code"]=>
NULL
["Description"]=>
NULL
["SubType"]=>
NULL
}
["TotalCharges"]=>
object(Ups\Entity\Charges)#87 (5) {
["CurrencyCode"]=>
string(3) "USD"
["MonetaryValue"]=>
float(43.17)
["Code"]=>
NULL
["Description"]=>
NULL
["SubType"]=>
NULL
}
["GuaranteedDaysToDelivery"]=>
object(stdClass)#72 (0) {
}
["ScheduledDeliveryTime"]=>
object(stdClass)#73 (0) {
}
["RatedPackage"]=>
array(1) {
[0]=>
object(Ups\Entity\RatedPackage)#88 (5) {
["Weight"]=>
string(3) "4.0"
["BillingWeight"]=>
object(Ups\Entity\BillingWeight)#95 (2) {
["UnitOfMeasurement"]=>
object(Ups\Entity\UnitOfMeasurement)#97 (4) {
["Code"]=>
string(3) "LBS"
["Description"]=>
NULL
["code":"Ups\Entity\UnitOfMeasurement":private]=>
string(3) "LBS"
["description":"Ups\Entity\UnitOfMeasurement":private]=>
NULL
}
["Weight"]=>
string(4) "32.0"
}
["TransportationCharges"]=>
object(Ups\Entity\Charges)#85 (5) {
["CurrencyCode"]=>
string(3) "USD"
["MonetaryValue"]=>
float(43.17)
["Code"]=>
NULL
["Description"]=>
NULL
["SubType"]=>
NULL
}
["ServiceOptionsCharges"]=>
object(Ups\Entity\Charges)#92 (5) {
["CurrencyCode"]=>
string(3) "USD"
["MonetaryValue"]=>
float(0)
["Code"]=>
NULL
["Description"]=>
NULL
["SubType"]=>
NULL
}
["TotalCharges"]=>
object(Ups\Entity\Charges)#93 (5) {
["CurrencyCode"]=>
string(3) "USD"
["MonetaryValue"]=>
float(43.17)
["Code"]=>
NULL
["Description"]=>
NULL
["SubType"]=>
NULL
}
}
}
["SurCharges"]=>
array(0) {
}
["TimeInTransit"]=>
NULL
["NegotiatedRates"]=>
NULL
}
}
}
But i am unable to access ['RatedShipment]['BillingWeight']['Weight'].
Where did i write a wrong code???
What do you think to use $arr = (array) $obj;?
Because it still looks like an object.
Or you could access $obj->RatedShipment->BillingWeight->Weight?

How to get specific Array value from an Array in Laravel

I'd like to get a specific array data from an array I've get from this code :
$user = $request->instance()->query('user');
var_dump($user);exit;
And the result is :
array(1) { ["user"]=> object(App\Models\User)#332 (27) {
["fillable":protected]=> array(5) { [0]=> string(4) "name" [1]=>
string(8) "username" [2]=> string(5) "email" [3]=> string(8)
"password" [4]=> string(5) "token" } ["hidden":protected]=> array(2) {
[0]=> string(8) "password" [1]=> string(14) "remember_token" }
["casts":protected]=> array(1) { ["email_verified_at"]=> string(8)
"datetime" } ["connection":protected]=> string(5) "mysql"
["table":protected]=> string(5) "users" ["primaryKey":protected]=>
string(2) "id" ["keyType":protected]=> string(3) "int"
["incrementing"]=> bool(true) ["with":protected]=> array(0) { }
["withCount":protected]=> array(0) { } ["perPage":protected]=> int(15)
["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false)
["attributes":protected]=> array(15) { ["id"]=> int(3) ["name"]=>
string(14) "Manchesteriyah" ["username"]=> string(14) "manchesteriyah"
["email"]=> string(24) "manchesteriyah#gmail.com" ["facebook"]=>
string(37) "https://web.facebook.com/xkionna" ["phone"]=> NULL
["email_verified_at"]=> NULL ["password"]=> string(60)
"$2y$10$IrqHE1JfyH0bJ0XD/Hjy.efLg95y/buTIir0PuXcOqDb1yCSS69Oe"
["image"]=> NULL ["description"]=> NULL ["role"]=> string(1) "1"
["token"]=> string(20) "ymsJxEtFnxdPBWYwlYFw" ["member_expiration"]=>
string(19) "2019-07-08 20:33:29" ["created_at"]=> string(19)
"2019-06-08 20:30:25" ["updated_at"]=> string(19) "2019-06-08
20:33:29" } ["original":protected]=> array(15) { ["id"]=> int(3)
["name"]=> string(14) "Manchesteriyah" ["username"]=> string(14)
"manchesteriyah" ["email"]=> string(24) "manchesteriyah#gmail.com"
["facebook"]=> string(37) "https://web.facebook.com/xkionna"
["phone"]=> NULL ["email_verified_at"]=> NULL ["password"]=>
string(60)
"$2y$10$IrqHE1JfyH0bJ0XD/Hjy.efLg95y/buTIir0PuXcOqDb1yCSS69Oe"
["image"]=> NULL ["description"]=> NULL ["role"]=> string(1) "1"
["token"]=> string(20) "ymsJxEtFnxdPBWYwlYFw" ["member_expiration"]=>
string(19) "2019-07-08 20:33:29" ["created_at"]=> string(19)
"2019-06-08 20:30:25" ["updated_at"]=> string(19) "2019-06-08
20:33:29" } ["changes":protected]=> array(0) { } ["dates":protected]=>
array(0) { } ["dateFormat":protected]=> NULL ["appends":protected]=>
array(0) { } ["dispatchesEvents":protected]=> array(0) { }
["observables":protected]=> array(0) { } ["relations":protected]=>
array(0) { } ["touches":protected]=> array(0) { } ["timestamps"]=>
bool(true) ["visible":protected]=> array(0) { }
["guarded":protected]=> array(1) { [0]=> string(1) "*" }
["rememberTokenName":protected]=> string(14) "remember_token" } }
What I'd like to get is the username value, I've tried some solution like this :
$user->username
but it give me error like this :
Trying to get property 'username' of non-object
How to fix this? Thanks for attention.
The $user variable seems to be holding an array containing 1 element, not the User itself. You must first retrieve the User object from the array and then access the username attribute.
$user = $request->instance()->query('user')['user'];
var_dump($user->username); exit;

Cake PHP how to add array to find fields

I'm using cakephp 2.x and I would like to get certain element as field from my model. the problem is with my structure. If I use find all then I recieve this:
array(9) {
[0]=> ...
[1]=>
array(7) {
["Person"]=>
array(18) {
["id"]=>
int(2)
["imie"]=>
string(6) "STEFAN"
["nazwisko"]=>
string(8) "ŚMIAŁY"
["tel"]=>
string(13) "+55 648989748"
["mob"]=>
string(13) "+87 489747489"
["email"]=>
string(25) "HSDJKAHSDJK#HSDFJASHF.CIJ"
["tytul_naukowy"]=>
string(7) "TEZ NIE"
["data_od"]=>
string(10) "2017-02-16"
["data_do"]=>
NULL
["object_id"]=>
int(-1)
["object_type_id"]=>
int(2)
["duty_id"]=>
int(28)
["created"]=>
string(22) "2017-02-16 13:22:32+01"
["modified"]=>
string(22) "2017-02-16 13:22:32+01"
["deleted"]=>
int(0)
["additional_email"]=>
string(17) "UDIHAS#HFSDKFH.OK"
["description"]=>
string(4) "OPIS"
["displayName"]=>
string(15) "STEFAN ŚMIAŁY"
}
["Duty"]=>
array(3) {
["id"]=>
int(28)
["nazwa"]=>
string(20) "KOORDYNATOR RECEPCJI"
["atrybut"]=>
string(1) "W"
}
["ObjectType"]=>
array(3) {
["id"]=>
int(2)
["nazwa"]=>
string(4) "Obks"
["model"]=>
string(3) "Obk"
}
["Cro"]=>
array(12) {
["id"]=>
NULL
["created"]=>
NULL
["nazwa"]=>
NULL
["tel"]=>
NULL
["fax"]=>
NULL
["email"]=>
NULL
["www"]=>
NULL
["modified"]=>
NULL
["deleted"]=>
NULL
["cro_type_id"]=>
NULL
["displayName"]=>
NULL
["objectTypeIdWithId"]=>
NULL
}
["Attachment"]=>
array(0) {
}
["Specialization"]=>
array(0) {
}
["Obk"]=>
array(1) {
[0]=>
array(13) {
["id"]=>
int(2)
["nazwa"]=>
string(10) "OBK Z ID 2"
["tel"]=>
string(13) "+48 123456890"
["fax"]=>
string(13) "+48 123456789"
["email"]=>
string(15) "EMAIL#EMAIL.COM"
["www"]=>
string(11) "WWW.WWW.WWW"
["modified"]=>
string(22) "2017-03-20 15:11:31+01"
["created"]=>
string(22) "2014-05-12 21:50:17+02"
["deleted"]=>
int(0)
["local_order"]=>
int(3)
["displayName"]=>
string(10) "OBK Z ID 2"
["objectTypeIdWithId"]=>
string(3) "2_2"
["PeopleObk"]=>
array(3) {
["id"]=>
int(1)
["person_id"]=>
int(2)
["obk_id"]=>
int(2)
}
}
}
}
[2]=>
array(7) {
["Person"]=>
array(18) {
["id"]=>
int(3)
["imie"]=>
string(5) "KAMIL"
["nazwisko"]=>
string(10) "STEFAŃSKI"
["tel"]=>
string(13) "+23 123123123"
["mob"]=>
string(13) "+21 412312312"
["email"]=>
string(16) "ASDASD#SDFDSF.LS"
["tytul_naukowy"]=>
string(3) "NIE"
["data_od"]=>
string(10) "2017-02-22"
["data_do"]=>
NULL
["object_id"]=>
int(-1)
["object_type_id"]=>
int(2)
["duty_id"]=>
int(26)
["created"]=>
string(22) "2017-02-22 16:14:32+01"
["modified"]=>
string(22) "2017-02-22 16:14:32+01"
["deleted"]=>
int(0)
["additional_email"]=>
string(22) "ADAM.2313#WEBIMPULS.PL"
["description"]=>
string(4) "TEST"
["displayName"]=>
string(16) "KAMIL STEFAŃSKI"
}
["Duty"]=>
array(3) {
["id"]=>
int(26)
["nazwa"]=>
string(41) "KOORDYNATOR OŚRODKÓW BADAŃ KLINICZNYCH"
["atrybut"]=>
string(1) "W"
}
["ObjectType"]=>
array(3) {
["id"]=>
int(2)
["nazwa"]=>
string(4) "Obks"
["model"]=>
string(3) "Obk"
}
["Cro"]=>
array(12) {
["id"]=>
NULL
["created"]=>
NULL
["nazwa"]=>
NULL
["tel"]=>
NULL
["fax"]=>
NULL
["email"]=>
NULL
["www"]=>
NULL
["modified"]=>
NULL
["deleted"]=>
NULL
["cro_type_id"]=>
NULL
["displayName"]=>
NULL
["objectTypeIdWithId"]=>
NULL
}
["Attachment"]=>
array(0) {
}
["Specialization"]=>
array(0) {
}
["Obk"]=>
array(1) {
[0]=>
array(13) {
["id"]=>
int(3)
["nazwa"]=>
string(10) "OBK Z ID 3"
["tel"]=>
string(13) "+48 123456890"
["fax"]=>
string(13) "+48 123456789"
["email"]=>
string(15) "EMAIL#EMAIL.COM"
["www"]=>
string(11) "WWW.WWW.WWW"
["modified"]=>
string(22) "2017-03-20 10:45:24+01"
["created"]=>
string(22) "2014-05-16 09:09:33+02"
["deleted"]=>
int(0)
["local_order"]=>
int(1)
["displayName"]=>
string(10) "OBK Z ID 3"
["objectTypeIdWithId"]=>
string(3) "2_3"
["PeopleObk"]=>
array(3) {
["id"]=>
int(2)
["person_id"]=>
int(3)
["obk_id"]=>
int(3)
}
}
}
}
And I want just to retrieve Person.id, Person.displayName and Obk.0.id. The two first are no problem, but wheneve I try to get Obk.0.id I'm getting error: SQLSTATE[42P01]: Undefined table: 7 ERROR: missing FROM for Table Obk.
How can I get something like that? I want to set the value in codition later, but it block my work.
In CakePHP 2 you can specify the fields you want returned by a find() query using the fields option like:-
$this->Foo->find('all', [
'fields' => ['Foo.id', 'Foo.name']
]);
This also works with contained models that have a belongsTo or hasOne relationship:-
$this->Foo->find('all', [
'fields' => ['Foo.id', 'Foo.name', 'Bar.name'],
'contain' => ['Bar']
]);
However, if the contained model is a hasMany or HABTM relationship you need to specify the fields of that model on the contain:-
$this->Foo->find('all', [
'fields' => ['Foo.id', 'Foo.name'],
'contain' => [
'Bar' => ['fields' => 'Bar.name']
]
]);
This is because CakePHP will be performing multiple SQL queries, so the contain needs to have the parameters for the query being run for the relevant model.

PHP: Extracting a value from a multidimensional array

In this array, I am attempting to extract the value int(2128) from the ["member_count"] key.
array(3) {
["total"]=>
int(1)
["data"]=>
array(1) {
[0]=>
array(17) {
["id"]=>
string(10) "999999999999"
["web_id"]=>
int(421097)
["name"]=>
string(21) "Name Of List"
["date_created"]=>
string(19) "2014-07-14 13:51:01"
["email_type_option"]=>
bool(false)
["use_awesomebar"]=>
bool(true)
["default_from_name"]=>
string(14) "Department Name"
["default_from_email"]=>
string(24) "user#email.com"
["default_subject"]=>
string(0) ""
["default_language"]=>
string(2) "en"
["list_rating"]=>
float(3.5)
["subscribe_url_short"]=>
string(23) "website/4545"
["subscribe_url_long"]=>
string(91) "website"
["beamer_address"]=>
string(47) "key goes here"
["visibility"]=>
string(3) "pub"
["stats"]=>
array(16) {
["member_count"]=>
int(2128)
["unsubscribe_count"]=>
int(20)
["cleaned_count"]=>
int(57)
["member_count_since_send"]=>
int(1)
["unsubscribe_count_since_send"]=>
int(9)
["cleaned_count_since_send"]=>
int(29)
["campaign_count"]=>
int(2)
["grouping_count"]=>
int(1)
["group_count"]=>
int(4)
["merge_var_count"]=>
int(6)
["avg_sub_rate"]=>
int(17)
["avg_unsub_rate"]=>
int(3)
["target_sub_rate"]=>
int(17)
["open_rate"]=>
float(38.672399892502)
["click_rate"]=>
float(14.932747735383)
["date_last_campaign"]=>
string(19) "2014-11-07 19:14:30"
}
["modules"]=>
array(0) {
}
}
}
["errors"]=>
array(0) {
}
}
Here is the PHP that I have so far but I'm unable to get just the value "int(2128)"
foreach($result as $row){
foreach($row[0] as $row1){
echo '<pre>';
var_dump($row1['member_count']);
echo '</pre>';
}
}
The Results I get:
string(1) "6"
NULL
string(1) "F"
string(1) "2"
NULL
NULL
string(1) "B"
string(1) "d"
string(0) ""
string(1) "e"
NULL
string(1) "h"
string(1) "h"
string(1) "u"
string(1) "p"
int(2128)
NULL
I greatly appreciate any help to narrow the results down to just the value "int(2128)".
Thanks.
If you only want the value 2128 use this:
$array['data'][0]['stats']['member_count']
($array is the name of your multidimensional array)

Array if contains value, unset

I got follow array ($json_output):
array(3) {
["ProductsSummary"]=>
array(4) {
["Records"]=>
int(500)
["TotalRecords"]=>
int(5720)
["TotalPages"]=>
int(12)
["CurrentPage"]=>
int(2)
}
["Products"]=>
array(500) {
[0]=>
array(10) {
["ProductId"]=>
int(1323819499)
["ShopId"]=>
int(1856)
["ProductName"]=>
string(21) "Fossil Creole JF84757"
["Deeplink2"]=>
string(0) ""
["Brand"]=>
NULL
["Manufacturer"]=>
string(6) "Fossil"
["Distributor"]=>
NULL
["EAN"]=>
string(13) "4048803717479"
["Keywords"]=>
NULL
["Properties"]=>
array(3) {
[0]=>
array(2) {
["PropertyName"]=>
string(12) "DeliveryTime"
["PropertyValue"]=>
string(1) "5"
}
[1]=>
array(2) {
["PropertyName"]=>
string(17) "MerchantArtNumber"
["PropertyValue"]=>
string(8) "85145452"
}
[2]=>
array(2) {
["PropertyName"]=>
string(6) "gender"
["PropertyValue"]=>
string(5) "Damen"
}
}
}
[1]=>
array(10) {
["ProductId"]=>
int(1323819505)
["ShopId"]=>
int(1856)
["ProductName"]=>
string(16) "SANSIBAR Armband"
["Deeplink2"]=>
string(0) ""
["Brand"]=>
NULL
["Manufacturer"]=>
string(8) "Sansibar"
["Distributor"]=>
NULL
["EAN"]=>
NULL
["Keywords"]=>
NULL
["Properties"]=>
array(3) {
[0]=>
array(2) {
["PropertyName"]=>
string(12) "DeliveryTime"
["PropertyValue"]=>
string(1) "5"
}
[1]=>
array(2) {
["PropertyName"]=>
string(17) "MerchantArtNumber"
["PropertyValue"]=>
string(8) "85189719"
}
[2]=>
array(2) {
["PropertyName"]=>
string(6) "gender"
["PropertyValue"]=>
string(5) "Herren"
}
}
}
I need to unset all Products which contains 'Herren' in Properties, so I tried:
<?php
foreach($json_output["Products"] as & $bla)
$check = $bla["Properties"][0]["PropertyValue"] . $bla["Properties"][1]["PropertyValue"] . $bla["Properties"][2]["PropertyValue"];
if (preg_match('/Herren/',$check))
{
unset($bla);
}
?>
But it's not working.
array_filter iterates for you and returns a filtered set of elements.
The callback function returns true if the element is to remain, and false if it is to be removed. json_encode converts the whole array into a string, strpos looks for the string Herren anywhere in that string. Since you require no regular expression, there's no need to use preg_match which is slower than strpos.
$array['Products']=array_filter($array['Products'], 'removeHerren');
function removeHerren($array){
return strpos(json_encode($array), 'Herren')===false;
}

Categories