2017-10-19 1 views
-7

이 어떻게 그것을 해결하기 위해 라인 8에서 PHP는 경고를array()를 사용하여 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); 
?> 

불법 오프셋 유형을주고있다?

+3

http://php.net/manual/fr/function.array.php – Fky

+4

매우 낮은 품질의 플래그. – Adam

+1

'$ files = array();'그리고 나서'$ files [ 'key'] = "value";' – nerdlyist

답변

1

배열에 값을 키로 할당하려면. 당신은 간단하게 작성할 수 있습니다

$files = array(); 
$files['some_key'] = 'an important value'; 
$files['another_key'] = 'a value'; 
$files['key'] = 'an non-important value'; 

출력 :

Array 
(
    [some_key] => an important value 
    [another_key] => a value 
    [key] => an non-important value 
) 

또한 단지 var[array_key'] = some_value'을 진술하여 배열을 만들 수 있습니다.

예를 들어

는 :

$another['key'] = "WOW... that's cool"; 

출력 :

Array 
(
    [key] => WOW... that's cool 
) 

그리고 ... 즐길 ... 정말 PHP는 배열과 매우 느슨한입니다

1

이 무엇을 당신 그러면 :

그러나 인덱스와 연관의 혼합처럼 심지어 뭔가 작동합니다 출력하는

<?php 

$files = array(); 

for($i=0; $i < 10; $i++){ 
    if($i%2 ==0){ 
     $files["Test".$i] = $i; 
    } else { 
     $files[]=$i; 
    } 
} 

echo "<pre>"; 
print_r($files); 

:

Array 
(
    [Test0] => 0 
    [0] => 1 
    [Test2] => 2 
    [1] => 3 
    [Test4] => 4 
    [2] => 5 
    [Test6] => 6 
    [3] => 7 
    [Test8] => 8 
    [4] => 9 
) 
관련 문제