试图从第二级删除数组项

时间:2013-08-11 16:04:49

标签: php

我试图查看这个,但我没有找到像我一样的阵列。也没有任何我尝试过的工作,现在我在第十次得到了一个恐怖。

我在103/104评论错误 我需要在一个点上从序列化数组得到这个转换(对不起) 我试图创建一个可以寻找需要删除的功能。

注意:我也需要添加一个完整的记录,因为我不知道如何在数组中做到这一点,但我应该问这个问题。

我查找数组的所有内容我都找不到我拥有的内容[0] =>作为顶部数组,当我尝试它的例子时它不会缝合成我所拥有的相同数组。 因此在这里提问。

感谢您的帮助,这是我的PHP代码

<?php
    // version:
$testarray =    
Array(
        "heading" => Array
        (
            "0" => 'Salads',
            "1" => 'Salads',
            "2" => 'Pasta',
        ),

        "special" => Array
        (
            "0" => '',
            "1" => '',
            "2" => '',
        ),

        "item" => Array
        (
            "0" => 'Small Green',
            "1" => 'Regular Caesar',
            "2" => 'Baked Lasagna',
        ),

        "description" => Array
        (
            "0" => 'Grape tomatoes, onions, green peppers and cucumbers on the bed of crisp lettuce.',
            "1" => 'Classic recipe with romaine lettuce and croutons',
            "2" => 'With meat sauce, tomato vegetarian sauce or Alfredo sauce',
        ),

        "price" => Array
        (
            "0" => 'See Desc',
            "1" => '$5.99',
            "2" => '$9.69',
        ),

        "notes" => Array
        (
            "0" => 'Available in Small ($2.99), Regular ($5.99)',
            "1" => '',
            "2" => '',
        ),

        "submit_val" => 'Submit'
    );

// does not work errors at this - Using $this when not in object context in
/*
function recursion($array0) {
    foreach ($array0 as $key0 => $value0) {
    echo $value0;
    if (is_array($value0))
        $this->recursion($value0);
    }
}
echo "recursion array<br>";
echo recursion($testarray);
*/


echo "testarray";
echo "<pre>";
print_r($testarray);
echo "</pre>";  
echo "<hr>";
$removed_array = removeItem(serialize($testarray), '"Submit"'); 
echo "removed array";
echo "<pre>";
print_r($removed_array);
echo "</pre>";  

// need to test this too for if it works or not need to add complete set ie
// header='pasta', special='',item = 'Spaghetti with Meatballs', descriotion = 'Spaghetti with  4 Meatballs', price='$9.99',notes=''
// need to be able to add above line to array [3] line
    function addItem($serializedArray, $item) 
{
   $a = unserialize($serializedArray);
   $a[] = $item;
   return serialize($a);
}

// trying to remove [submit_val] => Submit
global $serializedArray;
$serializedArray = array();
function removeItem($serializedArray, $remove){
    echo "passed array";
echo "<pre>";
print_r($serializedArray); 
echo "</pre>";  
   $a = unserialize($serializedArray);
 echo "search for remove: ".$remove."<br>";
    foreach($a as $key => $value) {
        echo $key . ' = ' . $value . '<br>';
    // get error here as Warning: Invalid argument supplied for foreach() for next line
    foreach ($value as $key2 => $value2) {
          echo $key2 . ' = ' . $value2 . '<br>';    
          if ($value2 == $remove) {
          echo "at value now ".$value2;     
          echo "search for remove: ".$remove;
      unset($a[$key2]);
      echo "unset: ". $a[$key2];
      }// if
      else {
      //echo "didnt find: ".$remove."<br>";
      }// if

    } //for each inner key2

    } // ofr each outer key
    return serialize($a);
}   // function
?>

编辑:很棒,我得到了我的递归测试的答案,但我从数组中删除项目的问题是更远,我试图删除“submit_val”=&gt; “提交”没有运气(这是帖子问题)抱歉我不清楚

如果任何人对下面的建议感兴趣

works now thank you
/**/
function recursion($array0) {
    foreach ($array0 as $key0 => $value0) {
    echo $value0;
    if (is_array($value0))
      return  recursion($value0);
    }
}
echo "recursion array<br>";
echo '<pre>';
echo recursion($testarray);
echo '</pre>';

2 个答案:

答案 0 :(得分:0)

function recursion($array0) {
    foreach ($array0 as $key0 => $value0) {
    echo $value0;
    if (is_array($value0))
        recursion($value0);
    }
}

虽然你不想从函数中返回任何内容吗?

答案 1 :(得分:0)

使用$this会引发错误,因为您在对象中使用它。$this的可接受用法是:

class Test { 
 function recursion($array0) {
    foreach ($array0 as $key0 => $value0) {
    echo $value0;
    if (is_array($value0))
        $this->recursion($value0);
    }
  }

} 

你的功能应该是:

function recursion($array0) {
    foreach ($array0 as $key0 => $value0) {
    echo $value0;
    if (is_array($value0))
        recursion($value0);
    }
}

删除$this应解决错误。