1

I am currently creating an array like so

$propertyData[] = array(
    'propertyID' => $property->propertyID,
    'propertyDepartment' => $property->department,
    'displayAddress' => $property->propertyAddress->displayAddress,
    'price' => $property->propertyDetails->price
);

I then have a one to many on one of my models, as such, I am adding multiple additional data like so

foreach ($property->propertyImages as $image) {
    $propertyData[]['images'] = array(
        'imageUrl' => $image->imageUrl,
    );
}

The result of the above is something like the following

array:2 [
  0 => array:4 [
    "propertyID" => "Some Data"
    "propertyDepartment" => "Some Data"
    "displayAddress" => "Some Data"
    "price" => "111111"
  ]
  "images" => array:3 [
    0 => array:1 [
      "imageUrl" => "Some URL"
    ]
    1 => array:1 [
      "imageUrl" => "Some URL"
    ]
    2 => array:1 [
      "imageUrl" => "Some URL"
    ]
  ]
]

This is nearly what I am after, but not quite. I want the images element to be part of the original array. So what I am after is the following

array:1 [
  0 => array:4 [
    "propertyID" => "Some Data"
    "propertyDepartment" => "Some Data"
    "displayAddress" => "Some Data"
    "price" => "111111"
    "images" => array:3 [
        0 => array:1 [
          "imageUrl" => "Some URL"
        ]
        1 => array:1 [
          "imageUrl" => "Some URL"
        ]
        2 => array:1 [
          "imageUrl" => "Some URL"
        ]
    ]
  ]
]

How would I go about achieving this?

Thanks

4
  • can you specify your problem more clearly? i mean what is the problem now? Commented Jan 18, 2020 at 21:46
  • A quick question, do you first fill the entire array with properties and then add the images or do you first add a property and then add the images relative to that property? Commented Jan 18, 2020 at 21:53
  • I query the database for a property based on an ID. That property can have many images. Commented Jan 18, 2020 at 21:54
  • Is $property->propertyImages already an array? Could you not just assign it like 'images' => $property->propertyImages? Commented Jan 19, 2020 at 7:28

2 Answers 2

1

If I understand you correctly you want this:

array:1 [
  0 => array:4 [
    "propertyID" => "Some Data"
    "propertyDepartment" => "Some Data"
    "displayAddress" => "Some Data"
    "price" => "111111"
    "images" => array:3 [
        "Some URL",
        "Some URL",
        "Some URL"
      ]
    ]
  ]
]

So, first created an $images variable with array of images:

$images = [];
foreach ($property->propertyImages as $image) {
  $images[] = $image->imageUrl;
}

And after that create your array with all properties:

$propertyData[] = array(
  'propertyID' => $property->propertyID,
  'propertyDepartment' => $property->department,
  'displayAddress' => $property->propertyAddress->displayAddress,
  'price' => $property->propertyDetails->price,
  'images' => $images
);
Sign up to request clarification or add additional context in comments.

Comments

1

Ok, so basically the problem you are having here is that you are automatically assigning a new element to an array and afterwards you don't know which is the index that the element was stored at. The easiest way I think you could do this is the following:

// First create a separate array for the property
// Notice I am adding an empty images array here
$newProperty = array(
    'propertyID' => $property->propertyID,
    'propertyDepartment' => $property->department,
    'displayAddress' => $property->propertyAddress->displayAddress,
    'price' => $property->propertyDetails->price,
    'images' => array()
);

// Then with your foreach you can easily add images to the images array
foreach ($property->propertyImages as $image) {
    $newProperty['images'][]['imageUrl'] = $image->imageUrl
}

// And finally you add the new property to your final array

$propertyData[] = $newProperty;

Personally I would do the following for the image insertion

foreach ($property->propertyImages as $image) {
    array_push($newProperty['images'], $image);
    // This way I'd have all the info related to the image
}

That should solve your problem.

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.