0

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.

4
  • i think there might be some issues in your array format Commented Mar 29, 2019 at 4:38
  • $array = array( 0 => [ "filename" => 'cn-100x100.png', "contents" => '' ], 1 =>[ "filename" => '', "contents" => '' ], ); print_r((object)$array[0]['filename']); this works Commented Mar 29, 2019 at 4:39
  • Theres a base64 data on the column contents If I removed the base64 data from the array column ['contents'], now I can access the first array. Any ideas, help? Commented Mar 29, 2019 at 5:54
  • stackoverflow.com/questions/48599135/… ,it is a similar issue please refer Commented Mar 29, 2019 at 5:58

3 Answers 3

1

you can update it

echo '<pre>';
print_r((array)$_POST['image'][0]['filename']);
exit;
Sign up to request clarification or add additional context in comments.

1 Comment

Theres a base64 data on the column contents If I removed the base64 data from the array column ['contents'], now I can access the first array. Any ideas, help?
1

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'];

2 Comments

Theres a base64 data on the column contents If I removed the base64 data from the array column ['contents'], now I can access the first array. Any ideas, help?
A bit of a hack, but consistent way to convert objects to array is by using: $variable = json_decode(json_encode($variable),true); - try applying this to $_POST['image'] variable
1

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']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.