0

I'm currently building an array off of an object and I've got one element called images that has multiple sub elements called 'urls' structured like so

categories": [
    {
      "images": [
        {

          "urls": [
            "path/test.jpg",
            "path/test2.jpg",
            "path/test3.jpg"
          ],
        },
        {

          "urls": [
            "path/test4.jpg",
            "path/test5.jpg",
            "path/test6.jpg"
          ],

        },
        {

          "urls": [
            "path/test7.jpg",
            "path/test8.jpg",
            "path/test9.jpg"
          ],

        },
]

The values there don't have keys, it's just the url path but I'd like to add these to my $groupItem array and just have each url be it's own element on the same level as the group number (basically I'm exporting and need each url as it's own column)

The structure I want

0 =>"path/test.jpg",
1 =>"path/test2.jpg",
2 =>"path/test3.jpg"
3 =>"path/test4.jpg",
4 =>"path/test5.jpg",
5 =>"path/test6.jpg"
6 =>"path/test7.jpg",
7 =>"path/test8.jpg",
8 =>"path/test9.jpg"

The loop/array:

foreach($prices->groups as $group){ 
    $groupItem = array(); 
    $groupItem["number"] = $group->number;

    foreach($group->images as $images){
        $groupItem["urls"] = $images->urls;
    }
}

How can I simply just add on any url to the groupItem level of that array?

2
  • You need to show the actual structure you want. What you show for structure doesn't show number or urls. Commented Jan 18, 2019 at 15:38
  • Also, $groupItem = array(); destroys your existing array each time thru the loop. Commented Jan 18, 2019 at 15:39

2 Answers 2

2

Outside the outer loop, init the value to an empty array:

$groupItem["urls"] = [];

Then use the empty array reference operator to append new values to the end of an array:

foreach($group->images as $images){
    $groupItem["urls"][] = $images->urls; // add this url to the end of the list
}

Alternatively, use array_push():

foreach($group->images as $images){
    array_push($groupItem["urls"], $images->urls);
}

I think you can probably also skip the inner loop and just use the array explode operator like this:

array_push($groupItem["urls"], ...$images->urls);
Sign up to request clarification or add additional context in comments.

8 Comments

That gives me an array called "urls" though, with each url inside. I want to take the actual URL and make them unindexed elements at the same level as the group->number
That will give you an array with both indexed and associative keys -- is that really what you want? You won't be able to iterate it properly.
I'm getting ``` "number" => "123" "urls" => array:4 [ //URL paths in here ] ``` but i want ``` "number" => "123" 0 => urlPath/1.jpg 1 => urlPath/2.jpg 2 => urlPath/3.jpg 3 => urlPath/4.jpg ```
yes because It needs to be 2d to dump to laravel excel. I just want them in the same row as the category info
Ok then just use this: $groupItem[] = $images->urls;
|
1

You might also use array_column with (from php 5.6) a variable length argument list:

For example, for the images which contains an array of objects where each object has a property urls and contains an array of image urls:

foreach ($prices->groups as $group) {
    $groupItem = array();
    $groupItem["number"] = $group->number;
    $groupItem= array_merge($groupItem, ...array_column($group->images, "urls"));    
}

Demo

8 Comments

foreach($group->images as $images){ $groupItem[] = array_merge(...array_column($images, "urls")); } I tried this but it doesn't work
Your example works but I can't add it to my array that way
@TomN. Would it be $groupItem["urls"] = array_merge(...array_column($images, "urls")); without using the foreach.
I tried that and it gives me 0 => array:12 [ …12] which is close, but I need those 12 elements to each be 0=>, 1=> etc. So the top level of my array still just has 12 elements, one of which is this array of 12 where I need it to actually have 15 elements on that one level
I;m getting ``` 0 => array:4 [▼ "number" => "123" 0 => array:12 [ …12] ] ``` I need ``` 0 => array: [▼ "number" => "123" 0 => "filepath/1.jpg" 1 => "filepath/2.jpg" 2 => "filepath/3.jpg" 3 => "filepath/4.jpg" 4 => "filepath/5.jpg" 5 => "filepath/6.jpg" 6 => "filepath/7.jpg" 7 => "filepath/8.jpg" etc ] ```
|

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.