|
Posted by "Alan Lord" on 10/13/05 17:39
Hi cc and others.
I have had quite a struggle with this. But finally, the end result looks
quite
simple...
Here is the array of Keys Names I want to preserve:
$filter = array(
'CategoryId' => 1, 'CategoryLevel' => 1, 'CategoryName' => 1,
'CategoryParentId' => 1
);
Here is the routine which (for ease, creates a new array containing only
the array
keys contained in $filter:
foreach ($orig_arr as $key => $data) {
if (is_array($data)) {
foreach ($data as $subkey => $subdata) {
if (isset($filter[$subkey])) {
$sub_arr[$subkey] = $subdata;
}
}
}
$new_arr[] = $sub_arr;
}
$new_arr has just what I need :-)
This only works on a two dimensional array. But with a bit of tweaking
and turing
into a function, it could probably be used on any number of dimensions
by making
it recursive?
Thanks to the contributors who supplied ideas and knowledge..
If anyone thinks this could be done a better way, please feel free to
comment!
Alan
> -----Original Message-----
> From: cc [mailto:nyvsld@gmail.com]
> Sent: 13 October 2005 07:43
> To: Alan Lord
> Cc: php-general@lists.php.net
> Subject: Re: Removing Items from an Array
>
> could Alan give samples about the input and expected output
> of the black box?
> then we can consider implement it.
>
> On 10/13/05, Alan Lord <lord_alan@hotmail.com> wrote:
> > Thanks for the replies gents.
> >
> > I have cludged together something from your solutions but
> it isn't yet
> > working. I've been staring at multi-dim arrays all day and
> am getting
> > tired, so I'll carry on tomorrow.
> >
> > Thanks again,
> >
> > Alan
> >
> > > -----Original Message-----
> > > From: Jochem Maas [mailto:jochem@iamjochem.com]
> > > Sent: 12 October 2005 19:18
> > > To: tg-php@gryffyndevelopment.com
> > > Cc: php-general@lists.php.net; lord_alan@hotmail.com
> > > Subject: Re: [PHP] Removing Items from an Array
> > >
> > > Id like to continue where TG left off ...
> > >
> > > hth.
> > >
> > > tg-php@gryffyndevelopment.com wrote:
> > > > If I understand what you're asking, then maybe this will help:
> > > >
> > > > $arrset1 = array("Apples" => 3, "Oranges" => 5,
> "Apricots" => 1);
> > > > $arrset2 = array("Couches" => 6, "Chairs" => 2, "Benches" => 5);
> > > >
> > > > $alldataarr["Fruits"] = $arrset1;
> > > > $alldataarr["Furniture"] = $arrset2;
> > > >
> > > > Say we want to remove "Chairs", and let's do it the hard way:
> > > >
> > > > foreach ($alldataarr as $key => $data) {
> > > > foreach ($data as $subkey => $subdata) {
> > > > if ($subkey == "Chairs) {
> > > > unset($alldataarr[$key][$subkey]);
> > > > }
> > > > }
> > > > }
> > > >
> > > > using foreach $arr as $key => $data you can get the
> > > key/index name as well as the actual value stored in that part of
> > > your array. Then all you have to do is refer back up to the main
> > > array using the current $key/$subkey values as your indexes.
> > > >
> > >
> > > $filter = array(
> > > 'Fruits' => array('Apples' => 1, 'Oranges' => 1),
> > > 'Furniture' => array('Couches' => 1, 'Chairs' => 1), );
> > >
> > > $alldataarr = array();
> > > $alldataarr["Fruits"] = array("Apples" => 3, "Oranges" => 5,
> > > "Apricots" => 1); $alldataarr["Furniture"] = array("Couches"
> > > => 6, "Chairs" => 2, "Benches" => 5);
> > >
> > > foreach ($alldataarr as $key => $data) {
> > > if (!isset($filter[$key]) {
> > > // we want it all;.
> > > continue;
> > > }
> > > $alldataarr[$key]= array_intersect_keys($data,
> $filter[$key]); }
> > >
> > >
> > > // heres one I prepared earlier:
> > >
> > >
> > > /**
> > > * array_intersect_keys()
> > > * ^--- the internal function (php5.x+?)
> > > has no 's'
> > > *
> > > * returns the all the items in the 1st array whose keys
> are found
> > > in any of the other arrays
> > > *
> > > * @return array()
> > > */
> > > function array_intersect_keys()
> > > {
> > > $args = func_get_args();
> > > $originalArray = $args[0];
> > > $res = array();
> > >
> > > if(!is_array($originalArray)) { return $res; }
> > >
> > > for($i=1;$i<count($args);$i++) {
> > > if(!is_array($args[$i])) { continue; }
> > > foreach ($args[$i] as $key => $data) {
> > > if (isset($originalArray[$key]) &&
> !isset($res[$key])) {
> > > $res[$key] = $originalArray[$key];
> > > }
> > > }
> > > }
> > >
> > > return $res;
> > > }
> > >
> > >
> > >
> > >
> > > >
> > > > Basic example, but I think you can modify this to work with
> > > what you're doing.
> > > >
> > > > Let me know if you have any questions about this example.
> > > >
> > > > -TG
> > > >
> > > >
> > > >
> > > > = = = Original message = = =
> > > >
> > > > Hi all,
> > > >
> > > > I'm really struggling here! I have a large, multi-dimensional
> > > > array that I want to "clean-up" a bit before committing
> to a database.
> > > >
> > > > I want to remove quite a bit of the array but using the
> > > KEYs not the
> > > > values. I know the keys I want to keep and I know the keys
> > > I want to
> > > > get rid of. I want to keep the structure and sequence of
> > > the array in tact.
> > > >
> > > > All of the array traversing functions in PHP seem to
> > > either: only work
> > > > on the values, or do not allow the removal of elements
> of the array!
> > > >
> > > > Can anyone offer a clue bat to a tired old array walker!
> > > >
> > > > Thanks
> > > >
> > > > Alan
> > > >
> > > >
> > > > ___________________________________________________________
> > > > Sent by ePrompter, the premier email notification software.
> > > > Free download at http://www.ePrompter.com.
> > > >
> > >
> > >
> >
> > --
> > PHP General Mailing List (http://www.php.net/) To
> unsubscribe, visit:
> > http://www.php.net/unsub.php
> >
> >
>
Navigation:
[Reply to this message]
|