数组问题

时间:2014-06-01 13:48:29

标签: php arrays

我在这里看不到问题。我有一个数组,我想通过使用这个代码来获取我需要的信息,但我似乎没有工作,我不知道为什么:

$member = array_slice($members, 0, 1); 
print_r($member);

$event=($member["title"]);
$content=($member["content"]);      
$day=($member["day"]);
$month=($member["month"]);
$year=($member["year"]); 
Array ( 
    [0] => Array ( 
        [title] => Daniel Ryan 
        [content] => Daniel-Ryan Spaulding is an Internationally-Touring Canadian Stand-Up Comedian, who has performed extensively in 35 countries worldwide. His comedy delves into traveling, international politics & gay rights. His intelligent cultural and social observations, high-energy, and brutal-but-polite sense of humor has won him fans across Europe. He appears regularly at the biggest comedy clubs & festivals in Scandinavia, had his hour-long comedy special air on TV2 Zulu, and was the first openly gay comedian to perform throughout Eastern Europe & China ! Get tickets in the door. 100kr 
        [imagename] => aarhuspride.jpg 
        [slug] => 20140517 
        [day] => 17 
        [month] => 05 
        [year] => 2014 
        [id] => 20140517.json 
    ) 
) 

Notice: Undefined index: title in C:\xampp\htdocs\kode\projekter\eksamen_tore\gemdata.php on line 44

Notice: Undefined index: content in C:\xampp\htdocs\kode\projekter\eksamen_tore\gemdata.php on line 45

Notice: Undefined index: day in C:\xampp\htdocs\kode\projekter\eksamen_tore\gemdata.php on line 46

Notice: Undefined index: month in C:\xampp\htdocs\kode\projekter\eksamen_tore\gemdata.php on line 47

Notice: Undefined index: year in C:\xampp\htdocs\kode\projekter\eksamen_tore\gemdata.php on line 48

3 个答案:

答案 0 :(得分:1)

Array ( [0] => Array (是您缺少的部分。你基本上有

array(
    array(
        'title' => 'Dan Ryan',
    )
)

作为建议,不要使用array_slice来提取单个元素子数组,而只需使用$members[$i]来提取单个元素。

答案 1 :(得分:1)

这可能对您有所帮助

$event=($member[0]["title"]);
$content=($member[0]["content"]);      
$day=($member[0]["day"]);
$month=($member[0]["month"]);
$year=($member[0]["year"]); 

你的数组是二维的 您的数据标题,内容等包含在数组$member[0]内 这就是它给你带来未定义索引错误的原因

答案 2 :(得分:0)

您从数组数组开始。 array_slice()返回一个数组,虽然在这种情况下只有一个元素,它本身就是一个数组。

将第一行更改为

$member = $members[0];    // Get the first element of $members

或将索引更改为

$event=($member[0]["title"]);
$content=($member[0]["content"]);      
$day=($member[0]["day"]);
$month=($member[0]["month"]);
$year=($member[0]["year"]);