Jump to content

Recommended Posts

Here are two example of the format of the Arrays, full code and array content in my code below.

 

ARRAY 1

 

Array
(
[version] => 1.0
[injuries] => Array
(
[timestamp] => 1377702112
[week] => 1
[injury] => Array
(
[0] => Array
(
[status] => Questionable
[id] => 10009
[details] => Shoulder

)

[1] => Array
(
[status] => Questionable
[id] => 10012
[details] => Ankle

)

 

ARRAY 2

 

Array
(
[version] => 1.0
[players] => Array
(
[timestamp] => 1377676937
[player] => Array
(
[0] => Array
(
[position] => TMDL
[name] => Bills, Buffalo
[id] => 0251
[team] => BUF
)

[1] => Array
(
[position] => TMDL
[name] => Colts, Indianapolis
[id] => 10009
[team] => IND
)

 

 

What I need to do is sort through both Arrays and finding matching values for the ID key. I need to then combine the values of both arrays into one array so I can print it out on screen. Here is how far I have gotten on this problem:

 

 

<?php
 
       function injuries_report() {         
       
       //Get json files
       
         
       //format json data into an array
       
          $obj1 = json_decode($injuryData, true);
         $obj2 = json_decode($playerData, true);
 
       
 
         //return print_r($obj1);   //print obj1 to see output
         
         return print_r($obj2);     //print obj2 to see output
 
       }
 
      ?> 
 
       <!--get injuries report -->

 

      <pre><?php injuries_report(); ?></pre>

 

Link to comment
https://forums.phpfreaks.com/topic/281643-need-help-with-arrays/
Share on other sites

Assuming you want to loop through the players and find matching injuries, you don't have to create one big array - you just need them in the right structure.

 

Create a new injuries array that uses the player ID as the array key.

array(
	10009 => array(status, id, details),
	10012 => array(status, id, details),
	...
)
Note that if you output this new array you'll see that the array keys will be numbers and not strings - if #0251 had an injury it would be under the 251 array key. However PHP will handle the conversion for you so you don't need to worry about it.

 

If it's possible to have multiple injuries on a player then you'll actually need a 2-D array

array(
	10009 => array(
		array(status, id, details),
		array(status, id, details),
		...
	),
	...
)
(since you can't use an array key twice), which implies more complicated logic than simply $id=>$data such as

if (isset(new injuries array[player])) {
	new injuries array[player][] = data
} else {
	new injuries array[player] = array(data)
}
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.