How to build an Object with an Array inside, using Eloquent Laravel

Asked

Viewed 813 times

0

Hello folks I need to build a array as follows for a plugin:

var data = [{
    "label": "Follows",
    "color": "#aad874",
    "data": [
        ["Seg", 50],
        ["Ter", 84],
        ["Qua", 52],
        ["Qui", 88],
        ["Sex", 69],
        ["Sab", 92],
        ["Dom", 58]
    ]
}, {
    "label": "UnFollows",
    "color": "#7dc7df",
    "data": [
        ["Seg", 13],
        ["Ter", 44],
        ["Qua", 44],
        ["Qui", 27],
        ["Sex", 38],
        ["Sab", 11],
        ["Dom", 39]
    ]
}];

I’ve tried a lot of things like:

$staticWeeks = self::statisticstaWeeks();

$staticsDays['Follow']      = [];
$staticsDays['FollowBy']    = [];

if(count($staticWeeks) > 0):
foreach($staticWeeks as $weeks):
array_push($staticsDays['Follow'], $weeks->ins_sta_follow);
array_push($staticsDays['FollowBy'], $weeks->ins_sta_following);
endforeach;
endif;

$staticsDays['Follow'] = $diasFollow = [
    "Seg" => (!empty($staticsDays['Follow'][0]) ? $staticsDays['Follow'][0] : 0), 
    "Ter" => (!empty($staticsDays['Follow'][1]) ? $staticsDays['Follow'][1] : 0), 
    "Qua" => (!empty($staticsDays['Follow'][2]) ? $staticsDays['Follow'][2] : 0),
    "Qui" => (!empty($staticsDays['Follow'][3]) ? $staticsDays['Follow'][3] : 0), 
    "Sex" => (!empty($staticsDays['Follow'][4]) ? $staticsDays['Follow'][4] : 0), 
    "Sab" => (!empty($staticsDays['Follow'][5]) ? $staticsDays['Follow'][5] : 0), 
    "Dom" => (!empty($staticsDays['Follow'][6]) ? $staticsDays['Follow'][6] : 0)];

$staticsDays['FollowBy'] = $diasFollowBy = [
    "Seg" => (!empty($staticsDays['FollowBy'][0]) ? $staticsDays['FollowBy'][0] : 0), 
    "Ter" => (!empty($staticsDays['FollowBy'][1]) ? $staticsDays['FollowBy'][1] : 0), 
    "Qua" => (!empty($staticsDays['FollowBy'][2]) ? $staticsDays['FollowBy'][2] : 0),
    "Qui" => (!empty($staticsDays['FollowBy'][3]) ? $staticsDays['FollowBy'][3] : 0), 
    "Sex" => (!empty($staticsDays['FollowBy'][4]) ? $staticsDays['FollowBy'][4] : 0), 
    "Sab" => (!empty($staticsDays['FollowBy'][5]) ? $staticsDays['FollowBy'][5] : 0), 
    "Dom" => (!empty($staticsDays['FollowBy'][6]) ? $staticsDays['FollowBy'][6] : 0)];

$follow = [(object)[
    "label" => "Seguindo",
    "color" => "#aad874",
    "data"  => $staticsDays['Follow']
]];

$followBy = [(object)[
    "label" => "Seguidores",
    "color" => "#7dc7df",
    "data"  => $staticsDays['FollowBy']
]];

$result = array_merge($follow, $followBy);

return json_encode( $result );

And yet you don’t return as I want.

  • Notice when to do "Seg" => "qualquercoisa" this will generate {"Seg": "qualquercoisa"} and what you want is ["seg", "qualquercoisa"] right? So instead of objects you have to create arrays with ["Seg", (!empty($staticsDays['Follow'][0]) ? $staticsDays['Follow'][0] : 0)]. Test that.

  • A tip nothing to do, but avoid using the alias to array(). Example $array = [ ]. Because it is only compatible from PHP 5.4.

  • Daniel Omine had already tried this, yet he came back an object

  • @Danielomine however Windows only works with Php 5.6 or higher, so you won’t have any problems

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.