doutang3077 2014-12-16 18:07
浏览 25
已采纳

PHP创建一个空数组

$number_array = array();

//in a while loop

//part of code
if(array_key_exists($read_num, $number_array))
{
        $arr[$read_num][A] .= $new_A;
}
else
{
$inner_arr = array( "$read_num" => array( "id"=>$read_num, "A"=>$new_A ) );
$number_array = array_push($number_array,$inner_arr);

unset($inner_arr);
}

I get error like:

Warning: array_key_exists() [function.array-key-exists]: The second argument should be either an array or an object in try.php on line 24
Warning: array_push() [function.array-push]: First argument should be an array in try.php on line 33

I declare an empty array outside of the while loop

$number_array = array();

what did i do wrong here that cause the error to appear

EDIT:

I have this value when I did the code above

Array
(
[0] => Array
    (
        [0011] => Array
            (
                [id] => 0011
                [A] => 2.50
                [B] => 2.00
                [C] => 5.40
            )

    )

[1] => Array
    (
        [0017] => Array
            (
                [id] => 0017
                [A] => 5.00
                [B] => 0.00
                [C] => 8.00
            )

    )

[2] => Array
    (
        [0022] => Array
            (
                [id] => 0022
                [A] => 1.00
                [B] => 0.00
                [C] => 1.60
            )

    )

How do I remove the [0] , [1] and [2] and make it as [0011] , [0017], [0022] so my array key exists can work

  • 写回答

1条回答 默认 最新

  • douhuan1979 2014-12-16 18:12
    关注

    It's because you re-assign the return value of array_push to $number_array

    $number_array = array_push($number_array,$inner_arr);
    

    remove it :

    array_push($number_array,$inner_arr);
    

    Because array_push return new number of elements in the array not the array itself.

    Alternatively you can just write:

    $number_array[] = $inner_arr;
    

    It's the same thing.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示