简体   繁体   English

用数组生成多维对象

[英]generating multidimensional object with array

I am generating an object like this: 我正在生成这样的对象:

控制台快照

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. 如您所见, education是一个对象内部的数组,但是我想要的是degree_1major_1及其值位于同一对象中。

This is how I want it but with education as an array: 这就是我想要的,但是将education作为一个数组: 在此处输入图片说明

One other thing: When I var_dump it in my php it is just fine with the arrays and everything. 另一件事:当我在php中var_dump时,就可以使用数组和其他所有东西了。 But my javascript gets the second image above- object of object when it was just an array.. 但是我的javascript在对象只是一个数组时,它在对象的上方获得了第二个图像。

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);
}

将第一个元素从education设为0,现在为1,这就是json_encode将其解析为对象的原因。

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. 如果是这种情况,则需要以简单的循环就足够的方式存储数据,如果不需要,则需要一些PHP逻辑。

We can't really help you on that until you show us an example of your table contents. 在向您展示表内容的示例之前,我们无法真正为您提供帮助。

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM