array(2) {
[0]=>
object(stdClass)#21 (7) {
["id"]=>
string(1) "1"
["title"]=>
string(7) "cvxzcvd"
["con"]=>
string(10) "gvsdvgsdfg"
["is_important"]=>
string(1) "1"
["date"]=>
string(3) "123"
["image"]=>
string(2) "55"
["cat_id"]=>
string(1) "1"
}
[1]=>
object(stdClass)#22 (7) {
["id"]=>
string(1) "2"
["title"]=>
string(4) "fsdf"
["con"]=>
string(9) "dfdsfvfds"
["is_important"]=>
string(1) "1"
["date"]=>
string(4) "5145"
["image"]=>
string(7) "5454124"
["cat_id"]=>
string(1) "2"
}
}
I passed this array into a view
$this->load->view('home/index',$news_data);
but I wanna use the data separately.
I mean if I wanna use the second title
or the second data.
how can I express that in the view
thanks
You can go like this:-
$news_data[1]->title;
$news_data[1]->data;
Store the array into variable like
$news_data['arr'] = { } ;
,and in view file access it by $arr;
print_r($arr);
Related
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
I'm sending the HTML FORM data with AJAX to PHP script and I'm converting the data to associative array like this:
$json = json_decode($request->get('json'),true);
so, when I'm dumping the variable I get this array:
array(11) {
[0]=>
array(2) {
["name"]=>
string(21) "form[pickupDate][day]"
["value"]=>
string(1) "4"
}
[1]=>
array(2) {
["name"]=>
string(23) "form[pickupDate][month]"
["value"]=>
string(1) "1"
}
[2]=>
array(2) {
["name"]=>
string(22) "form[pickupDate][year]"
["value"]=>
string(4) "2016"
}
[3]=>
array(2) {
["name"]=>
string(22) "form[pickupTime][hour]"
["value"]=>
string(1) "0"
}
[4]=>
array(2) {
["name"]=>
string(21) "form[returnDate][day]"
["value"]=>
string(1) "1"
}
[5]=>
array(2) {
["name"]=>
string(23) "form[returnDate][month]"
["value"]=>
string(1) "1"
}
[6]=>
array(2) {
["name"]=>
string(22) "form[returnDate][year]"
["value"]=>
string(4) "2016"
}
[7]=>
array(2) {
["name"]=>
string(22) "form[returnTime][hour]"
["value"]=>
string(1) "0"
}
[8]=>
array(2) {
["name"]=>
string(19) "form[pickupAddress]"
["value"]=>
string(0) ""
}
[9]=>
array(2) {
["name"]=>
string(12) "form[agency]"
["value"]=>
string(1) "1"
}
[10]=>
array(2) {
["name"]=>
string(12) "form[_token]"
["value"]=>
string(43) "9dh6ghpMv5K9LUdSLvh6y2NOzqTzUrOfVriL8C63Ybs"
}
}
how can I get the value of form[pickupDate][day] without loop?
I was able to do it like this:
echo array_column($json, null, 'name')['form[pickupDate][day]']['value'];
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 am having a hard time extracting a value from the following JSON object
array(3) { [0]=> object(stdClass)#1 (11) { ["Group"]=> string(2) "18" ["GroupName"]=> string(8) "Wireline" ["Region"]=> string(2) "15" ["RegionName"]=> string(8) "Atlantic" ["Province"]=> string(1) "1" ["ProvinceName"]=> string(13) "New Brunswick" ["City"]=> string(2) "11" ["CityName"]=> string(10) "Campbelton" ["Site"]=> string(2) "37" ["SiteName"]=> string(16) "Campbellton PNCT" ["Year"]=> string(4) "2016" }
[1]=> object(stdClass)#2 (5) { ["PlatformID"]=> string(1) "1" ["PlatformTag"]=> string(6) "Access" ["Rack"]=> string(24) "23" Width 36" Depth Rack" ["RackValue"]=> string(1) "2" ["Comments"]=> string(0) "" }
[2]=> object(stdClass)#3 (12) { ["Rack"]=> string(31) "23" Width 36" Depth Rack Access" ["RackValue"]=> string(1) "2" ["RackComments"]=> string(0) "" ["Manufacturer"]=> string(6) "werwer" ["Name"]=> string(6) "werwer" ["Quantity"]=> string(1) "1" ["RackUnits"]=> string(1) "1" ["Power"]=> string(1) "1" ["ActivePassive"]=> string(6) "Active" ["ACDC"]=> string(2) "AC" ["ConnectivityIDs"]=> array(1) { [0]=> string(1) "2" } ["Connectivity"]=> array(1) { [0]=> string(5) "Fiber" } } }
I am trying to extract each item in a foreach loop within PHP Above is the var_dump of the $data[0] JSON object it demonstrates what the Array item looks like.
My foreach is the following
$data = json_decode($_POST["submitdata"]);
$Forecasts = $data[0];
foreach($Forecasts as $Forecast){
echo($Forecast->PlatformID);}
but it returns nothing as a result. Can someone explain to me how to get this from the second Array in the object?
simply place the Index of the inner array along with the object as shown below. This will check for the sub item for the PlatformID and return it to the screen.
foreach($Forecasts as $Forecast){
echo($Forecast[1]->PlatformID);}
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/
array(1) {
[0] => object(stdClass)#53 (14)
{
["cart_item_id"]=> string(2) "64"
["cart_id"]=> string(1) "1"
["nid"]=> string(3) "204"
["qty"]=> string(1) "1"
"changed"]=> string(10) "1340948878"
["data"]=> array(3)
{
["shippable"]=> string(1) "1"
["restrict_qty"]=> string(1) "0"
["module"]=> string(10) "uc_product"
}
["title"]=> string(5) "songs"
["vid"]=> string(3) "204"
["cost"]=> string(9) "123.00000"
["price"]=> string(9) "123.00000"
["weight"]=> string(1) "0"
["weight_units"]=> string(2) "lb"
["module"]=> string(10) "uc_product"
["model"]=> string(1) "1"
}
}
Please help how do get only nid value in new variable.
get via
$output = $array[0]->nid
access object by ->
access array by [index]