0

I created a JSON object in JQuery that looks like this:

var massUpdateDetails = {
 checkone: $('#checkone').val(),
 checktwo: $('#checktwo').val(),
 details: [{
 detailone: $('#detailone').val(),
 detailtwo: $('#detailtwo').val()
 }],
 locs: [{
 locone: $('#locone').val(),
 loctwo: $('#loctwo').val()
 }]
}

When I post the massUpdateDetails object to PHP, I can print out the object like this:

<?php
 if(isset($_POST['massUpdateDetails'])){
 $value = $_POST['massUpdateDetails'];
 }
 print_r($value);
?>

When using print_r($value), here is what I am seeing:

Array
(
 [checkone] => checkoneInfo
 [checktwo] => value1,value2
 [details] => Array
 (
 [0] => Array
 (
 [detailone] => details of one
 [detailtwo] => details of two
 )
 )
 [locs] => Array
 (
 [0] => Array
 (
 [locone] => loc one
 [loctwo] => loc two
 )
 )
 )

I'm trying to set the various values to PHP variable like this:

$checkone= isset($value['checkone']) ? $value['checkone'] : ''; // <-- no problem getting this set
$detailone = isset($value['details']['detailone']) ? $value['details']['detailone'] : ''; // <-- problem is here
echo $detailone;

As you see in the above, I am having a problem setting the variable for $detailone.

When I echo $detailone, I get no error in the console.

What am I doing wrong and how can I fix it?

asked Feb 25, 2021 at 21:19
2
  • 1
    Tip -- use the null coalesce operator for shorter code: $checkone = $value['checkone'] ?? '' Commented Feb 25, 2021 at 21:25
  • You need to loop through $value['details'] - foreach($value['details'] as $detail){ echo $detail['detailone'];} or else do $value['details'][0]['detailone'] Commented Feb 25, 2021 at 21:27

1 Answer 1

1

$value['details'] is an array, so you have to access each element as an array element, e.g. $value['details'][0]['detailone']

answered Feb 25, 2021 at 21:22
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.