0
    pragma solidity ^0.5.0;

    contract Testing{

        struct personalInfo {
            uint id;
            string[] colors;
            bool player;
        }

        mapping(uint=> personalInfo) public personals;
        constructor () public {

        }

        function updatePersonalInfo(uint _ID, string memory _color, bool _player) public {
            personalInfo storage updateP = personals[_ID];
            updateP.id = _ID;
            updateP.player = _player;
            updateP.colors.push(_color);
        }
}

Please tell me why _color is not pushing inside the array.?? No error but color array not returned in the output of personals function.

Suria
  • 7
  • 4
  • 1
    what is the error you are getting? – Aniket Jun 17 '19 at 09:35
  • No error getting and it compile and deployed successfully. but at the time of getting personals function by id...it returns the output of personalInfo structure excluding color array.

    Can you please try that code in remix so that you can get an clear idea.

    – Suria Jun 17 '19 at 09:37
  • I think there is no array of string in solidity https://stackoverflow.com/questions/42716858/string-array-in-solidity – Majd TL Jun 17 '19 at 09:45
  • Thank you @MajdTL I realised that thing. But what is the suggested way of my problem statement.I need that structure like a way.I need to store string array in structure , SO is there any alternative best solution ? and Thanks again. – Suria Jun 17 '19 at 09:48
  • See this for solution: https://ethereum.stackexchange.com/questions/17312/solidity-can-you-return-dynamic-arrays-in-a-function – Aniket Jun 17 '19 at 09:56
  • use bytes32[] instead of string, but then you should give the color as hex, i think like this web3.utils.asciiToHex(color) – Majd TL Jun 17 '19 at 10:11

1 Answers1

1

You can get array values by this code:

pragma solidity ^0.5.0;
pragma experimental ABIEncoderV2;

contract Testing{

    struct personalInfo {
        uint id;
        string[] colors;
        bool player;
    }

    mapping(uint=> personalInfo) public personals;
    constructor () public {

    }    function updatePersonalInfo(uint _ID, string  memory _color, bool _player) public {
        personalInfo storage updateP = personals[_ID];
        updateP.id = _ID;
        updateP.player = _player;
        updateP.colors.push(_color);
    }
    function rPersonalInfo(uint id)public view returns(personalInfo memory p){
        return personals[id];
    }
}
Mahesh Rajput
  • 1,211
  • 7
  • 17