为什么我会得到"未定义的变量错误"从类中返回数组时?

时间:2017-12-01 08:36:27

标签: php arrays oop

我正在体验"未定义的变量"从类返回数组时出错,但我不明白为什么会发生这种情况。

如果我在课堂上print_r($order_ids),则没有问题。只有在我从课堂外尝试function getOrderIds($start_order, $end_order) { $conn = new Database(); $sql = "SELECT order_id FROM oc_order WHERE order_status_id = '17' AND order_id BETWEEN '$start_order' AND '$end_order'"; $result = $conn->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { $order_ids[] = $row['order_id']; } } return($order_ids); } 时才会出现此问题。

CLASS FUNCTIONS FILE

$order = new Order();
$order->getOrderIds($start_order, $end_order); 

print_r($order_ids);

CALL

Notice: Undefined variable: order_ids

RESULT

album_names = ['Ctrl', 'Ctrl', 'Z', 'Ctrl', 'Ctrl', 'Z', 'Ctrl', 'Ctrl', 'Ctrl', 'Ctrl', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Ctrl', 'Ctrl', 'Ctrl', 'Ctrl', 'Ctrl', 'Ctrl', 'Z']
valence = [0.37, 0.598, 0.481, 0.166, 0.413, 0.0798, 0.549, 0.332, 0.348, 0.335, 0.355, 0.22, 0.433, 0.158, 0.357, 0.134, 0.367, 0.237, 0.248, 0.239, 0.535, 0.432, 0.505, 0.142]
energy = [0.579, 0.686, 0.551, 0.367, 0.61, 0.475, 0.488, 0.525, 0.534, 0.517, 0.56, 0.342, 0.688, 0.505, 0.551, 0.63, 0.71, 0.453, 0.518, 0.708, 0.463, 0.684, 0.296, 0.576]`

df = pd.DataFrame([album_names, energy, valence]).T
df.columns = ['album_name', 'energy', 'valence']

1 个答案:

答案 0 :(得分:2)

$order = new Order();
$order_ids = $order->getOrderIds($start_order, $end_order); 

print_r($order_ids);