0

I have an array like this:

$templateStyleArr = array(
                      0 => array(
                            'text' => 'General Text',
                            'default_value' => '#444',
                            'scopval' => 'general',
                            ),
                      1 => array(
                            'text' => 'Accent Color',
                            'default_value' => '#c91a1a',
                            'scopval' => 'Accent',
                            ),      
                      2 => array(
                            'text' => 'Button Hover',
                            'default_value' => '#2ec72e',
                            'scopval' => 'dark_btn_hover',
                            ),      
                      3 => array(
                            'text' => 'Button Text',
                            'default_value' => '#3300f5',
                            'scopval' => 'btn_txt',
                            ),  
                        )

I want to save this array to data base in Json format using PHP.I am using json_encode function. Json saved perfectly.Problem is When I try to decode the saved json(using json_decode),I am not getting the above array back.

4
  • 2
    What are you getting back? Commented Sep 22, 2015 at 13:01
  • `<pre>Array ( [0] => [{"text":"General Text","default_value":"#444","scopval":"general"},{"text":"Accent Color" ,"default_value" :"#c91a1a","scopval":"Accent"},{"text":"Button Hover","default_value":"#2ec72e","scopval":"dark_btn_hover" },{"text":"Button Text","default_value":"#3300f5","scopval":"btn_txt"}] ) when I print it like this echo '<pre>';print_r(json_decode($finalStyleData[0])); Nothing showing. Commented Sep 22, 2015 at 13:07
  • That is the json-formatted data! You are not showing the print_r after the json_decode. Commented Sep 22, 2015 at 13:09
  • Please provide a full code example Commented Sep 22, 2015 at 13:10

3 Answers 3

2

Referring to the manual, you may need to set the assoc parameter to TRUE so that the returned object will be converted into an associative array.

$array = json_decode($json_string_from_db, TRUE);
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to store just the array as string in the database you can try serialize(), with unserialize you'll get the exactly same array as return value

$templateStyleArr = array(
    0 => array(
        'text' => 'General Text',
        'default_value' => '#444',
        'scopval' => 'general',
    ),
    1 => array(
        'text' => 'Accent Color',
        'default_value' => '#c91a1a',
        'scopval' => 'Accent',
    ),
    2 => array(
        'text' => 'Button Hover',
        'default_value' => '#2ec72e',
        'scopval' => 'dark_btn_hover',
    ),
    3 => array(
        'text' => 'Button Text',
        'default_value' => '#3300f5',
        'scopval' => 'btn_txt',
    ),
);

$string = serialize( $templateStyleArr );
var_dump( unserialize( $string ) );

If the array indexes are really just numbers you can use this:

$string = json_encode( $templateStyleArr );
$arrayFromJson = json_decode( $string );

foreach( $arrayFromJson as $index => $jsonObject ) {
    // cast object to array
    $arrayFromJson[$index] = (array) $jsonObject;
}

var_dump( $arrayFromJson );

Comments

-1

basicly your json encode will convert array to object when you decode it, so you need create function for generate object to array, you should try this function where will convert correct

function to_array( $object ){
    if( !is_object( $object ) && !is_array( $object ) ){
        return $object;
    }
    if( is_object( $object ) ){
        $object = get_object_vars( $object );
    }
    return array_map( 'to_array', $object );
}

$array = to_array( json_decode($fromdb) );

print_r( $array );

it's work okay for me so let's try :)

Comments

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.