php - Searching an array of associate arrays for two matching parameters -


i have loop builds array of associative arrays looks this:

array(     'foo' => '',     'bar' => '',     'thingys' => array() ) 

on each iteration of loop, want search through array associate array that's 'foo' , 'bar' properties match of current associate array. if exists want append thingys property of current associative array match. otherwise append entire thing. know how loops, i'm wondering if there simpler way array function. i'm on php 5.3.

example

<?php  $arr = array(     array(         'foo' => 1,         'bar' => 2,         'thing' => 'apple'     ),     array(         'foo' => 1,         'bar' => 2,         'thing' => 'orange'     ),     array(         'foo' => 2,         'bar' => 2,         'thing' => 'apple'     ), );  $newarr = array(); ($i=0; $i < count($arr); $i++) {     $matchfound = false;     ($j=0; $j < count($newarr); $j++) {          if ($arr[$i]['foo'] === $newarr[$j]['foo'] && $arr[$i]['bar'] === $newarr[$j]['bar']) {             array_push($newarr[$j]['thing'], $arr[$i]['things']);             $matchfound = true;             break;         }     }     if (!$matchfound) {          array_push($newarr,             array(                 'foo' => $arr[$i]['foo'],                 'bar' => $arr[$i]['bar'],                 'things' => array($arr[$i]['thing'])             )         );     } }  /*output $newarr = array(     array(         'foo' => 1,         'bar' => 2,         'things' => array('orange', 'apple')     ),     array(         'foo' => 2,         'bar' => 2,         'things' => array('apple')     ), ) */  ?> 

i don't know if possible through built-in function, think no. can implemented through array_map, anyway have perform double loop.

i propose one-loop solution using temporary array ($keys) index of created $newarr items, based on foo , bar; elements of original array processed through foreach loop, , if $keys element first key foo value , second key bar value exists, current thing value added returned key index of $newarr, otherwise new $newarray element created.

$newarr = $keys = array(); foreach( $arr $row ) {     if( isset( $keys[$row['foo']][$row['bar']] ) )     { $newarr[$keys[$row['foo']][$row['bar']]]['thing'][] = $row['thing']; }     else     {         $keys[$row['foo']][$row['bar']] = array_push( $newarr, $row )-1;         $newarr[$keys[$row['foo']][$row['bar']]]['thing'] = array( $row['thing'] );     } } unset( $keys ); 

eval.in demo

edit: array_map variant

this same solution above, using array_map instead of foreach loop. note original code can converted in way.

$newarr = $keys = array(); function filterarr( $row ) {     global $newarr, $keys;     if( isset( $keys[$row['foo']][$row['bar']] ) )     { $newarr[$keys[$row['foo']][$row['bar']]]['thing'][] = $row['thing']; }     else     {         $keys[$row['foo']][$row['bar']] = array_push( $newarr, $row )-1;         $newarr[$keys[$row['foo']][$row['bar']]]['thing'] = array( $row['thing'] );     } }  array_map( 'filterarr', $arr ); 

eval.in demo


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -