I have a simple PHP multi-dimension array that I'm wanting to loop through, specifically the array with the name License and add all the values item that start with the same first word to its own array (with the key set to that word).
The idea is I'd end up with something similar to this:
[desktop] => Array(
[0] => 1–3 users
[1] => 4–6 users
[2] => 7–10 users
)
[web] => Array(
[0] => 10k views/month
[1] => 25k views/month
[2] => 50k views/month
)
[app] => Array(
[0] => 5k downloads
[1] => 5k–10k downloads
[2] => 10k–25k downloads
)
This is an example of the array I'm working with $product['options']
Array
(
[0] => Array
(
[id] => 6059896471689
[product_id] => 4678842515593
[name] => Class
[position] => 1
[values] => Array
(
[0] => Regular
[1] => Oblique
[2] => Family
)
)
[1] => Array
(
[id] => 6355533201545
[product_id] => 4678842515593
[name] => License
[position] => 2
[values] => Array
(
[0] => Desktop 1–3 users
[1] => Desktop 4–6 users
[2] => Desktop 7–10 users
[3] => Desktop 11–15 users
[4] => Desktop 16–25 users
[5] => Desktop 25–50 users
[6] => Desktop 51–100 users
[7] => Desktop 101–150 users
[8] => Desktop 151–250 users
[9] => Desktop 251–400 users
[10] => Desktop 401–700 users
[11] => Desktop 701–1000 users
[12] => Desktop 1000+ users
[13] => Web 10k views/month
[14] => Web 25k views/month
[15] => Web 50k views/month
[16] => Web 100k views/month
[17] => Web 250k views/month
[18] => Web 500k views/month
[19] => Web 750k views/month
[20] => Web 1m+ views/month
[21] => App 5k downloads
[22] => App 5k–10k downloads
[23] => App 10k–25k downloads
[24] => App 25k–50k downloads
[25] => App 50–100k downloads
[26] => App 100k–250k downloads
[27] => App 250k–500k downloads
[28] => App 500k–1m downloads
[29] => App 1m+ downloads
)
)
)
And this is where I'm currently at:
foreach ($product['options'] as $option) {
if ($option['name'] == 'Style') {
$optionTitle = 'Select your fonts';
} elseif ($option['name'] == 'License') {
$optionTitle = 'Please select the type of license(s) you require';
} else {
$optionTitle = '';
}
$optionValues = array();
foreach ($option['values'] as $value) {
if ($option['name'] == 'License') {
// 3 different arrays to be added to $optionValues (desktop, web, app) HOW?
//substr(strstr($value, ' '), 1);
} else {
$optionValues[] = $value;
}
}
if ($option['name'] == 'License') {
$optionValues['desktop'] = ???;
$optionValues['web'] = ???;
$optionValues['app'] = ???;
}
$productOptions[] = array(
'name' => $option['name'],
'title' => $optionTitle,
'values' => $optionValues
);
}
To create the data split up as you want, you can just use explode() with a space, but limit it to 2 items. The first is the type and the second is the range. Then add the items to the $optionValues array using these values.
So something like ...
if ($option['name'] == 'License') {
list($type, $range) = explode(" ", $value, 2);
$optionValues[$type][] = $range;
} else {
You can also remove the following code...
if ($option['name'] == 'License') {
$optionValues['desktop'] = ???;
$optionValues['web'] = ???;
$optionValues['app'] = ???;
}
I have a csv uploader i'm creating to push to an order api. Using phpSpreadsheets toArray method, i have an array like so:
Array
(
[0] => Array
(
[0] => product_sku
[1] => product_qty
[2] => shipping_name
[3] => shipping_address1
[4] => shipping_address2
[5] => shipping_city
[6] => shipping_county
[7] => shipping_postcode
[8] => shipping_type
[9] => customer_id
)
[1] => Array
(
[0] => test_sku_1
[1] => 3
[2] => Bruce Wayne
[3] => The Manor
[4] => Near Arkahm Asylumn
[5] => Gotham
[6] => Greater Gothan
[7] => B17MAN
[8] => 1
[9] => 14994333
)
[2] => Array
(
[0] => test_sku_2
[1] => 2
[2] => Bruce Wayne
[3] => The Manor
[4] => Near Arkahm Asylumn
[5] => Gotham
[6] => Greater Gothan
[7] => B17MAN
[8] => 1
[9] => 14994333
)
[3] => Array
(
[0] => test_sku_3
[1] => 7
[2] => Bruce Wayne
[3] => The Manor
[4] => Near Arkahm Asylumn
[5] => Gotham
[6] => Greater Gothan
[7] => L17MA2
[8] => 1
[9] => 14994333
)
)
Each order will be on a new line, however if a customer orders two different items, i need to group them together using their postcode. As such i was aiming to rearange into this format:
[orders] => Array(
Array(
[shipping_name] => "Bruce wayne",
[customer_id] => 14994333,
[address] => Array(
[shipping_address1] => "The Manor",
[shipping_address2] => "Near Arham Asylumn",
[shipping_city] => "Gotham",
[shipping_county] => "Greater Gotham",
[shipping_postcode] => "B17MAN",
)
[products] => Array(
Array(
[sku] => "test_sku_1",
[quantity] => 3
),
Array(
[sku] => "test_sku_2",
[quantity] => "2"
)
)
)
)
Once of the first problems i encountered was trying to match the postcodes. I managed to get a count using:
$getDuplicates = array_count_values(array_map(function($duplicates) {
return $duplicates[7]; //Where 7 is the postcode
}, $rows));
This worked in counting what i needed correctly. However from there i'm hitting a brick wall. If i'm counting the duplicates, i need it to also make a note of the rows it's already gone through so they aren't pushed incorrectly into the new array i want to make.
Pseudo it should be:
for each rows as row{
if row isn't set to be ignored{
for each count of this postcode{
array_push the product sku and qty
mark these rows as rows to be ignored
}
}
}
Can anyone help me with this?
So, long story short, i've been so caught up in this i completely missed the obvious. To solve this, i took the original array from phpspreadsheet and did an array_multisort:
foreach ($rows as $key => $row) {
$postcodes[$key] = $row[7]; //the postcode key
}
array_multisort($postcodes, SORT_DESC, $rows;
I then just worked my way through the array and if the postcode changed, i'd create a new array, otherwise i'd just push straight into the correct array for the products.
I feel really stupid i didn't think of this before. Thank you to #apokryfos for trying to help!
I try to make a tournament based on Swiss method.
I have for example 12 teams, and in each round, every team has points (0 to 100) and wins, loses or draws.
I want to find which teams play against each other, with these conditions:
Ordered by wins, draws, and points.
Not to be played previously.
In each round, I got foreach team the possible teams to play against in an array like this: (The key indicates the team id, and the values indicate ll possible teams to play separated by ",")
[2] => 4,11,6,10,3,8,7,12,
[5] => 4,11,9,10,3,8,1,12,
[4] => 2,5,6,10,8,7,12,
[11] => 5,9,10,3,8,7,
[9] => 5,11,6,3,8,7,12,
[6] => 2,4,9,10,3,7,12,
[10] => 2,5,4,11,6,8,7,12,
[3] => 5,11,9,6,8,7,
[8] => 2,5,4,11,9,10,3,1,12,
[7] => 2,4,11,9,6,10,3,1,12,
[1] => 5,4,11,9,6,3,8,7,
[12] => 2,5,4,9,6,10,8,7,
First I have all teams played before in a array: (key indicates team id)
Array (
[1] => 2,10,12,
[2] => 1,9,5,
[3] => 4,12,10,
[4] => 3,11,9,
[5] => 6,7,2,
[6] => 5,8,11,
[7] => 8,5,8,
[8] => 7,6,7,
[9] => 10,2,4,
[10] => 9,1,3,
[11] => 12,4,6,
[12] => 11,3,1, )
Then, I get all teams ordered by wins, loses and points in a array: (Keys indicates also team id)
Array
(
[1] => 2
[2] => 5
[3] => 4
[4] => 11
[5] => 9
[6] => 6
[7] => 10
[8] => 3
[9] => 8
[10] => 7
[11] => 1
[12] => 12
)
Finaly I try to find the possible match against two teams.
$checks = array();
$pairs = array();
for ($i = 1; $i <= count($list); $i++) {
for ($j = 1; $j <= count($list); $j++) {
if(strpos($plays[$list[$i]], $list[$j]) !== false || $list[$i] == $list[$j] ) {
}else{
if(!in_array($list[$i],$checks) && !in_array($list[$j],$checks)){
$pairs[] = $list[$i].",".$list[$j];
$checks[] = $list[$i];
$checks[] = $list[$j];
}
}
}
}
And finaly I print the array "$pairs". It shows:
Array
(
[0] => 2,4
[1] => 5,11
[2] => 9,6
[3] => 10,8
[4] => 3,7
)
Thats not correct because the team_id 1 and team_id 12 can not play in this round because the played before:
I don't know how to solve this.
Thanks again!
This is my $part_array:
Array (
[0] => 1015789
[1] => 1029402
[2] => 1031345
[3] => 1036476
[4] => 1061512
[5] => 1065031
[6] => 1069892
[7] => 1070721
[8] => 1073222
[9] => 1074811
)
$next_index = $next_index + 1;
$next_part = ($part_array[$next_index]);
Retrieve part# information using MySQL
Display part# and related information on page. Click on button to display next part# and related information and repeat until end, or user exits.
Each time I increment the $next_index and get the $next_part, the $part_array has the next part # added to the end of the array, so if I have read 3 records, my array now looks like this:
Array (
[0] => 1015789
[1] => 1029402
[2] => 1031345
[3] => 1036476
[4] => 1061512
[5] => 1065031
[6] => 1069892
[7] => 1070721
[8] => 1073222
[9] => 1074811
[10] => 1015789
[11] => 1029402
[12] => 1031345
)
How else could I do this without adding to the array? Does anyone have any suggestions? I am new to PHP and would greatly appreciate any advice.
This is what my array looks like :
Array (
[0] => SimpleXMLElement Object (
[key] => Array (
[0] => Track ID
[1] => Name
[2] => Artist
[3] => Album Artist
[4] => Composer
[5] => Album
[6] => Genre
[7] => Kind
[8] => Size
[9] => Total Time
[10] => Disc Number
[11] => Disc Count
[12] => Track Number
[13] => Year
[14] => Date Modified
[15] => Date Added
[16] => Bit Rate
[17] => Sample Rate
[18] => Play Count
[19] => Play Date
[20] => Play Date UTC
[21] => Artwork Count
[22] => Persistent ID
[23] => Track Type
[24] => Location
[25] => File Folder Count
[26] => Library Folder Count )
[integer] => Array (
[0] => 2056
[1] => 3732918
[2] => 230661
[3] => 1
[4] => 1
[5] => 1
[6] => 1993
[7] => 128
[8] => 44100
[9] => 3
[10] => 3439412487
[11] => 1
[12] => 5
[13] => 1 )
[string] => Array (
[0] => Eye of the Tiger
[1] => Survivor
[2] => Survivor
[3] => Frankie Sullivan/Jim Peterik
[4] => Greatest Hits
[5] => Rock
[6] => MPEG audio file
[7] => 772F0F53F195E705
[8] => File
[9] => file://localhost/Users/cameron/Music/iTunes/iTunes%20Media/Music/Survivor/Greatest%20Hits/01%20Eye%20of%20the%20Tiger.mp3 )
[date] => Array (
[0] => 2012-08-27T17:01:00Z
[1] => 2012-08-27T17:01:03Z
[2] => 2012-12-27T07:21:27Z ) )
that's 1 result, there is about 50 of them repeated.
I am trying to select the artist value in this case : Frankie Sullivan/Jim Peterik
please note: there is about 50 other results that come after this first one, so I would like to do a foreach to display all results.
any suggestions? I am stuck.this is the code I used to get these results:
$xml = simplexml_load_file('Library.xml');
$xmlx = $xml->dict->dict->dict->key;
$artist = $xmlx->xpath("//dict[key='Artist']");
echo "<pre>" . print_r($artist, true) . "</pre>";
It seems that you have an array of SimpleXMLElement,
so you can iterate over your array and use the SimpleXMLElement facilities.
Try:
foreach($yourArray as $simpleXmlElement)
{
// Retrieve the name
echo $simpleXmlElement->string[3];
}
Take a look at the documentation for more question: SimpleXMLElement
For your specific problem, assuming the key and string are synchronized, you can try:
// Loop on your array of SimpleXMLElement
foreach($yourArray as $simpleXmlElement)
{
// Initialize the position and found value
$position = 0;
$found = false;
// Loop on the sub array 'key' and search the 'Artist Album' position
foreach($simpleXmlElement->key as $index => $value)
{
if ($value == "Album Artist")
{
$found = true;
break;
}
// Not found, increment position
$position++;
}
// Retrieve the name if found the artist album key
if ($found)
echo $simpleXmlElement->string[$position];
}
As
[3] => Album Artist
will give the position 3
Then, when retrieving the string value, it will return Frankie Sullivan/Jim Peterik