Php 正在尝试从第二级删除数组项
我试图查找这个,但没有找到像我这样的数组。我尝试过的任何东西都不起作用,现在我已经是第十次得到了一份简历 我在103/104处对错误进行了评论 我需要在某一点上从一个序列化的数组中获取它,因此需要进行转换(抱歉) 我试图做一个功能,可以寻找什么需要删除 注意:我还需要添加一个完整的记录,因为我也不知道如何在数组中这样做,但我可能应该问另一个问题 我查找的所有数组都没有找到我所拥有的数组,它的[0]=>作为顶级数组,当我尝试它的示例时,它并不与我拥有的数组相同。 因此在这里提出问题 感谢您的帮助下面是我的php代码Php 正在尝试从第二级删除数组项,php,Php,我试图查找这个,但没有找到像我这样的数组。我尝试过的任何东西都不起作用,现在我已经是第十次得到了一份简历 我在103/104处对错误进行了评论 我需要在某一点上从一个序列化的数组中获取它,因此需要进行转换(抱歉) 我试图做一个功能,可以寻找什么需要删除 注意:我还需要添加一个完整的记录,因为我也不知道如何在数组中这样做,但我可能应该问另一个问题 我查找的所有数组都没有找到我所拥有的数组,它的[0]=>作为顶级数组,当我尝试它的示例时,它并不与我拥有的数组相同。 因此在这里提出问题 感谢您的帮助下
<?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
?>
尽管您不想从函数中返回任何内容吗?使用$此
会抛出错误,因为您是从对象中使用它的。。$this
的可接受用法为:
你的职能应该是:
删除$此
将解决此错误 我修改了我的问题-请参见底部的编辑注释-谢谢您的回答我排除了这一点并提出了问题。谢谢你在这方面的帮助我修改了帖子的问题谢谢你的回答是的,我也想知道递归的答案-以及如何返回它..再次感谢,并对混淆表示抱歉,请参阅我帖子底部的编辑说明
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>';
function recursion($array0) {
foreach ($array0 as $key0 => $value0) {
echo $value0;
if (is_array($value0))
recursion($value0);
}
}
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);
}
}