0

I need to push a second array into the first array by matching "cat_id" in the first array to "parent_id" in the second array.

I have the first array - $categories:

[
  {
    "cat_id": "350",
    "parent_id": "0",
    "cat_name": "Category 1"
  }
]

And a second array - $topics:

[
  {
    "cat_id": "351",
    "parent_id": "350",
    "cat_name": "Topic 1",
  },
  {
    "cat_id": "352",
    "parent_id": "350",
    "cat_name": "Topic 2",
  }
]

And I want this:

[
  {
    "cat_id": "350",
    "parent_id": "0",
    "cat_name": "Category 1",
    "topics": [
        {
          "cat_id": "351",
          "parent_id": "350",
          "cat_name": "Topic 1",
        },
        {
          "cat_id": "352",
          "parent_id": "350",
          "cat_name": "Topic 2",
        }
    ]
  }
]

I'm thinking that an embeded foreach loop might be the answer, but still sifting through all of the PHP array functions to try an figure out if there is an existing function that just does this: http://php.net/manual/en/ref.array.php

5
  • 1
    PHP doesn't have built in functions for everything. Commented Jul 11, 2016 at 19:28
  • 1
    Sounds like you need to filter the second array on matching "parent_id"s and set that array to the value of the first array's "topics". Commented Jul 11, 2016 at 19:29
  • You need to learn how to use basic tools to accomplish things. e.g. swing_hammer() and twist_screwdriver() instead of flailing around for build_a_house() Commented Jul 11, 2016 at 19:30
  • in the initial array -> "cat_id": "350", , in the expected array "cat_id": "222", Why? Commented Jul 11, 2016 at 19:33
  • I've updated the question to be more specific. Commented Jul 11, 2016 at 19:55

1 Answer 1

2

The short answer is:

foreach ( $categories AS &$category ) {
  foreach ( $topics AS $topic ) {
    if ( $category[ 'cat_id' ] == $topic[ 'parent_id' ] ) {
      $category[ 'topics' ][] = $topic;
    }
  }
}

This full script should (I hope) demonstrate the behavior you're after.

<?php

$categories = array(
    [
      "cat_id" => "350",
      "parent_id" => "0",
      "cat_name" => "Category 1",
      "topics" => []
    ]
  );

$topics = array (
    [
      "cat_id" => "351",
      "parent_id" => "350",
      "cat_name" => "Category 1 Topic 1",
    ],
    [
      "cat_id" => "352",
      "parent_id" => "350",
      "cat_name" => "Category 1 Topic 2",
    ]
  );

foreach ( $categories AS &$category ) {
  foreach ( $topics AS $topic ) {
    if ( $category[ 'cat_id' ] == $topic[ 'parent_id' ] ) {
      $category[ 'topics' ][] = $topic;
    }
  }
}

echo '<pre>';
print_r( $categories );
echo '</pre>';

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

11 Comments

Thanks for your answer Ben, sorry I should have been more specific, I've updated my question..
I've updated my answer to reflect your updated question. Hope it helps!
Hey Ben, I think this is close but it ends up repeating the first object per new topic as another object so I have redundant entry_id entries... this seems trivial because you should be able to say "if this key value matches this key value push this object into this array... Javascript libraries like Lodash these things are easy!
Hi Sean - If you could post some more of your sample arrays, I can likely figure out what's going on. You'll need to put it in your question or on pastebin with a link to it here, since comments are limited in characters. - Ben
Glad I can help. And again, if you'd like to share the data, the solution is likely pretty simple... I deal with a lot of very dirty data!
|

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.