0

I tried to implement it something like this

private static function getData($datas) {

    $days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
    $days_aliases = ['SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT'];

    $open_rates = array();

    foreach ($datas as $data) {

        $date = Carbon::parse($data->created_at)->format('l');

        for($i = 0; $i < count($days); $i++) {

            if($date == $days[$i]){

                $open_rates['x'][$i] = $days_aliases[$i];
                $open_rates[$data->name][$i] = (int)$data->open_rate;

            } 

        }

    }

    return $open_rates;

}

But the result is like this.

{ "x": { "0": "SUN", "1": "MON", "2": "TUE", "3": "WED", "4": "THU", "5": "FRI", "6": "SAT" } }

How to make an array like this PHP?

json: {
    'Day Pass' : [40, 10, 99, 50],
    'Day Pass' : [40, 10, 99, 50],
    'Day Pass' : [40, 10, 99, 50],
    'Day Pass' : [40, 10, 99, 50],
    'Day Pass' : [40, 10, 99, 50]
    }

3 Answers 3

2

Simply create a numeric-indexed array. Note that you can't have multiple keys with the same name in an array, so you need different names for "Day pass", otherwise you'd be overwriting them:

<?php
$json = [
    "json" => [
        "Day pass" => 
            [40, 10, 99, 50],
        "Day pass2" => 
            [40, 10, 99, 50],
        "Day pass3" => 
            [40, 10, 99, 50],
        "Day pass4" => 
            [40, 10, 99, 50],
    ],
];
print_r(json_encode($json));

Gives the correct result:

{
    "json": {
        "Day pass": [40, 10, 99, 50],
        "Day pass2": [40, 10, 99, 50],
        "Day pass3": [40, 10, 99, 50],
        "Day pass4": [40, 10, 99, 50]
    }
}

Demo

Using the same key instead, gives you one item:

<?php
$json = [
    "json" => [
        "Day pass" => 
            [40, 10, 99, 50],
        "Day pass" => 
            [40, 10, 99, 50],
        "Day pass" => 
            [40, 10, 99, 50],
        "Day pass" => 
            [40, 10, 99, 50],
    ],
];
print_r(json_encode($json));

Result:

{
    "json": {
        "Day pass": [40, 10, 99, 50]
    }
}

Demo


The problem with your specific code is that, in order to have json_encode() return a JSON array (instead of an object), the PHP array needs to be a numeric sequential 0-indexed array. What you can do is pass the array through array_values() in order to only preserve the values and reset the keys:

<?php
$open_rates = [];
$open_rates["x"]["0"] = "Sun";
$open_rates["x"]["2"] = "Mon";
var_dump(json_encode($open_rates)); // JSON object
$open_rates["x"] = array_values($open_rates["x"]);
var_dump(json_encode($open_rates)); // JSON array

Demo

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

6 Comments

How can we do it inside a loop? I tried to implement that. But it returns some array index which I don't want
Change it to this: $open_rates['x'] = []; if ($date == $days[$i]) { $open_rates['x'][] = $days_aliases[$i]; /* etc */ }
result is like this "x": [], "test4": { "0": 0, "1": 100, "2": 0, "3": 100, "4": 133, "5": 67, "6": 0 },
Sorry, I'm having a hard time editing this because I'm on my iPad. You're getting the results because of non sequential keys. json_encode() will give you a JSON array only if it's number indexed, sequential and starting at 0 (basically a normal array). Look here. It'll get you in the right direction using array_values().
Awesome dude array values is the answer.. Can you edit your answer so that I can check it?
|
1

The json you gave is not a valid json, keys shouldn't repeat inside a valid json

Assuming you meant:

json: {
    'Day Pass 1' : [40, 10, 99, 50],
    'Day Pass 2' : [40, 10, 99, 50],
    'Day Pass 3' : [40, 10, 99, 50],
    'Day Pass 4' : [40, 10, 99, 50],
    'Day Pass 5' : [40, 10, 99, 50]
}

It can be written in php as

<?php
$json = array(
    "json" => array(
        "Day Pass 1" => array(40, 10, 99, 50),
        "Day Pass 2" => array(40, 10, 99, 50),
        "Day Pass 3" => array(40, 10, 99, 50),
        "Day Pass 4" => array(40, 10, 99, 50),
        "Day Pass 5" => array(40, 10, 99, 50)
    )
)

1 Comment

How can we do it inside a loop? I tried to implement that. But it returns some array index which I don't want
0

The example that you have written is wrong. Object cannot have same key repeated. So assuming that key is different it should be something like this in PHP:

$json = array(‘day pass’ => array(40,10,99,50), ‘day pass2’ => array(20,30,30))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.