0

I am generating an object like this:

console snapshot

As you can see education is an array inside an object, but what I want is for degree_1 and major_1 and their values to be in the same object.

This is how I want it but with education as an array: enter image description here

One other thing: When I var_dump it in my php it is just fine with the arrays and everything. But my javascript gets the second image above- object of object when it was just an array..

public function show($id)
{
    $tmp = array();
    $post = array();
    $postInfo = Post::find($id);
    $params =  DB::select( DB::raw("SELECT param.*, sys_param_values.*,param_value.*,type_post.*,
                                       param.name AS paramName, 
                                       doc_param.name AS docParamName 
                                       FROM param
                                       LEFT JOIN doc_param ON param.doc_param_id = doc_param.id
                                       LEFT JOIN sys_param_values ON param.id = sys_param_values.param_id
                                       LEFT JOIN param_value ON sys_param_values.value_ref = param_value.id
                                       LEFT JOIN type_post ON sys_param_values.ref_id = type_post.id WHERE type_post.id = ".$id));

    $isMultiple = false;
    $post['postInfo'] = $postInfo['original'];
    foreach($params as $k=>$v) {

        $iteration    = $v->iteration;
        $docParamName = $v->docParamName;
        $paramName    = $v->paramName;

        if($v->value_ref == null) {
            $value = $v->value_short;
        } else {
            $value = $v->value;
        }

        if($iteration) {

            $post[$docParamName][$iteration][$paramName] = $value;
            // need to return education as array not as object
            // $post[$docParamName][] = array($paramName=>$value) ;

        }elseif(!$iteration) {
            $post[$docParamName][$paramName] = $value;
        }
    }   

    return Response::json($post);
}
7
  • 1
    Can you post your PHP code too, please? Commented Jul 27, 2015 at 13:16
  • How do you generate it now? What have you tried so far? Commented Jul 27, 2015 at 13:17
  • maybe make first element from education to 0, now is 1, so that's why json_encode could parse it as object. Commented Jul 27, 2015 at 13:22
  • Actually education is an array of objects. Show the code that builds education Edit your question, dont add it as a comment Commented Jul 27, 2015 at 13:24
  • @RiggsFolly yes it is but when i get it in my client side it returns as the second picture Commented Jul 27, 2015 at 13:25

2 Answers 2

1

Make first element from education to 0, now it is 1, so that's why json_encode is parsing it as an object.

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

Comments

0

I don't know what you data source looks like, but it looks to me that you're fetching vertical data and them want to display it horizontally. If that is the case your data need to be stored in a way that simply looping is enough, if not some PHP logic will be required.

We can't really help you on that until you show us an example of your table contents.

Cheers

1 Comment

I think you have a good point. Show us the output from your query at least the result rows related to the education data you are returning

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.