Below is the print output from $_POST['image']
stdClass Object
(
[0] => Array
(
['filename'] => cn-100x100.png
['contents'] =>
)
[1] => Array
(
['filename'] =>
['contents'] =>
)
)
when I do,
echo '<pre>';
print_r((object)$_POST['image'][0]['filename']);
exit;
it gives me an error
Notice: Undefined index: filename
Update
I tried to do var_dump( (array)$_POST['image']),
array(2) { ["'filename'"]=> string(14) "cn-100x100.png"
["'contents'"]=> string(10218) "base64..."}
If I removed the base64 data from the array column ['contents'], now I can access the first array.
you can update it
echo '<pre>';
print_r((array)$_POST['image'][0]['filename']);
exit;
Object properties cannot be accessed by using $object['property_name']
You have to use the arrow syntax $object->property_name or $object->{'property_name'}
In this case it looks like somehow $_POST['image'] has been defined as an object, so you would have to use:
$_POST['image']->{'0'}['filename']
You could also convert it to an array by using: $_POST['image'] = (array)$_POST['image'];
Convert all the object into the array by following way
$all_images= (array) $_POST['image'];
echo '<pre>';
print_r($all_images);
echo '</pre>';
Now, you can access like this $all_images[0]['filename']
Related
i am new to JSON i have a json object retrieved from the database in the form of
Array
(
[0] => stdClass Object
(
[id] => 1
[data] => {"vehicle":[{"year":"2000","make":"Ac","model":"Aceca","acquired_year":"2016","acquired_month":"2","use":"Business","distance_driven_to_work_or_school":"2","distance_driven_manually":"10000"}],"first_name":"ADAS","last_name":"DSADSADA","email":"asddsa#sda.com","phone":"dsasasa","postal_code":"","drivers":[{"name":"ssada","birth_year":"2016","birth_month":"2","birth_day":"2","gender":"female","martial_status":"Single","license_number_provided":"yes","license_number":"asddasdas","license_type":"","training_completed":"","years_been_listed_on_auto_policy_in_north_america":"No Previous Experience","license_suspensions":"","accidents":"Select","convictions":"Select","cancellation_reason":"","cancellation_year":"","cancellation_month":"","cancellation_day":""}],"considering_renters_to_reduce_rate":"yes","install_winter_tires":"no","park_in_private_driveway":"yes","willing_to_install_device":"no","years_insured_with_current_company":"4 Years","how_heard_about_us":"asdaa"}
[date] => 2017-11-20 18:17:52
[status] => 0
)
)
now when i try to use json_decode to convert it into an array i am getting Trying to get property of non-object here's my code
<?php
echo "<pre>";
print_r($quotes); //works fine uptil here
$data = json_decode($quotes->data,true);//the error line
echo "<pre>";
print_r($data);
?>
i tried it a couple of ways but it is not working i tried some other solutions as well stil ending up getting errors any help?
It is because $quotes is an array of objects. Try $quotes[0]->data, e.g.:
$data = json_decode($quotes[0]->data,true);
// ------------------------^^^
You're receiving an array containing objects from the database. You're almost there but instead of
$data = json_decode($quotes->data,true);
You should use
$data = json_decode($quotes[0]->data,true);
I have a variable with object like this in PHP code.
[{"author_id":2},{"author_id":1}]
How to get the value of author_id. thanks
use json_decode to convert the object in php and get it. Example:
<?php
$xx='[{"author_id":2},{"author_id":1}]';
$arr=json_decode($xx,true);
print_r($arr);
//Output: Array ( [0] => Array ( [author_id] => 2 ) [1] => Array ( [author_id] => 1 ) )
echo $arr[0]["author_id"];
//Outpu: 2
?>
This is serialized JSON Array with JSON objects inside.
$str = '[{"author_id":2},{"author_id":1}]';
$arr = json_decode($str, true);
foreach($arr as $item) {
echo $item['author_id'];
}
That data you posted is in JSON format. After decoding that standard format you can directly access the contents.
For the first entry that would simply be:
<?php
$data = json_decode('[{"author_id":2},{"author_id":1}]');
var_dump($data[0]->author_id);
The output obviously is:
int(2)
To access all entries have a try like that:
The output then is:
array(2) {
[0]=>
int(2)
[1]=>
int(1)
}
I want to echo an array_chunk as string, how do I do that ?
here is the code
$rt = $this->db->query("SELECT id_reg_pd FROM 043104_kuliahmhs_20152_2a0dc380_temp");
$_datao = array_chunk($rt->result(), 3);
foreach($_datao as $batman => $robin) {
print_r($robin);
}
I want echo id_reg_pd as string.
I have tried tried :
echo $robin->id_reg_pd;
but get php error like this
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Here the array from print_r($robin);
Array
(
[0] => stdClass Object
(
[id_reg_pd] => 001be76b-4e58-4cea-96cf-fee2d8e0abdc
)
[1] => stdClass Object
(
[id_reg_pd] => 001d4fe5-73f5-4bae-b126-1f787ea0104e
)
[2] => stdClass Object
(
[id_reg_pd] => 002ab28b-e0b9-464a-89fb-12552512a5d0
)
)
Loop over $robin and then check
foreach($robin as $value)
{
echo $value->id_reg_pd;
}
try like this
for($i=0;$i<count($_datao);$i++){
$newarr = (array) $robin[$i];
echo $newarr['id_reg_pd'];
}
Sahil is incorrect. It is not true that you must use a for / foreach loop to achieve your desired result. array_column() works on an array of objects. If you can use a simple implode() call to convert your array to a string, then here is a simple one-liner:
Code (Demo):
$robin=[
(object)['id_reg_pd'=>'001be76b-4e58-4cea-96cf-fee2d8e0abdc'],
(object)['id_reg_pd'=>'001d4fe5-73f5-4bae-b126-1f787ea0104e'],
(object)['id_reg_pd'=>'002ab28b-e0b9-464a-89fb-12552512a5d0']
];
//print_r($robin); // uncomment to see for yourself
//var_export(array_column($robin,'id_reg_pd')); // uncomment to see for yourself
echo implode(', ',array_column($robin,'id_reg_pd')); // implode with whatever glue you wish
Output:
001be76b-4e58-4cea-96cf-fee2d8e0abdc, 001d4fe5-73f5-4bae-b126-1f787ea0104e, 002ab28b-e0b9-464a-89fb-12552512a5d0
I did a print_r on my array $total and it returned the following:
Array ( ) Array ( ) Array ( ) Array ( ) Array ( [0] => stdClass Object (
[generated] => 6 [magnitude] => 3 [log_pk] => 14 [result] => 0.5000 ) )
Array ( ) Array ( )
I need to be able to print out log_pk from within the stdClass Object. I have tried print $total[0]->log_pk but that was unsuccessful. The error was Undefined offset: 0. Any help is appreciated. Thanks.
So this is within a loop, you should check if the index 0 exists first.
if (isset($total[0])) echo $total[0]->log_pk
Are you doing this within a loop? If so then it looks like the array is empty on most iterations. Try:
if (!empty($total)) print $total[0]->log_pk;
var_dump() displays structured information about one or more expressions that includes its type and value. Arrays and objects are explored recursively with values indented to show structure.
var_dump($total)
PHP: var_dump - Manual
it looks like your print_r is inside a loop.
while(true){
$total = some_function();
print_r($total);
if($condition) break;
}
// Here - outside the loop, is not the right place for the print_r();
If you want you print outside the loop, you would change $total = some_function(); to $total[] = some_function(); and then you can do print_r($total[index])
I'm trying to use JSON decode to retrieve some information but it's not working, it's just showing the data as null when I use var_dump
Here's the JSON formatted data passed in the URL
orderSummary={"orderInfo":[{"itemNumber":"1","quantity":"3","price":"5.99","productName":"Item_B"}]}
When I simply echo the un-decoded string I get the following
echo $_GET['orderSummary'];
//displays the following
{\"orderInfo\":[{\"itemNumber\":\"1\",\"quantity\":\"3\",\"price\":\"5.99\",\"productName\":\"Item_B\"}]}
However, when I try to decode it the result is null
$order = $_GET['orderSummary'];
$data = json_decode($order,true);
echo "<PRE>";
var_dump($data); die();
//displays the following
<PRE>NULL
Is it not properly formatted?
Run the input string through stripslashes() first.
$input = '{\"orderInfo\":[{\"itemNumber\":\"1\",\"quantity\":\"3\",\"price\":\"5.99\",\"productName\":\"Item_B\"}]}';
print_r(json_decode(stripslashes($input)));
Output
stdClass Object
(
[orderInfo] => Array
(
[0] => stdClass Object
(
[itemNumber] => 1
[quantity] => 3
[price] => 5.99
[productName] => Item_B
)
)
)
Demo
Alternatively
Turn off magic_quotes_gpc. Considering that it has been deprecated (and removed in 5.4), this the better option.