Author Topic: [SOLVED] Manipulate array (remove certain keys and renaming keys)  (Read 5030 times)

0 Members and 1 Guest are viewing this topic.

Offline lindmTopic starter

  • Enthusiast
  • Posts: 198
    • View Profile
I have the following array:

Code: [Select]
$array = array(
'FIGURE1cy'=>100,
'FIGURE1py'=>100,
'FIGURE2cy'=>100,
'FIGURE2py'=>100 ,
'somethingelse'=>hello,
'somethingelse2'=>hello2

etc
etc
);

I am looking for two ways to manipulate this array:
1. Remove all elements where the key ends with cy. I could use unset but there must be a simpler way since the array is fairly large. Some way to iterate through the array with unset??

2. Perform number 1 and change the name of all keys ending with cy to py.

Any help appreciated.

Offline Mark Baker

  • Addict
  • Posts: 1,586
  • Gender: Male
    • View Profile
Re: Manipulate array (remove certain keys and renaming keys)
« Reply #1 on: November 29, 2008, 04:32:46 PM »
Looping through the array and using unset is probably the easiest method, but array_walk() provides you with an alternative.

<?php

$array 
= array( 'FIGURE1cy'      => 100,
                
'FIGURE1py'      => 100,
                
'FIGURE2cy'      => 100,
                
'FIGURE2py'      => 100 ,
                
'somethingelse'  => 'hello',
                
'somethingelse2' => 'hello2'
);


function 
arrayWalkFunc($item1$key$array) {
	
if (
substr($key,-2) == 'cy') {
	
	
unset(
$array[$key]);
	
}
}

array_walk($array,'arrayWalkFunc',&$array);

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

?>
« Last Edit: November 29, 2008, 04:34:07 PM by Mark Baker »
9 out of 10 PHP problems can be resolved by setting
Code: (php) [Select]
error_reporting(E_ALL);
ini_set('display_errors', 1);
php -l <filename> will identify 9 out of the remaining 10 problems
Remember, the command line is your friend
Development Projects: PHPExcel and PHPPowerPoint

Offline lindmTopic starter

  • Enthusiast
  • Posts: 198
    • View Profile
Re: Manipulate array (remove certain keys and renaming keys)
« Reply #2 on: November 30, 2008, 10:58:08 AM »
Thanks!

Any suggestion if I want to rename all keys ending with cy to py?

Offline Mark Baker

  • Addict
  • Posts: 1,586
  • Gender: Male
    • View Profile
Re: Manipulate array (remove certain keys and renaming keys)
« Reply #3 on: November 30, 2008, 12:55:39 PM »
Any suggestion if I want to rename all keys ending with cy to py?
You could use the same basic technique.

However, that would cause problems with your test array, where you already have a key ending in 'py' that matches one of the 'cy' keys that you want to rename.


<?php

$array 
= array( 'FIGURE1cy'      => 100,
                
'FIGURE1py'      => 100,
                
'FIGURE2cy'      => 100,
                
'FIGURE2py'      => 100 ,
                
'somethingelse'  => 'hello',
                
'somethingelse2' => 'hello2'
);


function 
arrayWalkFunc($item1$key$array) {
	
if (
substr($key,-2) == 'cy') {
	
	
$newkey substr($key,0,-2).'py';
	
	
unset(
$array[$key]);
	
	
$array[$newkey] = $item1;
	
}
}

array_walk($array,'arrayWalkFunc',&$array);

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

?>


And again, a straight loop would be a better approach.
9 out of 10 PHP problems can be resolved by setting
Code: (php) [Select]
error_reporting(E_ALL);
ini_set('display_errors', 1);
php -l <filename> will identify 9 out of the remaining 10 problems
Remember, the command line is your friend
Development Projects: PHPExcel and PHPPowerPoint

Offline lindmTopic starter

  • Enthusiast
  • Posts: 198
    • View Profile
Re: Manipulate array (remove certain keys and renaming keys)
« Reply #4 on: November 30, 2008, 03:18:46 PM »
Is there a good example with a straight loop?

Thanks

Offline Barand

  • Sen . (ile || sei)
  • Staff Alumni
  • 'Mind Boggling!'
  • *
  • Posts: 15,132
  • Gender: Male
  • php 4.3/5.1 MySql 5.0.1
    • View Profile
Re: Manipulate array (remove certain keys and renaming keys)
« Reply #5 on: November 30, 2008, 03:28:33 PM »
Just one problem

Quote from: manual-array_walk
Users may not change the array itself from the callback function. e.g. Add/delete elements, unset elements, etc.
|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

Offline Mark Baker

  • Addict
  • Posts: 1,586
  • Gender: Male
    • View Profile
Re: Manipulate array (remove certain keys and renaming keys)
« Reply #6 on: November 30, 2008, 03:40:43 PM »
Just one problem

Quote from: manual-array_walk
Users may not change the array itself from the callback function. e.g. Add/delete elements, unset elements, etc.
Except my examples actually work because I'm explicitly passing the third parameter to array_walk() by reference;
9 out of 10 PHP problems can be resolved by setting
Code: (php) [Select]
error_reporting(E_ALL);
ini_set('display_errors', 1);
php -l <filename> will identify 9 out of the remaining 10 problems
Remember, the command line is your friend
Development Projects: PHPExcel and PHPPowerPoint

Offline lindmTopic starter

  • Enthusiast
  • Posts: 198
    • View Profile
Re: Manipulate array (remove certain keys and renaming keys)
« Reply #7 on: December 06, 2008, 12:49:30 PM »
Would it be possible to use array_map and unset somehow?
Code: [Select]
$array=$_POST;


function remove($item) {
if (substr($item,-2) == 'py') {
unset($item);
}
}



$b = array_map("remove", $_POST);

echo json_encode($b);


Offline premiso

  • Karma Chameleon
  • Staff Alumni
  • Freak!
  • *
  • Posts: 6,671
  • Gender: Female
  • effing right
    • View Profile
    • PHP Help
Re: Manipulate array (remove certain keys and renaming keys)
« Reply #8 on: December 06, 2008, 01:16:02 PM »
Code: [Select]
function remove(&$item) {
  foreach ($item as $key => $val) {
       if (substr($key,-2) == 'py') {
           unset($item[$key]);
       }
  }
}



$b = array_map("remove", $_POST);

echo json_encode($b);

I think that would work. You have to pass it by reference from what I remember....

Offline lindmTopic starter

  • Enthusiast
  • Posts: 198
    • View Profile
Re: Manipulate array (remove certain keys and renaming keys)
« Reply #9 on: December 06, 2008, 01:48:27 PM »
Tried this:
Code: [Select]
$array = array(
'FIGURE1cy'=>100,
'FIGURE1py'=>100,
'FIGURE2cy'=>100,
'FIGURE2py'=>100 ,
'somethingelse'=>hello,
'somethingelse2'=>hello2
);


function remove(&$item) {
  foreach ($item as $key => $val) {
       if (substr($key,-2) == 'py') {
           unset($item[$key]);
       }
  }
}



$b = array_map("remove", $array);

echo json_encode($b);

But get the error:
Code: [Select]
Warning: Invalid argument supplied for foreach() in /Library/WebServer/Documents/htdocs/phptest2/script/jsonnewyear.php on line 21

Warning: Invalid argument supplied for foreach() in /Library/WebServer/Documents/htdocs/phptest2/script/jsonnewyear.php on line 21

Warning: Invalid argument supplied for foreach() in /Library/WebServer/Documents/htdocs/phptest2/script/jsonnewyear.php on line 21

Warning: Invalid argument supplied for foreach() in /Library/WebServer/Documents/htdocs/phptest2/script/jsonnewyear.php on line 21

Warning: Invalid argument supplied for foreach() in /Library/WebServer/Documents/htdocs/phptest2/script/jsonnewyear.php on line 21

Warning: Invalid argument supplied for foreach() in /Library/WebServer/Documents/htdocs/phptest2/script/jsonnewyear.php on line 21
{"FIGURE1cy":null,"FIGURE1py":null,"FIGURE2cy":null,"FIGURE2py":null,"somethingelse":null,"somethingelse2":null}


Offline premiso

  • Karma Chameleon
  • Staff Alumni
  • Freak!
  • *
  • Posts: 6,671
  • Gender: Female
  • effing right
    • View Profile
    • PHP Help
Re: Manipulate array (remove certain keys and renaming keys)
« Reply #10 on: December 06, 2008, 01:52:38 PM »
Code: [Select]
function remove($item) {
  if (is_array($item)) {
     foreach ($item as $key => $val) {
          if (substr($key,-2) == 'py') {
             unset($item[$key]);
         }
     }
      return $item;
  }
}


$b = remove($_POST);


echo json_encode($b);

Should work as long as $_POST is an array, so make sure you are posting data.

Offline lindmTopic starter

  • Enthusiast
  • Posts: 198
    • View Profile
Re: Manipulate array (remove certain keys and renaming keys)
« Reply #11 on: December 06, 2008, 02:13:08 PM »
Seems to work! Now I have a follow up for another function...

I want to rename all keys ending with "cy" to "py". So

Code: [Select]
$array = array(
'FIGURE1cy'=>100,
'FIGURE2cy'=>100,
'somethingelse'=>hello,
'somethingelse2'=>hello2
);

would become

Code: [Select]
$array = array(
'FIGURE1py'=>100,
'FIGURE2py'=>100,
'somethingelse'=>hello,
'somethingelse2'=>hello2
);

My code so far (based on your code above):

Code: [Select]
function rename($item) {
  if (is_array($item)) {
     foreach ($item as $key => $val) {
          if (substr($key,-2) == 'cy') {
             XXXXXX
         }
     }
      return $item;
  }
}




$b = rename($array);

echo json_encode($b);

Not sure about the XXXX or if this is the best way to handle the problem?

Offline premiso

  • Karma Chameleon
  • Staff Alumni
  • Freak!
  • *
  • Posts: 6,671
  • Gender: Female
  • effing right
    • View Profile
    • PHP Help
Re: Manipulate array (remove certain keys and renaming keys)
« Reply #12 on: December 06, 2008, 03:13:16 PM »
Code: [Select]
function rename($item) {
  if (is_array($item)) {
     foreach ($item as $key => $val) {
          if (substr($key,-2) == 'cy') {
             $key = str_replace("cy", "py", $key);
             $item[$key] = $val;
         }
     }
      return $item;
  }
}


$b = rename($array);

echo json_encode($b);

Offline lindmTopic starter

  • Enthusiast
  • Posts: 198
    • View Profile
Re: Manipulate array (remove certain keys and renaming keys)
« Reply #13 on: December 06, 2008, 04:50:43 PM »
When testing:
Code: [Select]
$array = array(
'FIGURE1cy'=>100,
'FIGURE2cy'=>100,
'somethingelse'=>hello,
'somethingelse2'=>hello2
);

function rename($item) {
  if (is_array($item)) {
     foreach ($item as $key => $val) {
          if (substr($key,-2) == 'cy') {
             $key = str_replace("cy", "py", $key);
             $item[$key] = $val;
         }
     }
      return $item;
  }
}


$b = rename($array);

echo json_encode($b);

Gives the following error:
Code: [Select]
Fatal error: Cannot redeclare rename() in /Library/WebServer/Documents/htdocs/phptest2/script/jsonnewyear.php on line 36

Offline premiso

  • Karma Chameleon
  • Staff Alumni
  • Freak!
  • *
  • Posts: 6,671
  • Gender: Female
  • effing right
    • View Profile
    • PHP Help
Re: Manipulate array (remove certain keys and renaming keys)
« Reply #14 on: December 06, 2008, 05:10:52 PM »
You have the function defined somewhere else. Comment out the old function or rename that one to replace or something like that