not getting the desired output from an array - php

I need to create an dynamic array and can't get it right.
I need something like this:
Product Name
top
White
Black
Bottom
Red
Green
I came up with this code, which is producing the above text, so my logic must be about right.
$set = array();
$set['name'] = "Product Name";
$options = array("top", "bottom");
$values['top'] = array("White", "Black");
$values['bottom'] = array("Red", "Green");
echo "<pre>".$set['name']."</pre>";
foreach ($options as $o) {
echo "<pre>- $o</pre>";
$set['options'][]['name'] = $o;
foreach ($values[$o] as $v) {
echo "<pre>-- $v</pre>";
$set['options'][]['values']['name'] = $v;
}
}
The array that is created with the above code is:
Array
(
[name] => Product Name
[options] => Array
(
[0] => Array
(
[name] => top
)
[1] => Array
(
[values] => Array
(
[name] => White
)
)
[2] => Array
(
[values] => Array
(
[name] => Black
)
)
[3] => Array
(
[name] => bottom
)
[4] => Array
(
[values] => Array
(
[name] => Red
)
)
[5] => Array
(
[values] => Array
(
[name] => Green
)
)
)
)
THe output I want is:
Array
(
[name] => Product Name
[options] => Array
(
[0] => Array
(
[name] => top
[values] => Array
(
[0] => Array
(
[name] => White
)
[1] => Array
(
[name] => Black
)
)
)
[1] => Array
(
[name] => bottom
[values] => Array
(
[0] => Array
(
[name] => Red
)
[1] => Array
(
[name] => Green
)
)
)
)
)
What am I missing ?

You got
$set['options'][]['name'] = $o;
^
this one
and
$set['options'][]['values']['name'] = $v;
^
This one
in outer as well as inner loop which was adding new item to array (so there were indexes like 0, 1, 2 ...) so could not produce what you wanted.
You may correct your array like below:
Demo
<?php
$set = array();
$set['name'] = "Product Name";
$options = array("top", "bottom");
$values['top'] = array("White", "Black");
$values['bottom'] = array("Red", "Green");
echo "<pre>".$set['name']."</pre>";
foreach ($options as $o) {
echo "<pre>- $o</pre>";
$vals = array();
foreach ($values[$o] as $v) {
echo "<pre>-- $v</pre>";
$vals[] = array('name' => $v );
}
$set['options'][] = array('name' => $o, 'values' => $vals );
}
print_r($set);
?>

Related

Howto make 1 pocket of the same id from array

<?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'];
}

PHP How to restructure an array?

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.

Bulid for loop based on given array

I am generating dynamic textbox for save and add more functionality and i have to store that data in database i got the array but i dont know how to put that array in to loop so i can get my data.
Array looks like this based on this prepare loop so i can access every element of array:
Array
(
[0] => Array
(
[0] => Array
(
[0] => Array
(
[prem_type] => 1
)
[1] => Array
(
[phase_name] => a1
)
[2] => Array
(
[counter] => 2
)
[3] => Array
(
[block] => A
)
[4] => Array
(
[block] => B
)
)
)
[1] => Array
(
[0] => Array
(
[0] => Array
(
[prem_type] => 1
)
[1] => Array
(
[phase_name] => a2
)
[2] => Array
(
[counter] => 2
)
[3] => Array
(
[block] => A
)
[4] => Array
(
[block] => B
)
)
)
)
Thanks
try this
$array = //your array
foreach($array as $value){
foreach($value as $value2){
foreach($value2 as $value3){
foreach($value3 as $key3 => $value3){
//$key3 is rem_type, phase_name…
//$value3 is required values
}
}
}
}
To get to the data you can use something like:
foreach ($array AS $row) {
$prem_type = $row[0][0]['prem_type'];
$phase_name = $row[0][1]['phase_name'];
$counter = $row[0][2]['counter'];
$block1 = $row[0][3]['block'];
$block2 = $row[0][4]['block'];
}
An alternative is to restructure the array into something more tidy:
$result = array();
foreach ($array AS $rowId => $row) {
$result[$rowId] = array(
'prem_type' => $row[0][0]['prem_type'],
'phase_name' => $row[0][1]['phase_name'],
'counter' => $row[0][2]['counter'],
'block1' => $row[0][3]['block'],
'block2' => $row[0][4]['block']
);
}
This results in:
Array
(
[0] => Array
(
[prem_type] => 1,
[phase_name] => a1,
[counter] => 2,
[block1] => A,
[block2] => B
)
...
)
Here is the answer:
for($i=0;$i<count($data1);$i++){
for($j=0;$j<count($data1[$i]);$j++){
$prem_type =$data1[$i][$j][0]['prem_type'];
$prem_name= $data1[$i][$j][1]['phase_name'];
$no_of_phase= $data1[$i][$j][2]['counter'];
echo $prem_type." ".$prem_name." ".$no_of_phase."<br>";
for($k=3;$k<count($data1[$i][$j]);$k++){
echo $data1[$i][$j][$k]['unitname']."<br>";
}
}
}

[php]How to convert array values to one array value with comma?

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>';
}
}
}

array manipulation and rearranging

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
)
)
)
*/

Categories