extracting data from xml into custom array

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
PHPPadawan
New php-forum User
New php-forum User
Posts: 4
Joined: Sun May 20, 2018 10:30 am

Sun May 20, 2018 11:39 am

I'm new to PHP and I'd love to get some advice off you guys.
Is my use of ternary logic acceptable or is there a better way to achieve the same goal?
I'm also struggling to extract <card_id>49970</card_id> with my current code... could anyone suggest a solution?

Current code:

Code: Select all

<?php
ini_set('max_execution_time', 0);

error_reporting(E_ALL);
ini_set('display_errors', true);


for ($i=1;$i<=1;$i++){//return 1 to 16
	$url = 'cards_section_'.$i.'.xml';
	echo $url . '<br>';
	$result = scrape($url);
	//cardInfoDB($result, $pdo); //Call DB Function 
	ob_flush();
    flush();
}

$stmt->close();
$pdo->close();

// Scrape the xml file

function scrape($url)
	{

	// Decode xml into json data then an array

	$xml = simplexml_load_file($url);
	$json = json_encode($xml);
	$array = json_decode($json, TRUE);

	// print ("<pre>" . print_r($array, true) . "</pre>");

	foreach($array['unit'] as $array_key => $unit_array)
		{
		$result[] = array(
			'name' => (isset($unit_array['set']) && $unit_array['set'] == 7000 && (isset($unit_array['fusion_level']) && $unit_array['fusion_level'] == 2)) ? $unit_array['name'] : $unit_array['name'] . "-1",
			'card_id' => $unit_array['id'],
			'rarity' => $unit_array['rarity'],
			'card_set' => (isset($unit_array['set'])) ? $unit_array['set'] : null,
			'level' => 1,
			'fusion_level' => (isset($unit_array['fusion_level'])) ? $unit_array['fusion_level'] : 0,
		);
		if (isset($unit_array['upgrade']))
			{

			// loop upgrade's

			foreach($unit_array['upgrade'] as $upgrade_array_key => $upgrade_unit_array)
				{
				if ((end($unit_array['upgrade'])) == $upgrade_unit_array && (isset($upgrade_unit_array['card_id'])))
					{
					$result[] = array(
						'name' => $unit_array['name'],
						'card_id' => $upgrade_unit_array['card_id'],
						'rarity' => $unit_array['rarity'],
						'card_set' => (isset($unit_array['set'])) ? $unit_array['set'] : null,
						'level' => $upgrade_unit_array['level'],
						'fusion_level' => (isset($unit_array['fusion_level'])) ? $unit_array['fusion_level'] : 0,
					);
					}
				  else
					{
					if ((isset($upgrade_unit_array['card_id'])) == $upgrade_unit_array && (isset($upgrade_unit_array['level'])))
						{
						$result[] = array(
							'name' => $unit_array['name'] . "-" . $upgrade_unit_array['level'],
							'card_id' => $upgrade_unit_array['card_id'],
							'rarity' => $unit_array['rarity'],
							'card_set' => (isset($unit_array['set'])) ? $unit_array['set'] : null,
							'level' => $upgrade_unit_array['level'],
							'fusion_level' => (isset($unit_array['fusion_level'])) ? $unit_array['fusion_level'] : 0,
						);
						}
					}
				} 
			} 
		}

	print ("<pre>" . print_r($result, true) . "</pre>");
	return $result;
	} //End of the function
xml:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<root>
	
	<unit>
		<id>49971</id>
		<name>Ascension Altar</name>
		<picture>ascensionaltar</picture>
		<asset_bundle>277</asset_bundle>
		<attack>0</attack>
		<health>20</health>
		<cost>0</cost>
		<rarity>4</rarity>
		<type>6</type>
		<set>7000</set>
		<upgrade>
			<level>2</level>
			<card_id>49970</card_id>
		</upgrade>
	</unit>

	
	<unit>
		<id>55142</id>
		<name>Diesel Leadcar</name>
		<picture>raidertunkcannon_lv1</picture>
		<asset_bundle>280</asset_bundle>
		<attack>9</attack>
		<health>39</health>
		<cost>2</cost>
		<rarity>4</rarity>
		<skill id="overload" n="1" y="2"/>
		<skill id="strike" x="4" all="1"/>
		<skill id="flurry" c="7"/>
		<type>2</type>
		<set>4500</set>
		<upgrade>
			<card_id>55143</card_id>
			<level>2</level>
			<health>40</health>
		</upgrade>
		<upgrade>
			<card_id>55144</card_id>
			<level>3</level>
			<health>42</health>
			<skill id="overload" n="1" y="2"/>
			<skill id="strike" x="4" all="1"/>
			<skill id="flurry" c="6"/>
		</upgrade>
		<upgrade>
			<card_id>55145</card_id>
			<level>4</level>
			<health>44</health>
			<skill id="overload" n="1" y="2"/>
			<skill id="strike" x="5" all="1"/>
			<skill id="flurry" c="6"/>
		</upgrade>
		<upgrade>
			<card_id>55146</card_id>
			<level>5</level>
			<health>46</health>
			<skill id="overload" n="1" y="2"/>
			<skill id="strike" x="6" all="1"/>
			<skill id="flurry" c="6"/>
		</upgrade>
		<upgrade>
			<card_id>55147</card_id>
			<level>6</level>
			<health>48</health>
			<skill id="overload" n="1" y="2"/>
			<skill id="strike" x="6" all="1"/>
			<skill id="flurry" c="5"/>
		</upgrade>
	</unit>
	
	
	<unit>
		<id>25239</id>
		<name>Daedalus Charged</name>
		<picture>brutus_lv6</picture>
		<fusion_level>2</fusion_level>
		<base_commander_id>1504</base_commander_id>
		<health>196</health>
		<rarity>4</rarity>
		<skill id="enrage" x="3" all="1" />
		<skill id="strike" x="6" all="1" />
		<skill id="mimic" x="4" />
		<type>1</type>
		<set>7000</set>
	</unit>
	
	<unit>
		<id>25638</id>
		<name>Octane Rebuilt</name>
		<picture>octane_lv6</picture>
		<asset_bundle>1001</asset_bundle>
		<fusion_level>1</fusion_level>
		<base_commander_id>25583</base_commander_id>
		<health>60</health>
		<rarity>4</rarity>
		<skill id="entrap" x="1" all="1" />
		<skill id="rally" x="3" />
		<skill id="besiege" x="5" />
		<type>1</type>
		<set>7000</set>
		<upgrade>
            <level>2</level>
            <card_id>25639</card_id>
			<health>71</health>
			<skill id="entrap" x="1" all="1" />
			<skill id="rally" x="4" />
			<skill id="besiege" x="5" />
        </upgrade>
		<upgrade>
            <level>3</level>
            <card_id>25640</card_id>
			<health>82</health>
			<skill id="entrap" x="2" all="1" />
			<skill id="rally" x="4" />
			<skill id="besiege" x="5" />
        </upgrade>
		<upgrade>
            <level>4</level>
            <card_id>25641</card_id>
			<health>93</health>
			<skill id="entrap" x="2" all="1" />
			<skill id="rally" x="4" />
			<skill id="besiege" x="6" />
        </upgrade>
		<upgrade>
            <level>5</level>
            <card_id>25642</card_id>
			<health>104</health>
			<skill id="entrap" x="2" all="1" />
			<skill id="rally" x="5" />
			<skill id="besiege" x="6" />
        </upgrade>
		<upgrade>
            <level>6</level>
            <card_id>25643</card_id>
			<health>116</health>
			<skill id="entrap" x="3" all="1" />
			<skill id="rally" x="5" />
			<skill id="besiege" x="7" />
        </upgrade>
	</unit>
	

</root>
desired output for card_id 49970

Code: Select all

(
[name] => Ascension Altar
[card_id] => 49970
[rarity] => 4
[card_set] => 7000
[level] => 2
[fusion_level] => 0
)

chorn
php-forum Fan User
php-forum Fan User
Posts: 551
Joined: Fri Apr 01, 2016 2:18 am

Sun May 20, 2018 10:07 pm

Use XPATH to get the node, then assign every value to a new array as the key you want.

PHPPadawan
New php-forum User
New php-forum User
Posts: 4
Joined: Sun May 20, 2018 10:30 am

Mon May 21, 2018 11:16 am

Thanks! XPATH looks perfect for the job.

Post Reply