4

I'm trying to generate the following JSON data:

[
    {
        "ID": "A1000",
        "name": "John Simpson",
        "serialID": "S41234",
        "tagID": "ABC6456"
    },  
    {
        "ID": "B2000",
        "name": "Sally Smith",
        "serialID": "B5134536",
        "tagID": "KJJ451345"
    },
    {
        "ID": "A9854",
        "name": "Henry Jones",
        "serialID": "KY4239582309",
        "tagID": "HYG452435"
    }
]

from within a PHP foreach loop which looks like this:

foreach($records as $record) {

        $ID = $record->getField('propertyID');
        $name = $record->getField('assignedName');
        $serialID = $record->getField('serial');
        $tagID = $record->getField('tag');

    }

I've tried using:

$arr = array('ID' => $ID, 'name' => $name, 'serialID' => $serialID, 'tagID' => $tagID);

which works for each record within the loop, but can't work out the syntax to join them together into a single JSON data string?

1
  • 2
    don't overwrite $arr each iteration, just add [] Commented Sep 15, 2017 at 6:58

6 Answers 6

6

Problem:- Your are over-writing your $arr variable each-time inside foreach() loop.That cause the issue.

Solution:- Instead of over-writing, assign values to array like below:-

$arr = []; //create empty array

foreach($records as $record) {

    $arr[] = array(
                'ID' => $record->getField('propertyID');
                'name' => $record->getField('assignedName');
                'serialID' => $record->getField('serial');
                'tagID' => $record->getField('tag')
            );//assign each sub-array to the newly created array
} 
echo json_encode($arr);
Sign up to request clarification or add additional context in comments.

Comments

2

Add records to some kind of aggregation array and then json_encode it:

$items = [];
foreach($records as $record) {
    $items[] = [ 
        'ID' => $record->getField('propertyID'),
        'name' = $record->getField('assignedName'),
        'serialID' => $record->getField('serial'),
        'tagID' => $record->getField('tag'),
    ];
}

echo json_encode($items);

Comments

0

Try this:

$data= array();
foreach($records as $record) {

$data[] = array(
        'ID' => $record->getField('propertyID');
        'name' => $record->getField('assignedName');
        'serialID' => $record->getField('serial');
        'tagID' => $record->getField('tag')
        );
    }

echo json_encode($data);

Explanation: Make an array, put all the values on one of their numerical index with in the loop, json_enccode() it.

Comments

0
<?php
$ArrayName = array();

foreach($records as $record) {

        $ID = $record->getField('propertyID');
        $name = $record->getField('assignedName');
        $serialID = $record->getField('serial');
        $tagID = $record->getField('tag');

$ArrayName[] = array('ID' => $ID, 'name' => $name, 'serialID' => $serialID, 'tagID' => $tagID); //assign each sub-array to the newly created array

    }
echo json_encode($ArrayName);

?

> Blockquote

Comments

0

You could use something like array_map() for this:

$result = array_map(function ($record) {
    return [
        'ID'       => $record->getField('propertyID'),
        'name'     => $record->getField('assignedName'),
        'serialID' => $record->getField('serial'),
        'tagID'    => $record->getField('tag'),
    ];
}, $records);

echo json_encode($result);

Comments

-1

this code will help you:

<?php
$resultArray = array();

foreach($records as $record) 
{
        $newResultArrayItem = array();
        $newResultArrayItem['ID'] = $record->getField('propertyID');
        $newResultArrayItem['name'] = $record->getField('assignedName');
        $newResultArrayItem['serialID'] = $record->getField('serial');
        $newResultArrayItem['tagID'] = $record->getField('tag');

        $resultArray[] = $newResultArrayItem;
}

$encodedArray = json_encode($ArrayName);

echo $encodedArray;

?>

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.