<?php
$a = [["product"=>"another2", "id"=>112],["product"=>"xyz", "id"=>113], ["product"=>"lmn", "id"=>113],["product"=>"abc", "id"=>113], ["product"=>"another", "id"=>112]];
$data = [];
$products = [];
foreach ($a as $b) {
$products[]["product"] = $b["product"];
$data[$b["id"]] = $products;
}
echo "<pre>";
print_r($data);
and the output is
Array ( [112] => Array ( [0] => Array ( [product] => another2 ) [1] => Array ( [product] => xyz ) [2] => Array ( [product] => lmn ) [3] => Array ( [product] => abc ) [4] => Array ( [product] => another ) ) [113] => Array ( [0] => Array ( [product] => another2 ) [1] => Array ( [product] => xyz ) [2] => Array ( [product] => lmn ) [3] => Array ( [product] => abc ) ) )
i want to make 1 pocket of same ids.
make 1 pocket if id is 112 from all array. for example i need
Array
(
[112] => Array
(
[0] => Array
(
[product] => xyz
)
[2] => Array
(
[product] => lmn
)
[3] => Array
(
[product] => abc
)
)
[113] => Array
(
[0] => Array
(
[product] => another
)
[1] => Array
(
[product] => another2
)
)
)
)
How can I get this output ? can anyone help me to make this possible. I need 1 array if the id is the same as it makes another array when the id is differents
You are very close. You don't need to define the $products each time. Just loop on the array (saving the $k as the key) and assign.
Consider:
$a = [["product"=>"another2", "id"=>112],["product"=>"xyz", "id"=>113], ["product"=>"lmn", "id"=>113],["product"=>"abc", "id"=>113], ["product"=>"another", "id"=>112]];
$data = [];
foreach ($a as $k => $b) {
$data[$b["id"]][$k]["product"] = $b["product"];
}
Now $data will be your desire output.
Live example: 3v4l
Check below working code:
$a = [["product"=>"another2", "id"=>112],["product"=>"xyz", "id"=>113], ["product"=>"lmn", "id"=>113],["product"=>"abc", "id"=>113], ["product"=>"another", "id"=>112]];
$products = [];
foreach ($a as $b) {
$products[$b["id"]][]["product"] = $b["product"];
}
echo "<pre>";
print_r($products);
Try this one
$arr1 = [];
foreach($arr as $k => $v){
if(array_key_exists($v['id'], $arr1))
$arr1[$v['id']][]['product'] = $v['product'];
else
$arr1[$v['id']][]['product'] = $v['product'];
}
Related
I have a multi-dimensional array. Since the value of the string "volvo" is present twice, I want to combine those keys. Here's the source array:
Array
(
[0] => Array
(
[0] => Volvo
[1] => 22
)
[1] => Array
(
[0] => BMW
[1] => 15
)
[2] => Array
(
[0] => Saab
[1] => 5
)
[3] => Array
(
[0] => Volvo
[1] => 17
)
)
and I'd like to convert it to this one:
Array
(
[0] => Array
(
[0] => Volvo
[1] => 39
)
[1] => Array
(
[0] => BMW
[1] => 15
)
[2] => Array
(
[0] => Saab
[1] => 5
)
)
I think this would make more sense to return an associated array, that way you can do $arr["volvo"], if you're fine with an associated array, just remove the second foreach loop.
If not, this will get the correct output:
<?php
$arr = Array (
Array (
"Volvo",
22
),
Array (
"BMW",
15
),
Array (
"Saab",
5
),
Array (
"Volvo",
17
)
);
$tmpNewArr = Array();
foreach ($arr as $ele) {
if (!isset($arr[$ele[0]])) {
$tmpNewArr[$ele[0]] = 0;
}
$tmpNewArr[$ele[0]] += $ele[1];
}
$newArr = [];
foreach ($tmpNewArr as $key => $ele) {
array_push($newArr,[$key,$ele]);
}
var_dump($newArr);
?>
Here's an eval.in:
https://eval.in/766340
$keyValueCars = [];
foreach($cars as $car){
$brand = $car[0];
$total = $car[1];
if(!isset($keyValueCars[$brand])){
$keyValueCars[$brand] = total;
}
else{
$keyValueCars[$brand] += total;
}
}
You could use
array_unique(Your_array, SORT_REGULAR);
Array
(
[content_type] => Array
(
[0] => story
[1] => delhi
[2] => tez
)
[type] => Array
(
[0] => video_id
[1] => subcategory
[2] => story_id
)
[fetch_id] => Array
(
[0] => 32
[1] => 32
[2] => 2
)
[order] => Array
(
[0] => 6
[1] => 4
[2] => 5
)
[label] => Array
(
[0] => dsfs fdsf dsf sdf
[1] => dfsdfs
[2] => sdfsdfsd
)
[link] => Array
(
[0] => fsd fsdf sdf
[1] => fsdfsdfdsf
[2] => fsdfdsfds
)
[record] => Array
(
[0] => 10
[1] => 8
[2] => 12
)
)
Above is the array I have to sort this array in the basis of order field and it should shorted all the fields accordingly like below example.
$arr['order'][0] = 4;
$arr['order'][1] = 5;
$arr['order'][2] = 6;
$arr['type'][0] = 'subcategory';
$arr['type'][1] = 'story_id';
$arr['type'][2] = 'video_id';
and so on.....
You can try this -
$new = array();
// Extract and get the keys as values
$order = array_flip($array['order']);
// sort them according to keys
ksort($order);
// loop through main array
foreach($array as $key => $sub_array) {
// loop through order
foreach ($order as $o) {
// store the new value according to order
$new[$key][] = $sub_array[$o];
}
}
Demo
Some lesser solution:
asort($array['order']);
foreach ($array as $key => $subArray) {
$array[$key] = array_replace($array['order'], $subArray);
}
For reset a key sequence you may just to use array_values().
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.
Is it possible to connect the key genres, country, in one value
eg [genres] => value1, value2, value3
because there is a problem such that the Values from previous loop are added to the next loop
Here is printscreen http://s12.postimg.org/sv53nakej/Bez_tytu_u.png
1./////////////////////////////////////////////////////////////////////
stdClass Object
(
[title] => Marsjanin
[origTitle] => The Martian
[genres] => Array
(
[0] => Array
(
[key] => 28
[value] => akcja
)
[1] => Array
(
[key] => 33
[value] => sci-fi
)
)
[country] => Array
(
[0] => Array
(
[key] => 53
[value] => USA
)
[1] => Array
(
[key] => 53
[value] => Australia
)
)
)
2./////////////////////////////////////////////////////////////////////
stdClass Object
(
[title] => Pięćdziesiąt twarzy Greya
[origTitle] => Fifty Shades of Grey
[genres] => Array
(
[0] => Array
(
[key] => 16
[value] => melodramat
)
[1] => Array
(
[key] => 33
[value] => sci-fi
)
)
[country] => Array
(
[0] => Array
(
[key] => 53
[value] => USA
)
)
)
This is my code:
foreach( $urll as $urle){
$results = My_Parser::getMovie($urle);
foreach($results as $itemz => $valuez) {
if($itemz=='genres'){
foreach($valuez as $val) {
$genr[] = $val['value'];
}}
}
$genre = implode(', ', $genr);
echo '<tr><th>'.$genre.'</th><th>Importuj</th></tr>';
}
Values from previous loop are added to the next
ex.
A
B
C
D
Result:
A = A
A+B = B
A+B+C = C
A+B+C+D = D
Output [genres]
Array
(
[0] => akcja
[1] => sci-fi
)
Array
(
[0] => akcja / Incorect value added from first array
[1] => sci-fi / Incorect value added from first array
[2] => melodramat
[3] => sci-fi
)
Your $genr is being appended to constantly. Reset it before entering the nested loop:
foreach( $urll as $urle){
...
$genr = array(); // Add this!
foreach($results as $itemz => $valuez) {
...
}
...
$genre = implode(', ', $genr); // Now you're good.
...
}
You need to initialize the array before the foreach of foreach($valuez as $val)
foreach( $urll as $urle){
$results = My_Parser::getMovie($urle);
foreach($results as $itemz => $valuez) {
if($itemz=='genres'){
$genr=array(); //initialization
foreach($valuez as $val) {
$genr[] = $val['value'];
}
$genre = implode(', ', $genr); // use the variable -- As per as your requirement.
echo '<tr><th>'.$genre.'</th><th>Importuj</th></tr>';
}
}
}
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
)
)
)
*/