how to create foreach with this value array - php

i have array strings variable like this
$qtybuy = array('233','235');
$dede = array('test1','test2');
$all = array('exchange' => $dede , 'sell' => $qtybuy);
and if i execute with print_r($all)
i got this code
Array
(
[exchange] => Array
(
[0] => test1
[1] => test2
)
[sell] => Array
(
[0] => 233
[1] => 235
)
)
and my problem is i want to get array like this
Array
(
[0] => stdClass Object
(
[exchange] => test1
[sell] => 233
)
[1] => stdClass Object
(
[exchange] => test2
[sell] => 235
)
)
maybe must use a foreach or loop but i dont know how to change array object with foreach or loop .. maybe someone can help me. and sorry for my bad english..
thanks

<?php
$qtybuy = array('233','235');
$dede = array('test1','test2');
$new = [];
foreach ($qtybuy as $key => $value) {
$new[] = (object)[
'exchange' => $dede[$key],
'sell' => $qtybuy[$key]
];
}
var_dump($new);
As you can see, you can just cast to object :)

Related

Issue when using array_merge to merge multiple arrays into a single one

Before I jump into the code let me display the current representation of the array hierarchy
Array
(
[Details] => Array
(
[0] => Array
(
[0] => stdClass Object
(
[itemId] => 9999
[itemName] => test
[itemPrice] => 0.00
)
)
[1] => Array
(
[0] => stdClass Object
(
[itemId] => 10
[itemName] => yest
[itemPrice] => 12
)
)
)
)
My desired outcome is slightly different
Array
(
[Details] => Array
(
[0] => stdClass Object
(
[itemId] => 9999
[itemName] => test
[itemPrice] => 0.00
)
[1] => stdClass Object
(
[itemId] => 10
[itemName] => yest
[itemPrice] => 12
)
)
)
)
To my understanding it looks like the problem is because my code generate a new array inside of each for eachloop. I tried using Array_merge to try and have them in a single array yet it turned out that it only returned a single stack of elements.
Would really appreciate if someone can help me identify my mistake.
My code
public function getData($list)
{
$itemDetails= array();
foreach ($listas $Item) {
$query = $this->db->get_where('items_table', array('Name' => $Item));
$queryResult = $query->result();
$itemDetails[] = $queryResult;
$result = array_merge($itemDetails,$queryResult);
//$temp = array_merge($toppingDetails,$queryResult);
}
return array('Details'=>$itemDetails);
}
Don't assign to $itemDetails[]. Use array_merge() there.
public function getData($list)
{
$itemDetails= array();
foreach ($listas $Item) {
$query = $this->db->get_where('items_table', array('Name' => $Item));
$queryResult = $query->result();
$itemDetails = array_merge($itemDetails, $queryResult);
}
return array('Details' => $itemDetails);
}

How to combine array value with same key?

So i have this array that i get from query. The array look like this when i print_r
Array
(
[0] => Array
(
[Name] => NAME 1
[Last] => LastValue1
[Bid] =>
[Ask] =>
)
[1] => Array
(
[Name] => NAME 1
[Last] =>
[Bid] => BidValue1
[Ask] =>
)
[2] => Array
(
[Name] => Name 2
[Last] => LastValue2
[Bid] => BidValue2
[Ask] =>
)
[3] => Array
(
[Name] => NAME 1
[Last] =>
[Bid] =>
[Ask] => AskValue1
)
[4] => Array
(
[Name] =>Name 2
[Last] =>
[Bid] =>
[Ask] => AskValue2
)
)
and i want to achieve array looks like this
Array
(
[0] => Array
(
[Name] => NAME 1
[Last] => LastValue1
[Bid] => BidValue1
[Ask] => AskValue1
)
[2] => Array
(
[Name] => Name 2
[Last] => LastValue2
[Bid] => BidValue2
[Ask] => AskValue2
)
)
I try this way (get it from google)
$result = array();
foreach ($newArray as $element) {
$result[$element['Name']][] = $element;
}
echo "<pre>";print_r($result);
But it is not showing the result that i want. How can i achieve it ?
thanks in advance and sorry for my english
You can use below snippet for the same,
$result = [];
foreach ($newArray as $element) {
foreach ($element as $key => $value) {
// checking if value for key is already added to result array
if ((!empty($result[$element['Name']]) && !array_key_exists($key, $result[$element['Name']])) || empty($result[$element['Name']])) {
if (!empty($value)) { // checking if value not empty
$result[$element['Name']] = ($result[$element['Name']] ?? []);
// merge it to group wise name result array
$result[$element['Name']] = array_merge($result[$element['Name']], [$key => $value]);
}
}
}
}
array_merge — Merge one or more arrays
array_key_exists — Checks if the given key or index exists in the array
Demo
Output:-
Array
(
[0] => Array
(
[Name] => NAME1
[Last] => LastValue1
[Bid] => BidValue1
[Ask] => AskValue1
)
[1] => Array
(
[Name] => Name2
[Last] => LastValue2
[Bid] => BidValue2
[Ask] => AskValue2
)
)
Here is the shortest and simple solution by using foreach with array_filter
foreach($a as &$v){
$v = array_filter($v);
isset($r[$v['Name']]) ? ($r[$v['Name']] += $v) : ($r[$v['Name']] = $v);
}
You can use array_values to re arrange the order of array.
Working example : https://3v4l.org/XeQHa

Create a array inside existing array in Php

I have following array and i want to add "image" array inside that array(current array),How can i do this ?
here is my current array
Array
(
[0] => Array
(
[id] => 2
[first_name] => sandeep
[last_name] => sharma
)
[1] => Array
(
[id] => 3
[first_name] => Gaurav
[last_name] => Jain
)
And i want array something like this(want "image array" inside current array)
Array
(
[0] => Array
(
[id] => 2
[first_name] => sandeep
[last_name] => sharma
[image] =array
(
[0] => Array
(
[image] => abc.jpg
)
[1] => Array
(
[image] => abcdef.jpg
)
)
)
[1] => Array
(
[id] => 3
[first_name] => Gaurav
[last_name] => Jain
[image] =array
(
[0] => Array
(
[image] => abcdefghi.jpg
)
)
)
I tried with following code but its showing image array in last,i want image array inside current array,here is my code
$rows = $query->result_array(); //current array
array_push($rows, array("image"=>$image)); //$image is second array(where image save as array)
Try this
foreach ($rows as $i => $row) {
$rows[$i]['image'] = $image;
}
Please find the demo here
Simplest way is to loop your array :
foreach ($rows as $i => $row) {
// $i contains the index, here we push at the end the new array
$rows[$i][] = ['image' => $image];
}
Try this.
$rowsWithImages = array_map(function ($row) {
$row['image'] = $image;
return $row;
}, $rows);
print_r($rowsWithImages);
You can use foreach with pass by reference
foreach($a as &$_v){
$_v['image'] = $image;
}
Live DEMO : https://3v4l.org/VIgYP

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

Categories