1

I want to send the value to jquery-variable in new format, i am using nested foreach loop here is my code:

    foreach($names as $key1 => $desc1) {
        $arr_names[] = $names;
        $details = get_details( $key1 );
           if ( is_array( $states ) ) {
          foreach($details as $key2 => $desc2) {
          $title[] = $key2;
          $value[] = $desc2;
          } 
          } 
    }
echo "<input type='hidden' id='storageElement' data-storectrystts='".json_encode($namdetailsArray)."'>";

Right now, i can get this:

Array
(
    [0] => Sam
    [1] => Ben
    [2] => John

    [0] => Age
    [1] => Place
    [2] => Height
    [3] => Weight
    [4] => Year
    [5] => Salary

    [0] => 30
    [1] => AU
    [2] => 6
    [3] => 150
    [4] => First
    [5] => 50000
)

I need to send value in this format through data-storectrystts='".json_encode($namdetailsArray).":

'Sam' => [
    ['Age', '30'],
    ['Place', 'AU']],
'Ben' => [
    ['Height', '6'],
    ['Weight', '150']],
'John' => [
    ['Year', 'First'],
    ['Salary', '50000']]

Update

//if we use print_r($namdetailsArray); then everything is fine

Array
(
    [Sam] => Array
        (
            [0] => Array
                (
                    [0] => Age
                    [1] => 30
                )
            [1] => Array
                (
                    [0] => Place
                    [1] => AU
                )
        )

    [Ben] => Array
        (
            [0] => Array
                (
                    [0] => Height
                    [1] => 6
                )
            [1] => Array
                (
                    [0] => Weight
                    [1] => 150
                )
        )
    [John] => Array
        (
            [0] => Array
                (
                    [0] => Year
                    [1] => First
                )
            [1] => Array
                (
                    [0] => Salary
                    [1] => 50000
                )
        )
    [Derek] => Array
        (
            [0] => Array
                (
                    [0] => tax's cal
                    [1] => 100
                )
            [1] => Array
                (
                    [0] => distance
                    [1] => 5
                )
        )
)



//if we use echo json_encode($namdetailsArray) then everything is fine
{"Sam" => [
    ["Age", "30"],
    ["Place", "AU"]],
"Ben" => [
    ["Height", "6"],
    ["Weight", "150"]],
"John" => [
    ["Year", "First"],
    ["Salary", "50000"]],
"Derek" => [
    ["tax's cal", "100"],
    ["distance", "5"]]}



/*but if we use 
echo "<input type='hidden' id='storageElement' data-storectrystts='".json_encode($namdetailsArray)."'>"; 
echo '<div id="availhai"></div>';

var cSttArry = $("#storageElement").data('storectrystts');
$("#availhai").html(cSttArry);
then it is not showing anything after word "tax" */

{"Sam" => [
    ["Age", "30"],
    ["Place", "AU"]],
"Ben" => [
    ["Height", "6"],
    ["Weight", "150"]],
"John" => [
    ["Year", "First"],
    ["Salary", "50000"]],
"Derek" => [
    ["tax

1 Answer 1

1

You shouldn't be creating lots of separate arrays. Create a single associative array where the key is the name, and the value is a 2-dimensional array like you show.

$namdetailsArray = array();
foreach ($names as $name => $desc) {
    $personArray = array();
    foreach ($desc as $key => $value) {
        $personArray[] = array($key, $value);
    }
    $namdetailsArray[$name] = $personArray;
}
Sign up to request clarification or add additional context in comments.

15 Comments

Thanks Barmar, it is working but having an issue with the inverted comma in a value of array. one of the value is having inverted comma ([6] => tax's cal) so it got break on that value when i use json_encode.
json_encode() should take care of proper encoding of quotes. The problem is presumably with how you're using the values after you decode them.
For now i am just printing the array with php and through jquery: if i use print_r($namdetailsArray) or echo json_encode($cntrySttArry); then everything is fine but if i get value though jquery then it got break on on the value of inverted comma, please see the Update section above.
If you're putting it inside an HTML attribute, use htmlspecialchars(json_encode($namdetailsArray))
htmlspecialchars() will convert quotes to HTML entities.
|

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.