Hello I am trying to print a specific value from an array of objects. I am trying to get a value from an array name $allPhotos with a object's property of "nme"'s value.
This is what im trying:
echo $allPhotos[0]["nme"];
this is what the array looks like:
var_dump($allPhotos);
array(2) {
[0]=> object(Photo)#1 (10) {
["product"]=> array(5) {
["PKG1"]=> string(4) "6500"
["PKG2"]=> string(4) "9500"
["8x10"]=> string(4) "1500"
["5x7"]=> string(3) "750"
["4x6"]=> string(3) "300"
}
["price"]=> NULL ["sku"]=> string(1) "1"
["nme"]=> string(5) "test1"
["dir"]=> string(51) "http://"
["status"]=> string(1) "1" ["gallery"]=> string(16) "Church Directory"
["galleryCover"]=> string(1) "0"
["family"]=> string(0) ""
["familyCover"]=> string(0) ""
}
[1]=> object(Photo)#2 (10) {
["product"]=> array(5) {
["PKG1"]=> string(4) "6500"
["PKG2"]=> string(4) "9500"
["8x10"]=> string(4) "1500"
["5x7"]=> string(3) "750"
["4x6"]=> string(3) "300"
}
["price"]=> NULL
["sku"]=> string(1) "2"
["nme"]=> string(5) "test2"
["dir"]=> string(51) "http://"
["status"]=> string(1) "1"
["gallery"]=> string(16) "Church Directory"
["galleryCover"]=> string(1) "0"
["family"]=> string(0) ""
["familyCover"]=> string(0) ""
}
}
Thanks in advance!
I believe echo $allPhotos[0]->nme; should work.
Related
first I'm looking help from any expert here for PHP array calling. I try using var_dump but still I can't call my data. I'm really a beginner and need a lot of help. Thanks before for read my question below...
<?php var_dump($this->product->customfieldsSorted); ?>
I use that and get this array list...
array(1) {
["normal"]=>
array(1) {
[0]=>
object(stdClass)#222 (36) {
["virtuemart_custom_id"]=>
string(2) "21"
["custom_parent_id"]=>
string(1) "0"
["virtuemart_vendor_id"]=>
string(1) "1"
["custom_jplugin_id"]=>
string(1) "0"
["custom_element"]=>
string(0) ""
["admin_only"]=>
string(1) "0"
["custom_title"]=>
string(5) "Slide"
["show_title"]=>
string(1) "0"
["custom_tip"]=>
string(0) ""
["custom_value"]=>
string(0) ""
["custom_desc"]=>
string(0) ""
["field_type"]=>
string(1) "M"
["is_list"]=>
string(1) "0"
["is_hidden"]=>
string(1) "1"
["is_cart_attribute"]=>
string(1) "0"
["is_input"]=>
string(1) "0"
["layout_pos"]=>
string(0) ""
["custom_params"]=>
string(26) "width="1024"|height="400"|"
["shared"]=>
string(1) "0"
["published"]=>
string(1) "1"
["ordering"]=>
string(1) "0"
["virtuemart_customfield_id"]=>
string(3) "110"
["virtuemart_product_id"]=>
string(2) "95"
["customfield_value"]=>
string(2) "15"
["customfield_price"]=>
NULL
["customfield_params"]=>
string(25) "width="200"|height="200"|"
["fpublished"]=>
string(1) "0"
["override"]=>
string(1) "0"
["disabler"]=>
string(1) "0"
["_varsToPushParamCustom"]=>
array(2) {
["width"]=>
array(2) {
[0]=>
string(3) "200"
[1]=>
string(6) "string"
}
["height"]=>
array(2) {
[0]=>
string(3) "200"
[1]=>
string(6) "string"
}
}
["_varsToPushParamCustomField"]=>
array(2) {
["width"]=>
array(2) {
[0]=>
string(3) "200"
[1]=>
string(6) "string"
}
["height"]=>
array(2) {
[0]=>
string(3) "200"
[1]=>
string(6) "string"
}
}
["_varsToPushParam"]=>
array(2) {
["width"]=>
array(2) {
[0]=>
string(3) "200"
[1]=>
string(6) "string"
}
["height"]=>
array(2) {
[0]=>
string(3) "200"
[1]=>
string(6) "string"
}
}
["width"]=>
string(3) "200"
["height"]=>
string(3) "200"
["_xParams"]=>
string(18) "customfield_params"
["display"]=>
string(101) "<img src="/angga/images/stories/virtuemart/product/resized/marinecap_200x200.jpg" alt="marinecap" />"
}
}
Nah I want to get the:
["display"]=>
string(101) "<img src="/angga/images/stories/virtuemart/product/resized/marinecap_200x200.jpg" alt="marinecap" />"
using this:
<?php echo $this->product->customfieldsSorted->display;?>
and got nothing. Can anyone help? I have no clue. None at all. Thanks before :)
$this->product->customfieldsSorted is an array with the key "normal", you can get corresponding value: $this->product->customfieldsSorted['normal'].
$this->product->customfieldsSorted['normal'] is an array with the key 0, get its value: $this->product->customfieldsSorted['normal'][0].
$this->product->customfieldsSorted['normal'][0] is an object of stdClass, you can get value of the property display using $this->product->customfieldsSorted['normal'][0]->display.
I have the array like this:
} ["items":"Jcart":private]=> array(3) {
[0]=>
string(1) "3"
[1]=>
string(1) "2"
[2]=>
string(7) "ABC-123" }
How to get the "items" values in a php variable?
///////
Te complete object is:
object(Jcart)#1 (8) {
["config"]=>
array(12) {
["jcartPath"]=>
string(6) "jcart/"
["checkoutPath"]=>
string(12) "checkout.php"
["item"]=>
array(6) {
["id"]=>
string(10) "my-item-id"
["name"]=>
string(12) "my-item-name"
["price"]=>
string(13) "my-item-price"
["qty"]=>
string(11) "my-item-qty"
["url"]=>
string(11) "my-item-url"
["add"]=>
string(13) "my-add-button"
}
["paypal"]=>
array(5) {
["id"]=>
string(38) "seller_1282188508_biz#conceptlogic.com"
["https"]=>
bool(true)
["sandbox"]=>
bool(false)
["returnUrl"]=>
string(0) ""
["notifyUrl"]=>
string(0) ""
}
["currencyCode"]=>
string(3) "USD"
["csrfToken"]=>
bool(false)
["text"]=>
array(14) {
["cartTitle"]=>
string(13) "Shopping Cart"
["singleItem"]=>
string(4) "Item"
["multipleItems"]=>
string(5) "Items"
["subtotal"]=>
string(8) "Subtotal"
["update"]=>
string(6) "update"
["checkout"]=>
string(8) "checkout"
["checkoutPaypal"]=>
string(20) "Checkout with PayPal"
["removeLink"]=>
string(6) "remove"
["emptyButton"]=>
string(5) "empty"
["emptyMessage"]=>
string(19) "Your cart is empty!"
["itemAdded"]=>
string(11) "Item added!"
["priceError"]=>
string(21) "Invalid price format!"
["quantityError"]=>
string(38) "Item quantities must be whole numbers!"
["checkoutError"]=>
string(34) "Your order could not be processed!"
}
["button"]=>
array(4) {
["checkout"]=>
string(0) ""
["paypal"]=>
string(0) ""
["update"]=>
string(0) ""
["empty"]=>
string(0) ""
}
["tooltip"]=>
bool(true)
["decimalQtys"]=>
bool(false)
["decimalPlaces"]=>
int(1)
["priceFormat"]=>
array(3) {
["decimals"]=>
int(2)
["dec_point"]=>
string(1) "."
["thousands_sep"]=>
string(1) ","
}
}
["items":"Jcart":private]=>
array(2) {
[0]=>
string(1) "3"
[1]=>
string(1) "2"
}
["names":"Jcart":private]=>
array(2) {
[3]=>
string(12) "Hockey Stick"
[2]=>
string(13) "Baseball Mitt"
}
["prices":"Jcart":private]=>
array(2) {
[3]=>
string(5) "33.25"
[2]=>
string(5) "19.50"
}
["qtys":"Jcart":private]=>
array(2) {
[3]=>
string(1) "1"
[2]=>
string(2) "20"
}
["urls":"Jcart":private]=>
array(2) {
[3]=>
string(15) "http://bing.com"
[2]=>
string(16) "http://yahoo.com"
}
["subtotal":"Jcart":private]=>
float(423.25)
["itemCount":"Jcart":private]=>
int(21)
}
I just need the values in ["items":"Jcart":private]
I assume you got this by casting an object to an array. If that's the case, you shouldn't be doing that! The property is private because you're not supposed to access it directly because it's not a public API. The object should have a public method which you're supposed to call instead, that's the public API. Something like $foo->getItems() or such. Read the documentation and/or source code.
I thought to access the value of '1' here, I would need to the following
$data['category'][0];
["category"]=> array(1) { [0]=> string(1) "1" }
But it doesn't return anything. How would I access the string(1) section to get the value 1?
Here is the full var dump of the variable $data array
array(18) { ["RET"]=> string(65) "/account/add-submission" ["URI"]=> string(22) "account/add-submission" ["XID"]=> string(40) "3ee1766dfdbe4684831021c99a9197beaede03be" ["return_url"]=> string(36) "account/submission-complete/ENTRY_ID" ["channel_id"]=> string(1) "4" ["entry_id"]=> string(1) "0" ["ACT"]=> string(1) "4" ["meta"]=> string(556) "pdKeUQVJTA6FeLnmeqtK0gGu2C1S2gKOvRrMDjjKMou7JAp2HVA48Gn+yXTjY4tKuBam5rlyszhe3rEF2eClOB5bRPEJ8NYeh/qPBSkDuhuk0j+XYrQ0R7dJhaHZPIr1b5sge8/kqmWj2qvrpO5pE/iC6X4scIO2HmOPjWb4Sea2VgGwgQ70j7Qr1QmHlQAIZ95DXMp3YkietUWLWaFKvr8XwSx+vUhKEaueVoAbP0Le3fu0rMqz2LuZIScGpwn4yPJbenkc0P5ME/nM9CsfnzYPmM1cwTHO1Xe/wtJ3HGbNcglfn+A9ubz1GBNULgUvxYAW6eFrhqfAJ2omfiwSzpQkISJaDZvZofjOCHjiS7VaUIDgWOrznOm7oWR3m5Ut4TOxmsX2jeKpUAvLJQppc1+1hormnRSA0mambV0uodflDaZEZbPKmjWxsZD3doNJzmIG29bQtBV+UWdQ4xkxyM6fhyMmUkKGAgE+Xegkp/zK7+AXc4s8bEBENPAa1UbCkh0XEq4IYIqWRzYL/T2bfyySCPzvrbFBErIwj3jUF+w=" ["return"]=> string(36) "account/submission-complete/ENTRY_ID" ["site_id"]=> string(1) "1" ["submission_file_hidden_file"]=> string(0) "" ["submission_file_hidden_dir"]=> string(1) "1" ["type"]=> string(3) "1.1" ["snap_FxlgTgCET"]=> string(13) "vyHdSjitmEUoV" ["submission_file"]=> string(37) "{filedir_1}Tharp_BIO_Pic300x20038.jpg" ["field_id_48"]=> string(37) "{filedir_1}Tharp_BIO_Pic300x20038.jpg" ["field_ft_48"]=> string(4) "none" ["revision_post"]=> array(26) { ["RET"]=> string(65) "/account/add-submission" ["URI"]=> string(22) "account/add-submission" ["XID"]=> string(40) "3ee1766dfdbe4684831021c99a9197beaede03be" ["return_url"]=> string(36) "account/submission-complete/ENTRY_ID" ["author_id"]=> string(1) "1" ["channel_id"]=> string(1) "4" ["entry_id"]=> string(1) "0" ["ACT"]=> string(1) "4" ["meta"]=> string(556) "pdKeUQVJTA6FeLnmeqtK0gGu2C1S2gKOvRrMDjjKMou7JAp2HVA48Gn+yXTjY4tKuBam5rlyszhe3rEF2eClOB5bRPEJ8NYeh/qPBSkDuhuk0j+XYrQ0R7dJhaHZPIr1b5sge8/kqmWj2qvrpO5pE/iC6X4scIO2HmOPjWb4Sea2VgGwgQ70j7Qr1QmHlQAIZ95DXMp3YkietUWLWaFKvr8XwSx+vUhKEaueVoAbP0Le3fu0rMqz2LuZIScGpwn4yPJbenkc0P5ME/nM9CsfnzYPmM1cwTHO1Xe/wtJ3HGbNcglfn+A9ubz1GBNULgUvxYAW6eFrhqfAJ2omfiwSzpQkISJaDZvZofjOCHjiS7VaUIDgWOrznOm7oWR3m5Ut4TOxmsX2jeKpUAvLJQppc1+1hormnRSA0mambV0uodflDaZEZbPKmjWxsZD3doNJzmIG29bQtBV+UWdQ4xkxyM6fhyMmUkKGAgE+Xegkp/zK7+AXc4s8bEBENPAa1UbCkh0XEq4IYIqWRzYL/T2bfyySCPzvrbFBErIwj3jUF+w=" ["return"]=> string(36) "account/submission-complete/ENTRY_ID" ["site_id"]=> string(1) "1" ["title"]=> string(3) "asd" ["submission_file_hidden_file"]=> string(0) "" ["submission_file_hidden_dir"]=> string(1) "1" ["type"]=> string(3) "1.1" ["category"]=> array(1) { [0]=> string(1) "1" } ["snap_FxlgTgCET"]=> string(13) "vyHdSjitmEUoV" ["submission_file"]=> string(24) "Tharp_BIO_Pic300x200.jpg" ["field_id_48_hidden_file"]=> string(0) "" ["field_id_48_hidden_dir"]=> string(1) "1" ["field_id_48"]=> string(24) "Tharp_BIO_Pic300x200.jpg" ["field_ft_48"]=> string(4) "none" ["allow_comments"]=> string(1) "y" ["entry_date"]=> string(10) "1380114180" ["status"]=> string(4) "open" ["previous_status"]=> bool(false) } }
To access that data you have to use $data['revision_post']['category'][0] because category array is in revision_post array.
The value of $data['category'][0] is an array. So you would access like this:
$data['category'][0] = array('1');
echo $data['category'][0][0];
Try it for yourself by copying and pasting here http://writecodeonline.com/php/
i have an array with size=1 and when i try to get the current array its returns false but the array has values.
$article=getArticle($id);
if(is_array($article))
{ $article=current($article); }
the getArticle returns multidimensional array and when var_dump before current i can see the array.
If var_dump after current i get false.
if i use the code above (because the array size=1)
if(is_array($article))
{
foreach($article as $k=>$v)
{
$article=$v;
}
}
It works without any problem
Whats wrong with current?
Please help
===============EDIT========================
This is the var_dump before current
I have remove the content and summary because are large texts
array(1) {
[529]=>
array(14) {
["articles_id"]=>
string(3) "529"
["issue"]=>
string(3) "161"
["membership_type"]=>
string(1) "1"
["el"]=>
array(9) {
["title"]=>
string(23) "AUTOBIANCHI A112 ABARTH"
["url"]=>
string(23) "autobianchi-a112-abarth"
["summary"]=>
string(397) " i have remove it
"
["content"]=>
string(11580) " i have remove it too large
"
["meta_keywords"]=>
string(23) "AUTOBIANCHI,A112,ABARTH"
["meta_description"]=>
string(402) " "
["created"]=>
string(10) "1362076380"
["last_updated"]=>
string(1) "0"
["status"]=>
string(1) "1"
}
["categories_id"]=>
string(2) "12"
["authors_id"]=>
string(2) "16"
["brands_id"]=>
string(2) "36"
["models_id"]=>
string(3) "206"
["engines_id"]=>
string(3) "174"
["o-categories"]=>
array(1) {
["values"]=>
array(1) {
[12]=>
array(15) {
["categories_id"]=>
string(2) "12"
["object_id"]=>
string(1) "1"
["parent_id"]=>
string(1) "0"
["path"]=>
string(2) "12"
["handler"]=>
string(0) ""
["icon"]=>
string(0) ""
["tpl"]=>
string(12) "articles.php"
["alias"]=>
string(1) "0"
["is_nav"]=>
string(1) "1"
["pos"]=>
string(2) "10"
["depth"]=>
string(1) "0"
["cts"]=>
string(0) ""
["mts"]=>
string(0) ""
["configuration"]=>
string(0) ""
["el"]=>
array(15) {
["categories_id"]=>
string(2) "12"
["lang"]=>
string(2) "el"
["category_name"]=>
string(13) "Classic"
["sub_title"]=>
string(0) ""
["summary"]=>
string(0) ""
["image"]=>
string(0) ""
["html_code"]=>
string(0) ""
["meta_title"]=>
string(0) ""
["url"]=>
string(13) "classic"
["url_path"]=>
string(13) "classic"
["meta_keywords"]=>
string(0) ""
["meta_description"]=>
string(0) ""
["created"]=>
string(10) "1355235888"
["last_updated"]=>
string(1) "0"
["status"]=>
string(1) "1"
}
}
}
}
["o-authors"]=>
array(1) {
["values"]=>
array(1) {
[16]=>
array(4) {
["authors_id"]=>
string(2) "16"
["object_id"]=>
string(2) "26"
["image"]=>
string(0) ""
["el"]=>
array(9) {
["authors_id"]=>
string(2) "16"
["lang"]=>
string(2) "el"
["name"]=>
string(31) "Last First"
["last_name"]=>
string(10) "Last"
["first_name"]=>
string(20) "First"
["nick_name"]=>
string(0) ""
["created"]=>
string(10) "1360567827"
["last_updated"]=>
string(1) "0"
["status"]=>
string(1) "1"
}
}
}
}
["o-brands"]=>
array(1) {
["values"]=>
array(1) {
[36]=>
array(6) {
["brands_id"]=>
string(2) "36"
["object_id"]=>
string(2) "17"
["brand_name"]=>
string(11) "AUTOBIANCHI"
["created"]=>
string(10) "1363179463"
["last_updated"]=>
string(1) "0"
["status"]=>
string(1) "1"
}
}
}
["o-models"]=>
array(1) {
["values"]=>
array(1) {
[206]=>
array(7) {
["models_id"]=>
string(3) "206"
["object_id"]=>
string(2) "18"
["brands_id"]=>
string(2) "36"
["model_name"]=>
string(11) "A112 ABARTH"
["created"]=>
string(10) "1363179480"
["last_updated"]=>
string(1) "0"
["status"]=>
string(1) "1"
}
}
}
["o-engines"]=>
array(1) {
["values"]=>
array(1) {
[174]=>
array(6) {
["engines_id"]=>
string(3) "174"
["object_id"]=>
string(2) "19"
["engine"]=>
string(5) "1.050"
["created"]=>
string(10) "1363179448"
["last_updated"]=>
string(1) "0"
["status"]=>
string(1) "1"
}
}
}
}
}
Can you try
$article=getArticle($id);
if (is_array($article)) {
$article = reset($article);
}
or
$article=getArticle($id);
if (is_array($article)) {
$article = array_shift($article);
}
You can look into the documentation about reset and the documentation about array_shift.
If you copy array in getArticle() function then its internal pointer is lost.
I think this is the reason, it returns false.
I'm using array_multisort at the beginning of a script after querying the database. Works fine. The arrays are sorted.
Later on in the same script, I use array_multisort again after another query to the database. The arrays are the same length for this second call but a different length than the arrays used initially.
I'm getting the "array_multisort array sizes are inconsistent" warning in my error_log. Are the arrays persisting from one call to the next and therefore, causing this error? If so, is there a way to clean up the function and remove the initial arrays used before the second call?
Here's a summarized view of the code with var_dump output included:
...1st query...
...1st array_multisort use based on the query...
array_multisort($shiftdate, $beg24, $uniquename, $subunit, $unit, $dept, $shiftdetail, $scheduledStaff);
...1st var_dump values based on array_multisort...
array(3) {
[0]=>
array(10) {
["uniquename"]=>
string(3) "Bob"
["shiftdate"]=>
string(10) "2012-08-12"
["subunit"]=>
string(1) "1"
["unit"]=>
string(1) "1"
["dept"]=>
string(1) "1"
["shiftdetail"]=>
string(5) "7a-3p"
["firstname"]=>
string(0) ""
["lastname"]=>
string(0) ""
["email"]=>
string(0) ""
["beg24"]=>
string(4) "7.00"
}
[1]=>
array(10) {
["uniquename"]=>
string(4) "Cara"
["shiftdate"]=>
string(10) "2012-08-12"
["subunit"]=>
string(1) "1"
["unit"]=>
string(1) "1"
["dept"]=>
string(1) "1"
["shiftdetail"]=>
string(5) "7a-3p"
["firstname"]=>
string(0) ""
["lastname"]=>
string(0) ""
["email"]=>
string(0) ""
["beg24"]=>
string(4) "7.00"
}
[2]=>
array(10) {
["uniquename"]=>
string(4) "Ryan"
["shiftdate"]=>
string(10) "2012-08-12"
["subunit"]=>
string(1) "1"
["unit"]=>
string(1) "1"
["dept"]=>
string(1) "1"
["shiftdetail"]=>
string(5) "7a-3p"
["firstname"]=>
string(0) ""
["lastname"]=>
string(0) ""
["email"]=>
string(0) ""
["beg24"]=>
string(4) "7.00"
}
}
...2nd query...
...2nd array_multisort use based on the query...
array_multisort($shiftdate, $beg24, $uniquename, $subunit, $unit, $dept, $shiftdetail, $firstname, $lastname, $email, $scheduledStaffAndThoseWithDetail);
...NOTE: error_log warning occurs at this second array_multisort use...PHP Warning: array_multisort() [<a href='function.array-multisort'>function.array-multisort</a>]: Array sizes are inconsistent...
...2nd var_dump values based on array_multisort...
array(2) {
[0]=>
array(10) {
["uniquename"]=>
string(4) "Ryan"
["shiftdate"]=>
string(10) "2012-08-12"
["subunit"]=>
string(1) "1"
["unit"]=>
string(1) "1"
["dept"]=>
string(1) "1"
["shiftdetail"]=>
string(5) "7a-3p"
["firstname"]=>
string(4) "Ryan"
["lastname"]=>
string(5) "Dobbs"
["email"]=>
string(17) "dobbsr#gmail.com"
["beg24"]=>
string(4) "7.00"
}
[1]=>
array(10) {
["uniquename"]=>
string(4) "Cara"
["shiftdate"]=>
string(10) "2012-08-12"
["subunit"]=>
string(1) "1"
["unit"]=>
string(1) "1"
["dept"]=>
string(1) "1"
["shiftdetail"]=>
string(5) "7a-3p"
["firstname"]=>
string(4) "Cara"
["lastname"]=>
string(6) "Smith"
["email"]=>
string(14) "cara#gmail.com"
["beg24"]=>
string(4) "7.00"
}
}