在数组中以新格式打印数组值

时间:2017-01-27 01:21:05

标签: php jquery

我想以新格式将值发送到jquery-variable,我正在使用嵌套的foreach循环 这是我的代码:

    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)."'>";

现在,我可以得到这个:

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
)

我需要通过data-storectrystts ='“发送这种格式的值.json_encode($ namdetailsArray)。”:

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

更新

//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 个答案:

答案 0 :(得分:1)

您不应该创建许多单独的数组。创建一个关联数组,其中键是名称,值是您显示的二维数组。

function launchurl(x){
aKS = window.open(unescape(x), 'cwo', '');
}
launchurl("https://docs.google.com/forms/d/jjjjjjHtw7GM31QSgJ1kj0c7Gk/viewform?entry.1583361440=" + textvalue1 + "&entry.1582110718=" + textvalue2 + "&entry.581477285=" + textvalue3 + "&entry.414420907=" + textvalue4 + "&entry.886009407=" + textvalue5 +"&entry.2012346668=" + textvalue7 + "&entry.1997554716=" + textvalue8 + "&entry.11139272="+ textvalue9 +"&entry.1861391454=" + textvalue10 +"&entry.145797294=" + textvalue15+"&entry.744028611=" + textvalue11)}