2

I have an array that I'm trying to pass to a php file which is passing, but it shows the array as a string. How do I convert the string to an array so I can go through it in php? Here's my code:

jQuery

$('#add-child').click(function()
{
    var _attributes = [];
    $('#attributes input').each(function()
    {
        _attributes.push($(this).val());
    });
    $.post('../assets/hub.php',{'task':'add-child','parent':$('#parent-id').val(),'value':$('#child-value').val(),'attributes':JSON.stringify(_attributes)},function(callback)
    {
        console.log(callback);
    });
});

PHP

case 'add-child':
    $department = $_POST['parent'];
    $category   = $_POST['value'];        
    $attributes = $_POST['attributes'];
    print($department.' : '.$category.' : '.$attributes);
break;

CURRENT OUTPUT

test1 : test2 : ["sdfg","34","vsdf"] 

** EDIT **

I removed JSON.stringify() and added var_dump() and this is the output:

string(3) "114"
string(4) "asdf"
array(3) {
    [0]=>
    string(4) "asdf"
    [1]=>
    string(4) "ZVCX"
    [2]=>
    string(5) "asdfr"

}

6
  • Why don't you pass _attributes value as-is? WHY do you apply JSON.stringify for it? Commented Nov 18, 2012 at 21:41
  • I tried that and I get an error > Array to string conversion in ... Commented Nov 18, 2012 at 21:42
  • because you're concatenating variables assuming they are string at line print($department.' : '.$category.' : '.$attributes);. While $attributes is an array. Commented Nov 18, 2012 at 21:43
  • You converted the data to JSON. Now you have to parse it into a PHP array. Commented Nov 18, 2012 at 21:47
  • @Felix Kling: .. or remove conversion (since it pointless) )) Commented Nov 18, 2012 at 21:47

2 Answers 2

1

You don't need JSON.stringify at all and you check that everything is passed successfully using

var_dump($department, $category, $attributes);

not the pring_r with concatenations

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

3 Comments

hey zerkms, I did what you said and put the var_dump() results up in my post.
@Mike: and you get expected result. Everything works, doesn't it?
Well, I learned I'm an idiot .. really? I'm gonna concatenate a string and array and expected it to work? Rookie mistake. Also YES! It does output my result when I do the following print($attributes[0]); Thanks, and good job ;)
1
$attributes = json_decode($_POST['attributes']);

if you want to join the array back agian with a comma then

$attributes = implode(',' , json_decode($_POST['attributes']));

4 Comments

Hey Atif, I tried what you suggested and I get > Array to string conversion in ...
@Mike hey Mike, Im sorry I didn't understand what you said can you explain it clearly
@Atif Mohammed Ameenuddin: he says that he concatenates variables (strings and array) and wonders why array cannot be converted to a string :)))
@zerkms but the questions says "How do I convert the string to an array do I can go through it in php?"

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.