Jump to content

[SOLVED] Manipulate array (remove certain keys and renaming keys)


lindm

Recommended Posts

I have the following array:

 

$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.

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>';

?>

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.

Just one problem

 

Users may not change the array itself from the callback function. e.g. Add/delete elements' date=' unset elements, etc. [/quote']

Except my examples actually work because I'm explicitly passing the third parameter to array_walk() by reference;

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....

Tried this:

$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:

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}

 

 

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.

Seems to work! Now I have a follow up for another function...

 

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

 

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

 

would become

 

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

 

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

 

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?

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);

When testing:

$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:

Fatal error: Cannot redeclare rename() in /Library/WebServer/Documents/htdocs/phptest2/script/jsonnewyear.php on line 36

 

Had a final follow up of course...

 

If I want to change the value of all keys ending with py to zero how should I do then (see xxx below):

 

function ny($item) {
  if (is_array($item)) {
     foreach ($item as $key => $val) {
          if (substr($key,-2) == 'py') {
             xxx
         }
     }
      return $item;
  }
}

 

Had a final follow up of course...

 

If I want to change the value of all keys ending with py to zero how should I do then (see xxx below):

 

function ny($item) {
  if (is_array($item)) {
     foreach ($item as $key => $val) {
          if (substr($key,-2) == 'py') {
             xxx
         }
     }
      return $item;
  }
}

 

 

You cant figure that out given the code prior....wow. No offense but I would of hoped you could do that by now by looking at prior code and just basics of php arrays. I would read up on arrays if you are going to be using them alot and learn how they work.

 

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

Thank you for the patience, I am fairly new to arrays, loops etc. One strange thing with the code gathered so far (example below):

$array = array( 
'FIGURE1cy'=>10, 
'FIGURE2cy'=>20,
'FIGURE1py'=>30, 
'FIGURE2py'=>40, 
'somethingelse'=>hello,
'somethingelse2'=>hello2
);

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


     }
      return $item;
  }
}

$b = ny($array);

echo json_encode($b);

 

This returns the following:

{"FIGURE1cy":30,"FIGURE2cy":40,"FIGURE1py":0,"FIGURE2py":0,"somethingelse":"hello","somethingelse2":"hello2"}

 

I was hoping it would have been:

{"FIGURE1cy":0,"FIGURE2cy":0,"FIGURE1py":10,"FIGURE2py":20,"somethingelse":"hello","somethingelse2":"hello2"}

 

The goal is to change this array:

$array = array( 
'FIGURE1cy'=>10, 
'FIGURE2cy'=>20,
'FIGURE1py'=>30, 
'FIGURE2py'=>40, 
'somethingelse'=>hello,
'somethingelse2'=>hello2
);

to

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

 

The 3rd if is overwriting the 0, cause py is being checked again and assigning it $val.

 

Try this:

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

 

Should work like you want it to.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.