-1

I have a multidimensional array which I am looping through using a foreach loop.

I have then need to check whether any of those arrays have the key of 'parent_page' and the same value of any other arrays, such as:

$arrMulti = array(array(
    'page_id' => 1,
    'page_parent' => 28,
    'page_title' => 'Testing'
), array(
    'page_id' => 2,
    'page_parent' => 30,
    'page_title' => 'A seperate page'
), array(
    'page_id' => 3,
    'page_parent' => 28,
    'page_title' => 'Testing Sub Page'
));

So $arrMulti[0]['page_parent'] would match with $arrMulti[2]['page_parent'], so I need to then create a new array using those, something like this:

$arrParentIDs = array( 'parent_id' => array(
    1,
    3
));

Sorry for the poor explanation, but do you have any idea on how to do this?

Thanks!

DarkMantis
  • 1,510
  • 5
  • 19
  • 40
  • Correct answer: Yey, I have an idea. Almost correct answer: try it again at http://codereview.stackexchange.com/ – feeela Jun 13 '12 at 16:11
  • I struggle to see how this is a matter for codereview rather than StackOverflow? If I wanted to get someone to review my code and ask for pointers or criticism on my code then yes, I would put it on CodeReview. However, I am asking for help on how to do something to do with programming, thus a matter for StackOverflow. – DarkMantis Jun 13 '12 at 16:24
  • Well, as I understood your question, you are not asking for help but for a solution. Otherwise you would have had some solution or try on your own and had that explained in your question. The question: "This is my problem, how can I solve it." is off-topic on SO, IMHO. There are many ways to solve such a problem, most of them using some sort of loop to iterate over the array – but you knew that before asking, didn't you? – feeela Jun 13 '12 at 16:29
  • I did know that, however, I still was unaware of how to do this. Hence why I asked this question. I am not trying to take up people's time unnecessarily, I just wanted help with my issue and the best way of doing it. Luckily, I have found that now thanks to John B. I think I am having a bit of a strange day today where I cannot think properly. Anyways, thanks everyone for your time. – DarkMantis Jun 13 '12 at 17:27
  • here is an answer for your question. [http://stackoverflow.com/questions/1019076/how-to-search-by-key-value-in-a-multidimensional-array-in-php][1] [1]: http://stackoverflow.com/questions/1019076/how-to-search-by-key-value-in-a-multidimensional-array-in-php – Thu Ra Aug 13 '12 at 03:57

2 Answers2

2
$parentIds = array();
foreach($arrMulti as $temp):

    if(isset($temp['page_parent'] && !in_array($temp['page_parent'], $parentIds)){
        $parentIds[] = $temp['page_parent'];
    }

endforeach;

var_dump($parentIds);//to show the contents
Rooster
  • 9,954
  • 8
  • 44
  • 71
1

Try something like this..

foreach($arrMulti as $array) {
    foreach($array as $key=>$val) {
        //your statement/condition
    }
}