0

我试图查找这个,但我没有找到像我这样的数组。我尝试过的任何方法都没有奏效,现在我已经第十次出错了。

我在 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”=>“提交”但没有运气(这是帖子问题)对不起,我不清楚

如果有人从以下建议中获得兴趣

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

2 回答 2

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

虽然你不想从函数中返回任何东西吗?

于 2013-08-11T16:06:59.273 回答
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应该可以解决错误。

于 2013-08-11T16:08:10.153 回答