如何在 PHP 中使用 array() 声明关联数组?

Vai*_*mar -7 php xml-parsing

这是给 php 警告

<?php
$xml = simplexml_load_file("videos.xml") or die("Error: Object creation 
Failed");
$videos = array();

foreach( $xml->children() as $video){
   $a= $video->Serial;
   $b=$video->URI;
   $videos[$a] = $b;
}

header('Content-type: application/json');
echo json_encode($videos);
?>
Run Code Online (Sandbox Code Playgroud)

第 8 行中的非法偏移类型。如何修复?

Sam*_*Sam 5

使用键为数组赋值。你可以简单地写:

$files = array();
$files['some_key'] = 'an important value';
$files['another_key'] = 'a value';
$files['key'] = 'an non-important value';
Run Code Online (Sandbox Code Playgroud)

输出:

Array
(
    [some_key] => an important value
    [another_key] => a value
    [key] => an non-important value
)
Run Code Online (Sandbox Code Playgroud)

您也可以通过简单地声明来创建一个数组var[array_key'] = some_value'

例如:

$another['key'] = "WOW... that's cool";
Run Code Online (Sandbox Code Playgroud)

输出:

Array
(
    [key] => WOW... that's cool
)
Run Code Online (Sandbox Code Playgroud)

享受...