0

I'm trying to return an array of values using laravel foreach based on input values. This is my expected output

    [
[
    "number": 2567046807891,
    "message": "THis is a test",
    "sender": "Oasis Agrib"
],
[
    "number": 256782248927,
    "message": "THis is a test",
    "sender": "Oasis Agrib"
]

]

but am getting this instead

[
{
"number": 2567046807891,
"message": "THis is a test",
"sender": "Oasis Agrib"
},
{
"number": 256782248927,
"message": "THis is a test",
"sender": "Oasis Agrib"
}
]

This is what i have tried

$batch = array();
                foreach($data as $v){
                    $phone_number = $v['phonenumber'];
                    $batch[] = array('number' => $phone_number,'message' => $message, 'sender' => $sender);
                }
                return $batch;

How can best go about this situation

5
  • how do you render on your view file.. can you post the code here.? Commented Aug 24, 2019 at 13:15
  • am pushing the array to external an API. It only accepts that format 'msgdata'=> array( array( 'number'=>256704733492, 'message'=>'elias', 'senderid'=>'Good'), array( 'number'=>256774733492, 'message'=>'marvin', 'senderid'=>'Bad') ) Commented Aug 24, 2019 at 13:24
  • have you tried casting $batch to array as return (array) $batch; Commented Aug 24, 2019 at 13:39
  • Yes but i get the same result Commented Aug 24, 2019 at 13:47
  • it will be helpful if you could post your route,controller as well. Commented Aug 24, 2019 at 16:40

2 Answers 2

1

The expected outcome with curly brackets is correct. An array (also called a list) [] contains elements and cannot have properties/keys:

[
    [], // another array/list
    {}, //object
    "", //string
]

So in order to work with key/value pairs, you'll have to go with an object:

{
    "number": 256782248927,
    "message": "THis is a test",
    "sender": "Oasis Agrib"
}

Combine multiple objects in a list and you'll end up with the outcome you already posted:

[
    {
        "number": 2567046807891,
        "message": "THis is a test",
        "sender": "Oasis Agrib"
    },
    {
        "number": 256782248927,
        "message": "THis is a test",
        "sender": "Oasis Agrib"
    }
]

Note that an external API will not handle your 'expected' outcome as the syntax is invalid, it should return an error or discard invalid JSON.

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

3 Comments

but the api takes this format 'msgdata'=> array( array( 'number'=>256704733492, 'message'=>'elias', 'senderid'=>'Good'), array( 'number'=>256774733492, 'message'=>'marvin', 'senderid'=>'Bad') ) How can i go about that??
Probably if you change your return $batch; into return array('msgdata' => $batch);. You're only missing the msgdata layer.
Nothing changes. I still return the curly brackets
0

If you want to use an JSON structure as PHP array, you can use the function json_decode() and set the second flag to true

$json_string = '[
    {
        "number": 2567046807891,
        "message": "THis is a test",
        "sender": "Oasis Agrib"
    },
    {
        "number": 256782248927,
        "message": "THis is a test",
        "sender": "Oasis Agrib"
    }
]' ;  
$array = json_decode($json_string, true);

You can now use $array as a classic PHP associative array.

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.