I have an array, which contains data as follows:
Array (
[0] => Array (
[www.google.com] => www.google.com/a
)
[1] => Array (
[www.google.com] => www.google.com/a
)
[2] => Array (
[www.test.com] => www.test.com
)
[5] => Array (
[www.test.com] => www.test.com/c
)
)
I need to grup all links for particular url like this:
Array (
[www.google.com] => Array (
[0] => www.google.com/a
[1] => www.google.com/a
)
[www.test.com] => Array (
[0] => www.test.com
[1] => www.test.com/c
)
)
Please any help for this?
If we call the first array $domains.
$groups = array();
for ($i = 0; $i <= count($domains); $i++)
{
foreach ($domains[$i] as $domain => $url)
{
$groups[$domain][] = $url;
}
}
print_r($groups);
That might work...
Related
Hey Dear I am Making Referal System With 11 Level Of Referal So Ho Can I Show All Referals And There Level I Have Tried
Array
(
[L1] => Array
(
[0] => TL422632
[1] => TL626461
)
[L2] => Array
(
[0] => TL4321
[1] => TL191123
)
[L3] => Array
(
[0] => TL555938
)
[L4] => Array
(
[0] => TL197752
)
[L5] => Array
(
[0] => TL835309
)
[L6] => Array
(
[0] => TL495903
)
[L7] => Array
(
[0] => TL207447
)
[L8] => Array
(
[0] => TL427427
)
[L9] => Array
(
[0] => TL288884
)
[L10] => Array
(
[0] => TL251399
)
[L11] => Array
(
[0] => TL284394
)
)
But I Don't Know Ho To Get L Number By Value For Example I Have TL284394 And I Want To Know Level So It Will Show L11 How Can I Do It
you can use two foreach loop inside each other and array_search() like this:
$a = array("L1" => [ 0 => "TL422632" ],"L2"=> [ 0 => "TL422635" ]);
$x = "TL422635";
foreach($a as $key=>$value){
if($x === $value){
return $key;
}else{
foreach($value as $k=>$v){
if($x === $v){
return array_search([$k=>$v],$a);
}
}
}
}
I have group on string i want to explode by group and sub group
For example
operation.user.add
operation.user.edit
operation.permission.add
performance.tool.add
performance.tool.remove
operation.permission.edit
operation.tool.delete
operation.tool.remove
In want result as following:
Array
(
['operation'] => Array
(
['user'] => Array
(
[0] => 'add',
[1] => 'edit'
),
['permission'] => Array
(
[0] => 'add',
[1] => 'edit'
),
['tool'] => Array
(
[0] => 'delete',
[1] => 'remove'
),
),
['performance'] => Array
(
['tool'] => Array
(
[0] => 'add',
[1] => 'remove'
)
)
)
Can anybody give solution how i can convert string to array as above array?
Assuming that is one string value, this is probably as efficient as you can get:
$str = 'operation.user.add
operation.user.edit
operation.permission.add
performance.tool.add
performance.tool.remove
operation.permission.edit
operation.tool.delete
operation.tool.remove';
$output = [];
foreach (explode("\n", $str) as $string) {
$parts = explode('.', $string);
$output[$parts[0]][$parts[1]][] = $parts[2];
}
print_r($output);
Output:
Array
(
[operation] => Array
(
[user] => Array
(
[0] => add
[1] => edit
)
[permission] => Array
(
[0] => add
[1] => edit
)
[tool] => Array
(
[0] => delete
[1] => remove
)
)
[performance] => Array
(
[tool] => Array
(
[0] => add
[1] => remove
)
)
)
If you store your strings into an array then you can do this like below
<?php
$string = [
"operation.user.add",
"operation.user.edit",
"operation.permission.add",
"performance.tool.add",
"performance.tool.remove",
"operation.permission.edit",
"operation.tool.delete",
"operation.tool.remove",
];
$result = [];
for( $i = 0; $i < count( $string ); $i++ ) {
$parts = explode( '.', $string[$i] );
$result[$parts[0]][$parts[1]][] = $parts[2];
}
echo '<pre>';
print_r($result);
echo '</pre>';
Output:
Array
(
[operation] => Array
(
[user] => Array
(
[0] => add
[1] => edit
)
[permission] => Array
(
[0] => add
[1] => edit
)
[tool] => Array
(
[0] => delete
[1] => remove
)
)
[performance] => Array
(
[tool] => Array
(
[0] => add
[1] => remove
)
)
)
$str = 'operation.user.add
operation.user.edit
operation.permission.add
performance.tool.add
performance.tool.remove
operation.permission.edit
operation.tool.delete
operation.tool.remove';
$output = [];
foreach (explode("\n", $str) as $string) {
$parts = explode('.', $string);
$output[$parts[0]][$parts[1]][] = $parts[2];
}
print_r($output);
Thanks Nick for your answer.
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.
Here's my deal.
I have this array:
Array // called $data in my code
(
[0] => Array
(
[name] => quantity
[value] => 0
)
[1] => Array
(
[name] => var_id
[value] => 4
)
[2] => Array
(
[name] => quantity
[value] => 0
)
[3] => Array
(
[name] => var_id
[value] => 5
)
)
which I need it to be like:
Array // called $temp in my code
(
[0] => Array
(
[0] => Array
(
[name] => quantity
[value] => 0
)
[1] => Array
(
[name] => var_id
[value] => 4
)
)
[2] => Array
(
[0] => Array
(
[name] => quantity
[value] => 0
)
[1] => Array
(
[name] => var_id
[value] => 5
)
)
)
and I did it using this code I made:
$data = $_POST['data'];
$temp = array();
foreach($data as $key => $datum)
{
if($key%2 == 0)
{
$temp[$key] = array();
array_push($temp[$key], $datum, $data[$key+1]);
}
}
But I think that my code is some kinda stupid, specially if I have a huge data.
eventually what I want to do is just have each two indexes combined in one array, and I know that there should be something better than my code to do it, any suggestions?
Discover array_chunk()
$temp = array_chunk($data, 2);
$cnt = count($data);
$temp = array();
for ($i = 0; $i < $cnt; $i = $i + 2)
{
$temp[] = array($data[$i], $data[$i+1]);
}
Take a look at array_chunk.
<?php
$array = array(
array(1),
array(2),
array(3),
array(4),
);
print_r(
array_chunk($array, 2, false)
);
/*
Array
(
[0] => Array
(
[0] => Array
(
[0] => 1
)
[1] => Array
(
[0] => 2
)
)
[1] => Array
(
[0] => Array
(
[0] => 3
)
[1] => Array
(
[0] => 4
)
)
)
*/
I have an array like this..
Array
(
[0] => Array
(
[0] => 1``
[1] => 2``
[2] => 3``
)
[1] => Array
(
[0] => 4``
[1] => 5``
[2] => 6``
)
[2] => Array
(
[0] =>
[1] => 7``
[2] =>
)
)
I want the result like this below,
$remaining_value = Array
(
[0] => 1`` 4``,
[1] => 2`` 5`` 7``,
[2] => 3`` 6``,
)
How to do this in an single loop.. Plz help me..
If the lower-level arrays will always have the same number of elements then you can do something like this:
$subArrayCount = count( $inputArray );
$outputArray = array();
$firstSubArray = reset( $inputArray );
foreach( $firstSubArray as $key => $value )
{
$outputArray[$key] = $value;
for( $innerLoop = 1; $innerLoop < $subArrayCount; $innerLoop++ )
{
$outputArray[$key].= $inputArray[$innerLoop][$key];
}
}
var_dump( $outputArray );
This should work:
<?php
$remaining_value=array();
foreach($array as $loopv1){
foreach($loopv1 as $key2 => $loopv2){
if(empty($remaining_value[$key2]))$remaining_value[$key2]=$loopv2; else $remaining_value[$key2].=" ".$loopv2;
}
}
?>