i have an array:
Array
(
[users] => Array
(
[101] => Array
(
[odata] => Array
(
[0] => 2
[1] => 1
[2] => 1
[3] => 1
)
)
[200] => Array
(
[odata] => Array
(
[0] => 2
[1] => 1
[2] => 1
[3] => 0
)
)
)
[rjad] => Array
(
[2] => Array
(
[0] => red
[1] => blue
[2] => green
)
)
)
in this array [rjad] key = [odata] key, and [odata] value = second key in [rjad].
What i want is to create new array from old one with user ids values in second array:
like this:
[rjad] => Array
(
[2] => Array
(
[0] => red
[1] => Array
(
[blue] => Array
(
[0] => 101
[1] => 200
)
[2] => green
)
)
Struggling already 3 hours with no success. Any ideas how to get it are very wellcome.
Image do explain relatinships
http://imageshack.us/photo/my-images/818/phparray.jpg/
I'd seriously rethink your structure, but here's a working (if ugly) solution
foreach ($users as $userId => $user) {
foreach ($user['odata'] as $key => $value) {
if (isset($rjad[$key][$value])) {
if (!is_array($rjad[$key][$value])) {
$colour = $rjad[$key][$value];
$rjad[$key][$value] = array();
$rjad[$key][$value][$colour] = array();
} else {
reset($rjad[$key][$value]);
$colour = key($rjad[$key][$value]);
}
$rjad[$key][$value][$colour][] = $userId;
}
}
}
Working demo -> http://codepad.viper-7.com/IEcpvU
Based on comments seems you may want a separate array, which is easily solved
// copy the array and act on it instead of directly on $rjad
$result = $rjad;
foreach ($users as $userId => $user) {
foreach ($user['odata'] as $key => $value) {
if (isset($result[$key][$value])) {
if (!is_array($result[$key][$value])) {
$colour = $result[$key][$value];
$result[$key][$value] = array();
$result[$key][$value][$colour] = array();
} else {
reset($result[$key][$value]);
$colour = key($result[$key][$value]);
}
$result[$key][$value][$colour][] = $userId;
}
}
}
Your question isn't entirely clear - what is the relationship between rjad...blue and those user accounts from the other array? Is item 0 in the odata arrays the default colour?
Related
I have an array that I'd like to restructure. I want to group items by turn. I can figure out how to extract data from the array using foreach($arr['history'] as $obj) my issue is with populating a new array using a loop.
Currently it looks like this:
Array (
[history] => Array (
[id] => 23452435
[legend] => Array (
[0] => Array (
[player] => me
[turn] => 1
[card] => Array (
[name] => foo
)
)
[1] => Array (
[player] => me
[turn] => 1
[card] => Array (
[name] => bar
)
)
[2] => Array (
[player] => opponent
[turn] => 1
[card] => Array (
[name] => derp
)
)
[3] => Array (
[player] => opponent
[turn] => 2
[card] => Array (
[name] => hoo
)
)
)
))
I want it to look like the following, but I can't figure out how to automatically create and populate this structure. This is an array with a sub-array for each turn, containing an array for me and opponent
Array (
[0] => Array (
[me] => Array (
[0] => foo
[1] => bar
)
[opponent] = Array (
[0] => derp
)
)
[1] => Array (
[me] => Array ()
[opponent] => Array (
[0] => hoo
)
))
Thanks.
Edit:
This is what I needed. Thanks for the answers.
$result = [];
foreach ($arr['history'] as $historyItem) {
foreach ($historyItem['legend'] as $list) {
$result[$list['turn']][$list['player']][] = $list['card']['name'];
}
}
Try this:
$result = [];
foreach ($data['history']['legend'] as $list) {
$result[$list['turn']-1][$list['player']][] = $list['card']['name'];
}
Fiddle it! http://ideone.com/BtKOKJ
You can just start adding data to the new array. PHP is extremely forgiving.
$historyByTurns = array();
foreach ($arr['history'] as $historyItem) {
foreach ($historyItem['legend'] as $legendItem) {
$turn = $legendItem['turn'];
$player = $legendItem['player'];
if (!array_key_exists($turn, $historyByTurns)) {
$historyByTurns[$turn] = array();
}
if (!array_key_exists($player, $historyByTurns[$turn])) {
$historyByTurns[$turn][$player] = array();
}
foreach ($legendItem as $card) {
$historyByTurns[$turn][$player][] = $card['name'];
}
}
}
You will have to test it, as I have no way to do that ATM.
i have this array
$items = Array (
[0] => Array ( [0] => xx [1] => 'update')
[1] => Array ( [0] => bx [1] => 'update')
[2] => Array ( [0] => xx [1] => 'creation')
[3] => Array ( [0] => fs [1] => 'creation')
[4] => Array ( [0] => tx [1] => 'update')
[5] => Array ( [0] => bx [1] => 'creation')
)
i'm trying to remove duplicate values based on the first element (xx,bx,ax etc)
if two elements from the same table match, i'd like to keep the last one with higher index,
the result would be like the following
$items = Array (
[0] => Array ( [0] => xx [1] => 'creation')
[1] => Array ( [0] => bx [1] => 'creation')
[2] => Array ( [0] => fs [1] => 'creation')
[3] => Array ( [0] => tx [1] => 'update')
)
keeping the last one was a bit confusing to me as i'm new to PHP.
Thank you in advance
Generic deduplication:
$result = [];
foreach ($array as $item) {
$result[$item[0]] = $item;
}
Functional:
$result = array_reduce($array, function ($acc, $i) {
return [$i[0] => $i] + $acc;
}, [])
Awesome:
array_column($array, null, 0);
Well, i found the solution,
i started bu reversing the array, then executed the following code
$taken = array();
$reverse = array_reverse($items, true);
foreach($reverse as $key => $item) {
if(!in_array($item[0], $taken)) {
$taken[] = $item[0];
} else {
unset($reverse[$key]);
}
}
i have a new problem and my mind is burning, i have an array in php
$test = array();
Array
(
[0] => Array
(
[account] => 14319896
[value] => 725.57
[id] => 280
)
[1] => Array
(
[account] => 163157
[value] => -723.57
[id] => 283
)
[2] => Array
(
[account] => 163157
[value] => 723.57
[id] => 284
)
[3] => Array
(
[account] => 161817
[value] => -723.57
[id] => 285
)
)
i need the accounts, they are more than one in this array, in this example i need $test[1][id] and $test[2][id]
have you an idea? i have no idea more at this time.
Thanks for your help.
Use the account number as key in a new array, count each entry and then take the items with a count > 1
$dupes = array();
foreach($array as $account) {
++$dupes[$account['account']];
}
$dupes = array_filter($dupes, function($count) { return $count > 1; });
Editing to answer the comments below the question …
If you want the IDs (or keys) of the duplicates, do not store the count directly, but use another array instead.
$dupes = array();
foreach($array as $key => $account) {
if(!array_key_exists($account, $dupes))
$dupes[$account['account']] = array();
$dupes[$account['account']][] = $account['id']; // or: = $key
}
$dupes = array_filter($dupes, function($ids) { return count($ids) > 1; });
You should change your architecture to have a associative array where keys are account number organized like that :
Array
(
[14319896] => Array
(
[0]=>Array(
[value] => 725.57
[id] => 280
)
)
[163157] => Array
(
[0]=>Array(
[value] => -723.57
[id] => 283
)
[1]=>Array(
[value] => 723.57
[id] => 284
)
)
[161817] => Array
(
[0]=>Array(
[value] => -723.57
[id] => 285
)
)
)
$dupes = array();
foreach($array as $key => $account) {
$dupes[$account['account']][] = array($account['id'],$account['value']);
}
Whith that architecture you can get everything you want thanks to array_filter :
$dups = array_filter($dupes, function($account){ return count($account)>1;});
How to arrange this array by last inner index ( 0, 1, 2 ) and get the value of the last inner index as the value of each second index:
Array
(
[text] => Array
(
[grid] => Array
(
[0] => 3
[1] => 4
[2] => 5
)
[image] => Array
(
[0] =>
[1] =>
[2] =>
)
[align] => Array
(
[0] => left
[1] => right
[2] => left
)
[title] => Array
(
[0] =>
[1] =>
[2] =>
)
[content] => Array
(
[0] =>
[1] =>
[2] =>
)
)
)
And have the results as below:
Array
(
[text] => Array
(
[0] => Array
(
[grid] => 3
[image] =>
[align] => left
[title] =>
[content] =>
)
[1] => Array
(
[grid] => 4
[image] =>
[align] => right
[title] =>
[content] =>
)
[2] => Array
(
[grid] => 5
[image] =>
[align] => left
[title] =>
[content] =>
)
)
)
This will do the work
function restructure($arr){
$newArr = array();
foreach($arr as $k => $v){
foreach($v as $k1 => $v1){
foreach($v1 as $k2 => $v2){
$newArr[$k][$k2][$k1] = $v2;
}
}
}
return $newArr;
}
As SiGanteng suggested, i dont see other ways than a for/foreach loop:
function buildArray($source, $key = false)
{
// Build the new array
$new_array = array();
// Define groups
$groups = $key === false ? array_keys($source) : array($key);
foreach($groups AS $group)
{
// Get the keys
$keys = array_keys($array[$group]);
// Count the values
$num_entries = count($array[$group][$keys[0]]);
for($i = 0; $i < $num_entries; $i++)
{
foreach($keys AS $key)
{
$new_array[$group][$i][$key] = $array[$group][$key][$i];
}
}
}
return $new_array;
}
This allow you to define the key you need to build; If not specified, the function build the array for every key.
This should work.
function formatit($arr) {
$new = array();
foreach($arr as $k=>$v) {
foreach($v as $k1=>$v1) {
$new[$k1][$k] = $v1;
}
}
return $new;
}
Tested. Call it as
$arr['text'] = formatit($arr['text']);
http://ideone.com/rPzuR
Hell once again, I am wondering how do you go about adding data to a new array index when inside a foreach loop?
the code I have atm is,
// Connect to the database to gather all data pertaiing to the link in question
$assoResult = mysql_query("SELECT * FROM associate_users");
while ($assoRow = mysql_fetch_field($assoResult)) {
$resultArray[] = $assoRow->name;
}
// Connect to the database to gather all data pertaiing to the link in question
$assoResult2 = mysql_query("SELECT * FROM associate_users WHERE id='$getID'");
while ($assoRow2 = mysql_fetch_object($assoResult2)) {
foreach ($resultArray as $row) {
$array = array(array( 1 => $assoRow2->$row, 2 => $row, ),);
echo "<br />"; print_r($array);
}
}
Below is the outputted data that comes from the "echo "br />"; print_r($array);" line.
=================================================================
Array ( [0] => Array ( [1] => 1 [2] => id ) )
Array ( [0] => Array ( [1] => Bob[2] => contactName ) )
Array ( [0] => Array ( [1] => Bob's Tyres [2] => company ) )
Array ( [0] => Array ( [1] => XXXXXXXXXXXXXX [2] => address1 ) )
Array ( [0] => Array ( [1] => XXXXXXXXXXXXXX [2] => address2 ) )
Array ( [0] => Array ( [1] => XXXXXXXXX [2] => address3 ) )
Array ( [0] => Array ( [1] => XXXXXX [2] => postcode ) )
As you can see the array is being created a new over and over, what I need is for the above data to increment the 1st dimension index key on every loop, so it looks like...
=================================================================
Array ( [0] => Array ( [1] => 1 [2] => id ) )
Array ( [1] => Array ( [1] => Bob[2] => contactName ) )
Array ( [2] => Array ( [1] => Bob's Tyres [2] => company ) )
Array ( [3] => Array ( [1] => XXXXXXXXXXXXXX [2] => address1 ) )
Array ( [4] => Array ( [1] => XXXXXXXXXXXXXX [2] => address2 ) )
Array ( [5] => Array ( [1] => XXXXXXXXX [2] => address3 ) )
Array ( [6] => Array ( [1] => XXXXXX [2] => postcode ) )
Thank you in advance I am out of all options in getting this to work and desperate.
Dan.
Change the values assigning part of your code to
$count=0;
foreach ($resultArray as $row) {
$array[$count][1] = $assoRow2->$row
$array[$count][2]=$row;
$count++;
echo "<br />"; print_r($array);
}
This code gets you the output you asked for without inefficiently using two queries:
// Connect to the database to gather all data pertaining to the link in question
$result = mysql_query("SELECT * FROM associate_users WHERE id=" . (int)$getID);
$resultArray = array();
$resultCount = 0;
$row = mysql_fetch_assoc($result);
$count = 0;
foreach ($row as $key => $value) {
$temp = array();
$temp[$count] = array(1 => $value, 2 => $key);
$count++;
echo "<br />"; print_r($temp);
}
Why you want it like this, I have no idea.
//extra code.declaring array
$array = array();
while ($assoRow2 = mysql_fetch_object($assoResult2)) {
foreach ($resultArray as $row) {
// 1st parameter is the array name, here array name is array .give gd name according to use
array_push($array,array( 1 => $assoRow2->$row, 2 => $row, ));
echo "<br />"; print_r($array);
}
}