2

I have to push an associative array in a normal array (not to convert).
Example (NO CODE):

project = {}
element["title"] = "My title"
element["description"] = "My description"

is there a way to have this

echo $project->title;
//or
echo $project[0]["title"]

? I'v tried this, but server says: ERROR 500

    $i = 0;
    $projects = {};
    foreach($projectsElements as $element) {
        while($i <= $nRowsForProject) {
            $idSection = $element->idSection;
            if($idSection == 1) $elements["".$element->internalDescription.""] = $element->text;
            else if($idSection == 2) $elements["".$element->internalDescription.""] = $element->text;
            else if($idSection == 3) $elements["".$element->internalDescription.""] = $element->text;
            $i++;
        }
        array_push($projects,$elements);
        $i=0;
    }
2
  • $projects []= $element;? Commented Oct 12, 2016 at 11:59
  • 1
    You confuse things: an object is something different than an array. The -> operator references a property inside an object, but that is not possible in a declarative manner. Also creating an object is not possible by a simple $projects = {}; as you attempt to, that is a syntax error (your error 500). Instead you typically have to implement a class of which you then can instantiate an object. Commented Oct 12, 2016 at 12:01

3 Answers 3

4

$projects = {}; is not valid php.

If you want to initialize an empty array (associative or numeric, that does not matter), you need:

$projects = [];

or on older php versions:

$projects = array();

Also note that you need to do the same to your $elements array at the beginning of each iteration otherwise it will grow on every iteration. Assuming that the descriptions are not all the same...

foreach($projectsElements as $element) {
    $elements = [];
    while($i <= $nRowsForProject) {
        ...

And your while loop does not seem to make a lot of sense: You are not using the $i variable in your loop so are just doing the same assignments on each iteration.

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

Comments

0
$projects = []; // declare empty array
foreach($projectsElements as $element) {
  $projects []= $element; // push $element into $projects array
}

Comments

0
$i = 0;
$projects = array();
foreach($projectsElements as $element) {
    while($i <= $nRowsForProject) {
    $elements = array();
        $idSection = $element->idSection;
        if($idSection == 1) $elements["".$element->internalDescription.""] = $element->text;
        else if($idSection == 2) $elements["".$element->internalDescription.""] = $element->text;
        else if($idSection == 3) $elements["".$element->internalDescription.""] = $element->text;
        $i++;
    }
    array_push($projects,$elements);
    $i=0;
}

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.