Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将数组中的数组值存储到PHP变量中_Php_Multidimensional Array_Xml Parsing - Fatal编程技术网

将数组中的数组值存储到PHP变量中

将数组中的数组值存储到PHP变量中,php,multidimensional-array,xml-parsing,Php,Multidimensional Array,Xml Parsing,我使用XML解析器将XML提要数据存储到mysql表中,如果我得到一个XML文件结果作为数组,结果如下所示。如何将每个值存储到变量中 不仅仅是这种类型的数组,如果我们使用不同的XML文件或URL,其结果可能不是多维数组 如何在php变量中存储数组值 <?php function objectsIntoArray($arrObjData, $arrSkipIndices = array()) { $arrData = array(); // if input is o

我使用XML解析器将XML提要数据存储到mysql表中,如果我得到一个XML文件结果作为数组,结果如下所示。如何将每个值存储到变量中

不仅仅是这种类型的数组,如果我们使用不同的XML文件或URL,其结果可能不是多维数组

如何在php变量中存储数组值

   <?php
function objectsIntoArray($arrObjData, $arrSkipIndices = array())
{
    $arrData = array();

    // if input is object, convert into array
    if (is_object($arrObjData)) {
        $arrObjData = get_object_vars($arrObjData);
    }

    if (is_array($arrObjData)) {
        foreach ($arrObjData as $index => $value) {
            if (is_object($value) || is_array($value)) {
                $value = objectsIntoArray($value, $arrSkipIndices); // recursive call
            }
            if (in_array($index, $arrSkipIndices)) {
                continue;
            }
            $arrData[$index] = $value;
        }
    }
    return $arrData;
}


$xmlUrl = "testxmel.xml"; // XML feed file/URL
$xmlStr = file_get_contents($xmlUrl);
$xmlObj = simplexml_load_string($xmlStr);
$arrXml = objectsIntoArray($xmlObj);

李约瑟
53
卢桂珍
31
任何人都可以帮助我解决这个XML解析器结果问题。提前谢谢

// let's assume $myArray is your array

$guest = count( $myArray ); // no. of guests

foreach( $myArray as $guests ) {
   $name = $guests[ 'name' ];
   $age = $guests[ 'age' ];

   // work with this data here...
}
希望这有帮助


希望这有帮助。

使用


使用


数组有什么问题?这是存储此类数据的最佳方式。PHP中不能有多个$name变量。之后您想对这些变量做什么?我的问题是如何将这些值存储在php变量中,如foreach($arrXml as$row){}数组有什么问题?这是存储此类数据的最佳方式。PHP中不能有多个$name变量。之后您想对这些变量做什么?我的问题是如何将这些值存储在php变量中,如以下foreach($arrXml as$row){}它将错误显示为“在E:\wamp\www\testxml.php的第31行为foreach()提供的参数无效”我已将
$myArray
用作数组。用你的数组替换它。我也替换了它,但它不起作用。我认为数组计数是1。这就是它显示无效参数的原因。它将错误显示为“在E:\wamp\www\testxml.php的第31行为foreach()提供的无效参数”我已将
$myArray
用作数组。用你的数组替换它。我也替换了它,但它不起作用。我认为数组计数是1。这就是它显示无效论点的原因。+1表示伟大的答案。我编辑了我的问题,你能看到这个问题并告诉我如何在数据库中存储不同类型的XML结果的解决方案吗?很好的例子。这对我很有用。非常感谢:)丽珠,如果这个答案能满足你的问题,那么请把它记下来。为提供解决方案的VolkerK评分。伟大答案+1。我编辑了我的问题,你能看到这个问题并告诉我如何在数据库中存储不同类型的XML结果的解决方案吗?很好的例子。这对我很有用。非常感谢:)丽珠,如果这个答案能满足你的问题,那么请把它记下来。为提供解决方案的VolkerK评分。
// let's assume $myArray is your array

$guest = count( $myArray ); // no. of guests

foreach( $myArray as $guests ) {
   $name = $guests[ 'name' ];
   $age = $guests[ 'age' ];

   // work with this data here...
}
<?php
$pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'localonly', 'localonly');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
setup($pdo);

$stmt = $pdo->prepare('INSERT INTO soFoo (name, age) VALUES (:name, :age)');
$stmt->bindParam('name', $name);
$stmt->bindParam('age', $age);


$data = data();
// number of elements in $data['guest']
echo count($data['guest']), " new entries<br />\n";

// add data to mysql database
// iterate over elements in  $data['guest']
foreach( $data['guest'] as $row ) {
    // assign "values" to the previously bound parameters
    $name = $row['name'];
    $age = $row['age'];
    // execute preapred statement
    $stmt->execute();
}
$stmt = null;

// retrieve data: only count
// if you only want count the records do not transfer the data from the mysql server to your php process - use Count(*) instead
$row = $pdo->query('SELECT Count(*) from soFoo')->fetch();
echo $row[0], " entries in database<br />\n";

// retrieve data: actual data
$stmt = $pdo->query('SELECT name, age from soFoo', PDO::FETCH_ASSOC);
foreach( $stmt as $row ) {
    echo $row['name'], ' ', $row['age'], "<br />\n";
}
// stmt->rowCount() after a SELECT statement doesn't work with all pdo drivers, but for PDO_MySQL it does
echo $stmt->rowCount(), ' records fetched';

// boilerplate: create temporary database structure ...
function setup($pdo) {
    $pdo->exec('
        CREATE TEMPORARY TABLE soFoo (
            id int auto_increment,
            name varchar(32),
            age int,
            primary key(id)
        )
    ');
}

// boilerplate: a function returning example data to work on...
function data() {
    return array (
        'guest' => array (
            array (
                'name' => 'Joseph Needham',
                'age' => 53
            ),
            array (
                'name' => 'Lu Gwei-djen',
                'age' => 31
            )
        )
    );
}
<?php
$pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'localonly', 'localonly');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
setup($pdo);

$stmt = $pdo->prepare('INSERT INTO soFoo (name, age) VALUES (:name, :age)');
$stmt->bindParam('name', $name);
$stmt->bindParam('age', $age);

// insert data
$data = data();
echo count($data->guest), " new entries<br />\n";
foreach( $data->guest as $guest ) {
    $name = (string)$guest->name;
    $age = (string)$guest->age;
    $stmt->execute();
}
$stmt = null;

// retrieve data: only count
$row = $pdo->query('SELECT Count(*) from soFoo')->fetch();
echo $row[0], " entries in database<br />\n";

// retrieve data: actual data
$stmt = $pdo->query('SELECT name, age from soFoo', PDO::FETCH_ASSOC);
foreach( $stmt as $row ) {
    echo $row['name'], ' ', $row['age'], "<br />\n";
}
echo $stmt->rowCount(), ' records fetched';


function setup($pdo) {
    $pdo->exec('
        CREATE TEMPORARY TABLE soFoo (
            id int auto_increment,
            name varchar(32),
            age int,
            primary key(id)
        )
    ');
}

function data() {
    // return simplexml_load_file('...');
    return new SimpleXMLElement( <<< eox
<?xml version="1.0" encoding="utf-8"?>
<everyone>
  <guest>
    <name>Joseph Needham</name>
    <age>53</age>
  </guest>
  <guest>
    <name>Lu Gwei-djen</name>
    <age>31</age>
  </guest>
</everyone>
eox
    );
}