0

enter image description here["cus_AKIT4Rz6NHlKMa","cus_AKIP3muBIL95H0","cus_AKI1l8vRoFhUyK","cus_AKI0B1DHs2IBxB","cus_AKHxuw8UvlwH8v","cus_AKHx6iyvJJOwWe","cus_AK03kmAOGCziJ8"]

The data above is the result of the code below, it comes from a PHP array I created on Kuha.php

function StartRetrieve()
{
var a = $('#selType').val();

    $.ajax({

        type:"POST",
        url:"Kuha.php",
        data:'&Type='+a,
        datatype:'json',
        cache:false,
        success:function(s){
        alert(s);

        }

    })
}

how can I access each data and separate it? Because when I use

for (var i = 0; i <= s.length; i++) {
$('#tb').append('<tr><td>'+s[i][0]+'</td></tr>');
}

the [" "] is included and it is printed vertically like

[
"
c
u
s
A
K
I
T
4
R
z
...
"
]

I want the result separated and printed horizontally

cus_AKIT4Rz6NHlKMa
cus_AKIP3muBIL95H0
cus_AKI1l8vRoFhUyK

this is the Kuha.php

<?php
function httpGet($url) {
    $ch = curl_init();
    $headers = array('Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxx');
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $output=curl_exec($ch);
    curl_close($ch);
     $output = json_decode($output,true);//only necessary if the result of $output is a json encoded array
    return $output;
 }



$Type = $_POST['Type'];
$url = "https://api.stripe.com/v1/".$Type;

$array = httpGet($url);
$id = '';
 foreach($array["data"] as $a){
    $id .= $a['id'];
    $id .= ',';

 }
 //echo $id;
$a = rtrim($id,',');
$a = explode(',', $a);

echo json_encode($a);
?>

Thank you for those who will answer.

1
  • Its not a 2 dimensional array. Just use s[i]. Commented Mar 21, 2017 at 9:29

1 Answer 1

2

var s = ["cus_AKIT4Rz6NHlKMa", "cus_AKIP3muBIL95H0", "cus_AKI1l8vRoFhUyK", "cus_AKI0B1DHs2IBxB", "cus_AKHxuw8UvlwH8v", "cus_AKHx6iyvJJOwWe", "cus_AK03kmAOGCziJ8"];
var tr = $('<tr/>');//comment this for vertical
for (var i = 0; i < s.length; i++) {
//var tr = $('<tr/>');//comment this for horizontal
  

  tr.append('<td>' + s[i] + '</td>');
  $('#tb').prepend(tr);
}
td {
  background-color: blue;
  
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table id="tb"></table>

Use this inside your success function just remove the initialization of var s

Sign up to request clarification or add additional context in comments.

1 Comment

Comments are not for extended discussion; this conversation has been moved to chat.

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.