0

In my form im outputting some json or a string of data that is an array from Input::old('tags') the problem I'm having is being able to test it and see if it is json and if it is do a foreach loop and put the tag attribute is an array then implode the array and output it in an input field.

[{"id":112,"tag":"corrupti","tagFriendly":"corrupti","created_at":"2015-07-20 00:05:28","updated_at":"2015-07-20 00:05:28","pivot":{"question_id":60,"tag_id":112,"created_at":"2015-07-20 00:05:28","updated_at":"2015-07-20 00:05:28"}},{"id":9,"tag":"placeat","tagFriendly":"placeat","created_at":"2015-07-20 00:05:23","updated_at":"2015-07-20 00:05:23","pivot":{"question_id":60,"tag_id":9,"created_at":"2015-07-20 00:05:28","updated_at":"2015-07-20 00:05:28"}}]

So my output should look like this

'corrupti, placeat, etc...'

Could someone help me get the desired result following the steps I just outlined

This code doesn't work but it's what I kinda need to happen. is_array is always false

<?php
    $tags = Input::old('tags');
    if(is_array($tags)) {
        foreach ($tags as $tag) {
            $tags[] = $tag->tag;
        }
        $tags = implode(', ', $tags);   
    }                       
?>
3
  • json_decode will return an object containing the array so how you have it now is_array should be returning false. Commented Jul 23, 2015 at 2:07
  • it returns false even if I don't decode it Commented Jul 23, 2015 at 2:08
  • yes because it's not seeing an array. you need to extract the array from the object. try $obj = json_decode($tags); print_r($obj[0]); see what that shows. Commented Jul 23, 2015 at 2:10

3 Answers 3

3

Here you go.

$tags = json_decode(Input::old('tags'), true); // decode json input as an array

if (is_array($tags)) {
    foreach ($tags as $key=>$value) {
        $tags[$key] = $value['tag'];
    }
    $tags = implode(', ', $tags);
    echo $tags;
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can use array_map to extract the tag property from the object.

<?php
$jsonObject = json_decode(Input::old('tags'));
if(is_array($jsonObject)) {
    $onlyTags = array_map(function($item) { return $item->tag; },$jsonObject);
    print implode(",", $onlyTags);
}

2 Comments

the part where it goes is_array($jsonObject) return false so the code doesn't get past the if statement
show us the output of this thing: dd(Input::old('tags'));
1

Try adding true to json_decode.

json_decode($tags, true);

According to the docs that should return an associative array. http://php.net/manual/en/function.json-decode.php

is_array might not work with associative arrays with a key/value pair. Try replacing is_array with this function:

function is_associate_array($array)
{
    return $array === array_values($array);
}

So:

if(is_associate_array(json_decode($tags, true))) {

}

3 Comments

It still returns false when you test if it is an array
don't believe is_array will work. please look at my edit.
your function is_associate_array do the opposite thing. it should be named as is_not_associate_array. BTW is_array will work fine. the only reason why is_array(json_decode(Input::old('tags'), true)) not works is that Input::old('tags') - is not a json string

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.