PHP foreach not saving all data from JSON

335 Views Asked by At

I'm pulling data from JSON array in php. Everything works as expected but somewhere in the loop parts gets lost, it only pulls first one. Please help. Thank you very much.

Here is Json:

$json = '
{
    "theId": "fB17",
    "loadId": "82387T",
    "description": "Short description",
    "contact": "Name of person",
    "contactinfo": "Phone number or email of contact person",
    "pickupaddress": "Address",
    "parts": [
        { "number": "655-2032B" },
        { "number": "655-2056" },
        { "number": "655-2056" },
        { "number": "300-85091" }
    ]
}';

PHP code:

    $data = json_decode($json);
    $theId .= $data->theId;
    $loadId .= $data->loadId;
    $description .= $data->description;
    $contact .= $data->contact;
    $contactinfo .= $data->contactinfo;
    $pickupaddress .= $data->pickupaddress;

    ALL ABOVE GET PULLED FROM JSON AND SAVED PROPERLY

Saving data

    $obj = new ElggObject();
    $obj->subtype = "load";
    $obj->description = strip_tags($description);
    $obj->title = $title.$theId.'-'.$loadId;
    $obj->contact = $contact;
    $obj->contactinfo = $contactinfo;
    $obj->pickupaddress = $pickupaddress;

    $guid = $obj->save();

Object is saved with basic info

Now going through "parts" data from Json

foreach ($data->parts as $core_title) {

    // Getting "parts' value from Json
    $titlename = $core_title->number;

    //Now need to use that data and find existing entity with that title in Database

    $dbprefix = elgg_get_config('dbprefix');

    $options['joins'][] = "JOIN {$dbprefix}objects_entity oe ON oe.guid = e.guid";
    $options['wheres'][] = "oe.title = '$titlename'";
    $options['types'] = array('object');
    $options['subtypes'] = array('core');
    $options['limit'] = 1;

    $entity = elgg_get_entities($options);
    // I got that entity that matches the title

    //Now get GUID of the entity
    foreach ($entity as $o) {
        $boxg = $o->guid;
    }

    // It works I get the GUID as $boxg but now need to save EACH $data->parts as annotation to database

    $obj->annotate('cores', $boxg);
}

IT only grabs first one fron Json ( 655-2032B )and saves only that one.

If I do this it saves each $data->parts value not just first one:

foreach ($data->parts as $core_title) {
    $titlename = $core_title->number;

    $obj->annotate('cores', $titlename);
}
2

There are 2 best solutions below

0
Matey On

This code makes no sense:

//Now get GUID of the entity
foreach ($entity as $o) {
    $boxg = $o->guid;
}

It goes through all items in $entity and keeps overwriting $boxg value without actually doing anything with the value.

The result of this foreach loop is that $boxg holds the GUID of the last item in $entity and only then this one single value used to annotate:

    $obj->annotate('cores', $boxg);

Is this what you really wanted to do? Shouldn't the annotate method be used inside the foreach loop?

Apart from that it's hard to give you a clear answer because your code is rather obscure and not well explained.

0
Damir Gasparlin On

Figured it out finally. It was not foreach but what was in it.

Ended up making function that does database query to find existing entity with that title.

This is what I ended up with. Thanks everyone for the help.

foreach ($data->parts as $core_title) {

    $titlename = $core_title->number;

    $entity = get_object_by_title('core',$titlename);

    foreach ($entity as $o) { 
    $coreguidd = $o->guid; 
    $obj->annotate('cores', $coreguidd);
    }


    }