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

0 Members and 1 Guest are viewing this topic.

Offline lindmTopic starter

  • Enthusiast
  • Posts: 198
    • View Profile
Re: Manipulate array (remove certain keys and renaming keys)
« Reply #15 on: December 06, 2008, 05:55:48 PM »
Excellent! rename is of course an already built in function so I can't use that name.

Many thanks for the help!

Offline lindmTopic starter

  • Enthusiast
  • Posts: 198
    • View Profile
Re: Manipulate array (remove certain keys and renaming keys)
« Reply #16 on: December 06, 2008, 06:05:57 PM »
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):

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

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 #17 on: December 06, 2008, 06:09:48 PM »
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):

Code: [Select]
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.

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

Offline lindmTopic starter

  • Enthusiast
  • Posts: 198
    • View Profile
Re: Manipulate array (remove certain keys and renaming keys)
« Reply #18 on: December 06, 2008, 06:35:05 PM »
Thank you for the patience, I am fairly new to arrays, loops etc. One strange thing with the code gathered so far (example below):
Code: [Select]
$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:
Code: [Select]
{"FIGURE1cy":30,"FIGURE2cy":40,"FIGURE1py":0,"FIGURE2py":0,"somethingelse":"hello","somethingelse2":"hello2"}
I was hoping it would have been:
Code: [Select]
{"FIGURE1cy":0,"FIGURE2cy":0,"FIGURE1py":10,"FIGURE2py":20,"somethingelse":"hello","somethingelse2":"hello2"}
The goal is to change this array:
Code: [Select]
$array = array(
'FIGURE1cy'=>10,
'FIGURE2cy'=>20,
'FIGURE1py'=>30,
'FIGURE2py'=>40,
'somethingelse'=>hello,
'somethingelse2'=>hello2
);
to
Code: [Select]
$array = array(
'FIGURE1cy'=>0,
'FIGURE2cy'=>0,
'FIGURE1py'=>10,
'FIGURE2py'=>20,
'somethingelse'=>hello,
'somethingelse2'=>hello2
);

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 #19 on: December 06, 2008, 06:38:00 PM »
The 3rd if is overwriting the 0, cause py is being checked again and assigning it $val.

Try this:
Code: [Select]
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.

Offline lindmTopic starter

  • Enthusiast
  • Posts: 198
    • View Profile
Re: Manipulate array (remove certain keys and renaming keys)
« Reply #20 on: December 07, 2008, 02:38:22 AM »
Perfect!

Thank you.