Jump to content

[SOLVED] Problem: "Undefined index" -how can i declare an index without putting in info?


physaux

Recommended Posts

Ok guys, so i have got this following dilemma:

 

I have an Array declared, and used all without problems, here it is:

 

$Data[$index][$index2]["Name"]

 

The "Name" index works fine because i have declared it for each $index and $index2. The problem comes up as so:

 

I want to have another index, called "Links". So the finished product could be accessed like so:

 

$Data[$index][$index2]["Links"][1] = "sometext";

$Data[$index][$index2]["Links"][2] = "moretext";

$Data[$index][$index2]["Links"][3] = "ahilovewriting...";

 

The problem is that i make the above declarations throughout my code. So i take it i need to have ["Links"][1] to be ready to accept information.

 

So i decided to add the following line to my code when i look through all the indexes:

$Data[$index][$index2]["Links"];

 

But i got the following error: Undefined index: Links in /Users/../ on that line

 

so my question is, how can i "prepare" (or do i need to) my array ["Links"] so that i can do the following operations to it:

 

count(Data[$index][$index2]["Links"])            //GAVE ME AN ERROR BEFORE, i thought it would give me 0.

isset(Data[$index][$index2]["Links"])              //IDK even what happened here, this is later on and i didn't get to it yet without errors.

 

So how can i make this work? Thanks for your help!!!

 

 

What you posted is a little bit confusing (to me anyway).  It may be easier to help you if you post the full code.

 

I came up with a short little example that may (or may not) help you:

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

$vehicles = array();
$vehicles['Ford'][] = 'F250';
$vehicles['Ford'][] = 'F350';

echo "<pre>";
print_r($vehicles['Ford']);
echo "</pre>";
// No errors up to this point because 'Ford' exists.


// Throws an undefined index error because 'Chevy' is not yet defined.
echo "<pre>";
print_r($vehicles['Chevy']);
echo "</pre>";


// Make sure the index exists before doing anything
if (isset($vehicles['Toyota'])) {
echo "<pre>";
print_r($vehicles['Toyota']);
echo "</pre>";
// No error
}


echo "<pre>";
print_r($vehicles['Toyota']);
echo "</pre>";
// Throws an undefined index error because 'Toyota' is not yet defined.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.