1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Removing a key in array

Discussion in 'Development' started by Levi, Jun 29, 2018.

  1. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    how would i remove "test" in $back[$sender->getName()] if it exists
    PHP:
    $back = [];
    $item $player->getInventory()->getItemInHand();
    $lore $item->getLore();
    foreach (
    $lore as $lores) {
                            
    $back[$player->getName()] = $lores;
                        }
    foreach (
    $back as $parentKey => $keys) {
                            foreach (
    $keys as $key) {
                                if (
    $key == "test") {
                                    unset(
    $back[$parentKey][$key]);
                                }
                            }
                        }
     
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    huh, unset($back[$parentKey]["test"])?
     
    corytortoise likes this.
  3. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    yes remove test from the parentkey
     
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Yeah, just do that, you don't need to loop through $keys btw
     
  5. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    Code:
     Cannot unset string offsets
     
  6. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Okay, well in that case, just unset "test" from $back directly. No foreach loops.
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.