I need to add another another element to each level of the array (sorry, think that is bad terminology).
I have an array -
Array ( [0] => Array (
[actor_rt_id] => 162683283,
[item_number] => 3 )
[1] => Array (
[actor_rt_id] => 162657351,
[item_number] => 5 )
)
This code produces the array. The commented out line is what I tried to add to the array. The code before the comment creates the array.
$data_itemone['actor_rt_id'] = $this->input->post('actor_id');
$data_itemtwo['item_number'] = $this->input->post('item_number');
$data_item = array_merge($data_itemone, $data_itemtwo);
$res = [];
foreach($data_item as $key => $value){
foreach ($value as $data => $thevalue) {
$res[$data][$key] = $thevalue;
//$res['film_id'] = $film_id;
}
}
I have an another variable I need to add from post which is a single string.
$film_id = $this->input->post('film_id');
I need it to be in the array like so -
Array ( [0] => Array (
[actor_rt_id] => 162683283,
[item_number] => 3,
[film_id] => 52352
)
[1] => Array (
[actor_rt_id] => 162657351,
[item_number] => 5,
[film_id] => 52352
)
)
...but my code (uncommented) produces -
Array ( [0] => Array (
[actor_rt_id] => 162683283,
[item_number] => 3
)
[film_id] => 16639,
[1] => Array
( [actor_rt_id] => 162657351,
[item_number] => 5 )
)
Tried a few things. Can't seem to get it to work.
Change
$res['film_id'] = $film_id;
to
$res[$data]['film_id'] = $film_id;
this will add it to the right array.
How if you try this.
$data_itemone['actor_rt_id'] = [123, 245];
$data_itemtwo['item_number'] = [456, 789];
$film_id = 52352;
$data_item = array_merge($data_itemone, $data_itemtwo);
$res = [];
foreach($data_item as $key => $value){
foreach ($value as $data => $thevalue) {
$res[$data][$key] = $thevalue;
$res[$data]['film_id'] = $film_id;
}
}
print_r($res);
Try this one
<?
$data_itemone['actor_rt_id'] = $this->input->post('actor_id');
$data_itemtwo['item_number'] = $this->input->post('item_number');
$film_id = $this->input->post('film_id');
$data_item = array_merge($data_itemone, $data_itemtwo);
$res = [];
foreach($data_item as $key => $value){
foreach ($value as $data => $thevalue) {
$res[$data][$key] = $thevalue;
$res[$data]['film_id'] = $film_id;
}
}
?>
Related
i want to know if it's possible to change an array structure, currently i'm receiving this array:
[{"ano":["2004","2006"]},{"ano":["2006",""]},{"ano":["2011",""]},{"ano":["2013",""]}]
I need those dates split one by row like this:
[{"ano":"2004"},{"ano":"2006"},{"ano":"2006"},{"ano":"2011"}]
So, basically i'm think i could clean empty and duplicated values, and then split the array or something?
I'm using PHP and MySQL SELECT to return those values like this:
while($ano = $pegaAno->fetchObject()){
$ar1 = array("ano" => $ano->inicio);
$ar2 = array("ano" => $ano->fim);
$result = array_merge_recursive($ar1, $ar2);
//print_r($result);
array_push($return_arr,$result);
}
Any help please?
while($ano = $pegaAno->fetchObject())
array_push($return_arr, array("ano" => $ano->inicio), array("ano" => $ano->fim));
$return_arr = array_unique($return_arr);
Perhaps you want something like this? So you can have an array as
0 => [ "ano" => value ]
1 => [ "ano" => value ]
2 => [ "ano" => value ]
//etc...
You may also try this code
$input[0]['anno'] = array("2004","2006");
$input[1]['anno'] = array("2008","");
$input[2]['anno'] = array("2002","2006");
$input[3]['anno'] = array("2004","2013");
$arr = array();
foreach ($input as $value) {
foreach ($value as $key => $value1) {
foreach ($value1 as $value2) {
$arr[] = $value2;
}
}
}
$resulted = array_filter(array_unique($arr));
$resulted_array = array();
foreach ($resulted as $value) {
$resulted_array[][$mainkey] = $value;
}
print_r($resulted_array);
//RESULT
Array
(
[0] => Array
(
[anno] => 2004
)
[1] => Array
(
[anno] => 2006
)
[2] => Array
(
[anno] => 2008
)
[3] => Array
(
[anno] => 2002
)
[4] => Array
(
[anno] => 2013
)
)
Here is the raw data
Array
(
[name] => me
[tickets] => Array
(
[1] => Array
(
[equipment] => Array
(
[1] => Array
(
[name] => DVR
[received] => 10
)
[2] => Array
(
[name] => DCT
[received] => 3
)
)
)
[2] => Array
(
[equipment] => Array
(
[1] => Array
(
[name] => DVR
[received] => 4
)
[2] => Array
(
[name] => DCT
[received] => 6
)
)
)
)
)
Users have multiple tickets, but each ticket has the same item with different 'received' amounts. I would like to sum the received amount into one variable/array.
Here is a demo of how I would like to get it to work like
Array
(
[name] => me
[equipment] => Array
(
[DVR] => 14
[DCT] => 9
)
)
Here is my most recent failed attempt at building my own array from a multidimensional array.
foreach($data as $user){
$sum = [];
$sum['name'] = $user->name;
$sum['equipment'] = [];
foreach($user->tickets as $ticket){
foreach($ticket->equipments as $eqpt){
$sum['equipment'][$eqpt['name']] += $eqpt['pivot']['received'];
}
}
print_r($sum);
}
Please try the following code. There's only a single user in your $data, though, so you need to do $data = [$data]; first.
foreach ($data as $user) {
$sum = [];
$sum['name'] = $user['name'];
$sum['equipment'] = [];
foreach($user['tickets'] as $ticket){
foreach($ticket['equipment'] as $eqpt){
$sum['equipment'][$eqpt['name']] += $eqpt['received'];
}
}
print_r($sum);
}
PHP arrays are accessed with square bracket syntax
There's probably a typo in $ticket->equipments.
Try with this:
$array = $data; //$data is your array
$sum = array('name' => $array['name'], 'equipment' => array());
foreach($array['tickets'] as $row) {
for($i = 0; $i < count($row); $i++) {
foreach($row['equipment'] as $infos) {
$sum['equipment'][$infos['name']] += $infos['received'];
//print_r($infos);
}
}
}
print_r($sum);
well, after much googling and trial and error this appears to work
$sum = [];
// $data is a collection returned by Laravel
// I am converting it to an array
foreach($data->toArray() as $user){
$items = [];
foreach($user['tickets'] as $ticket){
foreach($ticket['equipments'] as $eqpt){
$name = $eqpt['name'];
if (! isset($items[$name]))
{
$items[$name] = $eqpt['received'];
} else {
$items[$name] += $eqpt['received'];
}
}
}
$sum[] = [
'name' => $user['name'],
'equipment' => $items
];
}
#tsnorri #Adrian Cid Almaguer
I want to create a list where if its already in the array to add to the value +1.
Current Output
[1] => Array
(
[source] => 397
[value] => 1
)
[2] => Array
(
[source] => 397
[value] => 1
)
[3] => Array
(
[source] => 1314
[value] => 1
)
What I want to Achieve
[1] => Array
(
[source] => 397
[value] => 2
)
[2] => Array
(
[source] => 1314
[value] => 1
)
My current dulled down PHP
foreach ($submissions as $timefix) {
//Start countng
$data = array(
'source' => $timefix['parent']['id'],
'value' => '1'
);
$dataJson[] = $data;
}
print_r($dataJson);
Simply use an associated array:
$dataJson = array();
foreach ($submissions as $timefix) {
$id = $timefix['parent']['id'];
if (!isset($dataJson[$id])) {
$dataJson[$id] = array('source' => $id, 'value' => 1);
} else {
$dataJson[$id]['value']++;
}
}
$dataJson = array_values($dataJson); // reset the keys - you don't nessesarily need this
This is not exactly your desired output, as the array keys are not preserved, but if it suits you, you could use the item ID as the array key. This would simplify your code to the point of not needing to loop through the already available results:
foreach ($submissions as $timefix) {
$id = $timefix['parent']['id'];
if (array_key_exists($id, $dataJson)) {
$dataJson[$id]["value"]++;
} else {
$dataJson[$id] = [
"source" => $id,
"value" => 1
];
}
}
print_r($dataJson);
You should simplify this for yourself. Something like:
<?
$res = Array();
foreach ($original as $item) {
if (!isset($res[$item['source']])) $res[$item['source']] = $item['value'];
else $res[$item['source']] += $item['value'];
}
?>
After this, you will have array $res which will be something like:
Array(
[397] => 2,
[1314] => 1
)
Then, if you really need the format specified, you can use something like:
<?
$final = Array();
foreach ($res as $source=>$value) $final[] = Array(
'source' => $source,
'value' => $value
);
?>
This code will do the counting and produce a $new array as described in your example.
$data = array(
array('source' => 397, 'value' => 1),
array('source' => 397, 'value' => 1),
array('source' => 1314, 'value' => 1),
);
$new = array();
foreach ($data as $item)
{
$source = $item['source'];
if (isset($new[$source]))
$new[$source]['value'] += $item['value'];
else
$new[$source] = $item;
}
$new = array_values($new);
PHP has a function called array_count_values for that. May be you can use it
Example:
<?php
$array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));
?>
Output:
Array
(
[1] => 2
[hello] => 2
[world] => 1
)
I have a array here:
$array = array(
array('t'=>'t1','v'=>'001'),
array('t'=>'t2','v'=>'002'),
array('t'=>'t3','v'=>'003'),
array('t'=>'t1','v'=>'004'),
array('t'=>'t4','v'=>'005'),
array('t'=>'t2','v'=>'006'),
array('t'=>'t5','v'=>'007'),
array('t'=>'t3','v'=>'008'),
);
The final array i want is this :
array(
't1' => array('v'=>array(001,004)),
't2' => array('v'=>002),
't3' => array('v'=>array(003,008)),
't4' => array('v'=>005),
't5' => array('v'=>006),
't6' => array('v'=>007)
)
Is there any way i can achieve the final array using php array manipulation functions? I don't want to use any loops (for or foreach). Tried doing using usort() but getting no where
Here is my usort code that calls user defined function:
public function sort($a,$b)
{
$const = array();
$temp1 = array();
$temp2 = array();
//echo $a['t'].":".$a['v'] . " - " . $b['t'].":".$b['v']. "<br/>";
if($a['t'] == $b['t']){
$temp1[$a['t']] = array($a['v'],$b['v']);
//$const = $temp1;
}else{
if(!array_key_exists($a['t'],$temp1) && !array_key_exists($b['t'],$temp1)){
$temp2[$a['t']] = array($a['v']);
$temp2[$b['t']] = array($b['v']);
}
}
$result = array_merge($temp1, $temp2);
print_r($result);
}
$array = array(
array('t'=>'t1','v'=>'001'),
array('t'=>'t2','v'=>'002'),
array('t'=>'t3','v'=>'003'),
array('t'=>'t1','v'=>'004'),
array('t'=>'t4','v'=>'005'),
array('t'=>'t2','v'=>'006'),
array('t'=>'t5','v'=>'007'),
array('t'=>'t3','v'=>'008'),
);
$res = array();
foreach($array as $val){
$res[$val['t']]['v'][] = $val['v'];
}
echo "<pre>";
print_r($res);
Output :
Array
(
[t1] => Array
(
[v] => Array
(
[0] => 001
[1] => 004
)
)
[t2] => Array
(
[v] => Array
(
[0] => 002
[1] => 006
)
)
[t3] => Array
(
[v] => Array
(
[0] => 003
[1] => 008
)
)
[t4] => Array
(
[v] => Array
(
[0] => 005
)
)
[t5] => Array
(
[v] => Array
(
[0] => 007
)
)
)
<?php
$array = array(
array('t'=>'t1','v'=>'001'),
array('t'=>'t2','v'=>'002'),
array('t'=>'t3','v'=>'003'),
array('t'=>'t1','v'=>'004'),
array('t'=>'t4','v'=>'005'),
array('t'=>'t2','v'=>'006'),
array('t'=>'t5','v'=>'007'),
array('t'=>'t3','v'=>'008'),
);
foreach ($array as $key => $row) {
$t[$key] = $row['t'];
$v[$key] = $row['v'];
}
array_multisort($v, SORT_ASC, $array);
$reqArray = array();
foreach($array as $value)
{
$reqArray[$value['t']]['v'][] = $value['v'];
}
print_r($reqArray);
?>
Try with:
$input = array( /* your data */ );
$output = array();
foreach ( $input as $value ) {
$t = $value['t'];
$v = $value['v'];
if ( !isset($output[$t]) ) {
$output[$t] = array('v' => $v);
} else if ( is_array($output[$t]['v']) ) {
$output[$t]['v'][] = $v;
} else {
$output[$t]['v'] = array($output[$t]['v'], $v);
}
}
foreach ($this->CsInventory as $value)
{
print_r($value) // print 1
$vname = $value[] = $value['VesselName'];
$total = $value[] = $value['Total'];
$Box = $value[] = $value['Box'];
print_r($value); // print 2
$rdata .= '<td>'.$vname.'</td>
<td>'.$total.'</td>
<td>'.$Box.'</td>';
}
Print 1
Array
(
[VesselName] => MARIANNE
[Total] => 13838
[Box] => 1156
)
Array
(
[Box] => 154
)
Array
(
[Box] => 3825
)
Array
(
[Box] => 50571
)
print 2
Array
(
[VesselName] => MARIANNE
[Total] => 15452
[Box] => 1156
[0] => MARIANNE
[1] => 15452
[2] => 1156
)
Array
(
[Box] => 2276
[0] =>
[1] =>
[2] => 2276
)
Array
(
[Box] => 3825
[0] =>
[1] =>
[2] => 3825
)
Array
(
[Box] => 49235
[0] =>
[1] =>
[2] => 49235
)
i how can i remove an empty value in the array? i try many ways but i can get any solution.. so decide to here in the forum?
I'd try to reduce effort.
foreach ($this->CsInventory as $value)
{
foreach ($value as $key => $item)
{
$value[] = $item;
$rdata .= "<td>$item</td>";
}
print_r($value)
}
As a general comment, not sure why you're adding anonomous values back to the $values stack, might be better to use a different array.
If you have specific array elements you want to get rid of, you can use unset($array[$key]);
You could also prevent them getting into the array in the first place by using
if($value['VesselName']) {$vname = $value[] = $value['VesselName'];}
instead of simply
$vname = $value[] = $value['VesselName'];
function array_remove_empty($arr){
$narr = array();
while(list($key, $val) = each($arr)){
if (is_array($val)){
$val = array_remove_empty($val);
// does the result array contain anything?
if (count($val)!=0){
// yes :-)
$narr[$key] = $val;
}
}
else {
if (trim($val) != ""){
$narr[$key] = $val;
}
}
}
unset($arr);
return $narr;
}
array_remove_empty(array(1,2,3, '', array(), 4)) => returns array(1,2,3,4)