value is not inserting into array properly - php

It is correctly insert the value of $name[$init] into the key dsf under the $val['dsf'] but it is inerting only the last value in dsf under the $val['customProduct']['dsf']
$name = array(0=>'Amit',1=>'Amit1',2=>'Amit2');
foreach($order->orderLines as $init =>$val){
$val['dsf'] = $name[$init];
$val['customProduct']['dsf'] = $name[$init];
}

You need the $val pass by reference:
$name = array(0=>'Amit',1=>'Amit1',2=>'Amit2');
$orderLines = array(
array(
'dsf' => array(),
'customProduct' => array()
),
array(
'dsf' => array(),
'customProduct' => array()
),
array(
'dsf' => array(),
'customProduct' => array()
),
);
foreach($orderLines as $init => &$val){ //edit here
$val['dsf']= $name[$init];
$val['customProduct']['dsf'] = $name[$init];
}
print_r($orderLines);
Output:
Array
(
[0] => Array
(
[dsf] => Amit
[customProduct] => Array
(
[dsf] => Amit
)
)
[1] => Array
(
[dsf] => Amit1
[customProduct] => Array
(
[dsf] => Amit1
)
)
[2] => Array
(
[dsf] => Amit2
[customProduct] => Array
(
[dsf] => Amit2
)
)
)
See here

Please Try This and In Case If You Have Any Query Just Comment I Am Happy To Answer
<?php
$name = array(0=>'Amit',1=>'Amit1',2=>'Amit2');
$orderLines = array(
array(
"id"=>1,
"order_id"=>10,
"dfs"=>0,
"customProduct"=>array(
"id"=>102,
"order_id"=>10,
"dfs"=>0,
"name"=>""
)
),
array(
"id"=>2,
"order_id"=>20,
"dfs"=>1,
"customProduct"=>array(
"id"=>105,
"order_id"=>20,
"dfs"=>1,
"name"=>""
)
),
array(
"id"=>3,
"order_id"=>50,
"dfs"=>2,
"customProduct"=>array(
"id"=>107,
"order_id"=>50,
"dfs"=>2,
"name"=>""
)
)
);
$orderLinestemp = array();
foreach($name as $value){
$temp_array = array("dfs"=>$value,"customProduct"=>array("name"=>$value));
array_push($orderLinestemp, $temp_array);
}
$orderLines=array_replace_recursive($orderLines,$orderLinestemp);
echo "<pre/>";
print_r($orderLines);
?>
output

Related

How can I get a key value from two arrays on match?

I have 2 arrays, I'm trying to find any matches and return 'url from $array_full.
I tried array_intersect($array_full, $array_ids), but it doesn't work.
$array_full = array
(
Array
(
'#attributes' => Array
(
'topicid' => 102000,
'url' => 'Velkommen.htm',
'alias' => 'Velkommen'
)
),
Array
(
'#attributes' => Array
(
'topicid' => 130313,
'url' => 'WStation/WAS_Indstillinger.htm',
'alias' => 'WAS_Indstillinger'
)
),
Array
(
'#attributes' => Array
(
'topicid' => 130315,
'url' => 'SPedestal/Applikationer/LoadSharing/Indstillinger.htm',
'alias' => 'LOS_Indstillinger'
)
),
Array
(
'#attributes' => Array
(
'topicid' => 130312,
'url' => 'WStation/WAS_Indstillinger.htm',
'alias' => 'WAS_Indstillinger'
)
)
);
$array_ids = array('130312', '130315');
I expect to get an array of matched url's, like:
array('WStation/WAS_Indstillinger.htm','SPedestal/Applikationer/LoadSharing/Indstillinger.htm')
A simple couple of foreach loops seems the easiest approach
$results = [];
foreach ( $array_full as $a ) {
foreach ( $a as $item ) {
if ( in_array($item['topicid'], $array_ids) ) {
$results[] = $item['url'];
}
}
}
print_r($results);
RESULT
Array
(
[0] => SPedestal/Applikationer/LoadSharing/Indstillinger.htm
[1] => WStation/WAS_Indstillinger.htm
)
You will have to make foreach inside foreach to find item that is matching to ID.
Something like this (not tested, may contain some typos).
foreach($array_ids as $id) {
foreach($array_full as $key => $fullItem) {
if($fillItem['#attributes']['topicid'] != $id) {
continue;
}
//do what you need with $fullItem array
$key; // this is the key you want
}
}
you can use array_map, in_array to get the URL's
$result = [];
array_map(function($v) use ($array_ids,&$result){
$result[] = in_array($v['#attributes']['topicid'], $array_ids) ? $v['#attributes']['url'] : '';
}, $array_full);
Result:-
echo '<pre>';
print_r(array_filter($result));
Array
(
[2] => SPedestal/Applikationer/LoadSharing/Indstillinger.htm
[3] => WStation/WAS_Indstillinger.htm
)

PHP array_push with custom key

I'm trying to merge two arrays which have a custom key using array_push, but when I use array_push it removes the custom key.
For example if I just create a normal array with a custom key it works fine:
$price_arr = array();
$date = '2017-08-01';
$insert_data = array(
$date => array(
'adult_1' => '10'
)
);
print_r($insert_data);
The result is:
Array ( [2017-08-01] => Array ( [adult_1] => 10 ) )
However if I use array push it removes the custom key, for example:
$price_arr = array();
$date = '2017-08-01';
$insert_data = array(
$date => array(
'adult_1' => '10'
)
);
array_push($price_arr, $insert_data);
$insert_data = array(
$date => array(
'child_1' => '2'
)
);
array_push($price_arr, $insert_data);
print_r($price_arr);
The result is:
Array ( [0] => Array ( [2017-08-01] => Array ( [adult_1] => 10 ) ) [1] => Array ( [2017-08-01] => Array ( [child_1] => 2 ) ) )
The result I'm trying to produce is:
Array ( [2017-08-01] => Array ( [adult_1] => 1 [child_1] => 2 ) )
Any help appreciated!
why not just do
$arr['custom_key'] = 'your value';
you do are not bound to use array_push , just assign it and it is done.
$price_arr = array();
$date = '2017-08-01';
$price_arr[$date]['adult_1'] = 10;
$price_arr[$date]['child_1'] = 2;
print_r($price_arr);
You have to use array_merge instead of array_push
$price_arr = array();
$date = '2017-08-01';
$insert_data = array(
$date => array(
'adult_1' => '10'
)
);
$price_arr = array_merge($insert_data);
$insert_data = array(
$date => array(
'child_1' => '2'
)
);
$price_arr[$date] = array_merge($price_arr[$date],$insert_data[$date]);
echo "<pre>";
print_r($price_arr);

php array flip value as key and make it simple

i have been troubling to format array correctly
i have this code:
require('simple_html_dom.php');
$table = array();
$html = file_get_html('apc.html');
foreach($html->find('tr') as $row) {
$rack = ltrim($row->find('td',0)->plaintext);
$location = ltrim($row->find('td',1)->plaintext);
$usage = ltrim($row->find('td',2)->plaintext);
$auk = ltrim($row->find('td',3)->plaintext);
$cost = ltrim($row->find('td',4)->plaintext);
$rack = rtrim($rack);
$location = rtrim($location);
$usage = rtrim($usage);
$auk = rtrim($auk);
$cost = rtrim($cost);
$table[$rack][$usage][$auk][$cost] = true;
}
echo '<pre>';
print_r($table);
echo '</pre>';
using simple_html_dom above i can convert html table to an array as follow:
[Rack01] => Array
(
[741,60] => Array
(
[1.409,04] => Array
(
[267,72] => 1
)
)
[110,88] => Array
(
[210,67] => Array
(
[40,03] => 1
)
)
)
[Rack 09] => Array
(
[843,84] => Array
(
[1.603,30] => Array
(
[304,63] => 1
)
)
)
I would like to have result as below:
array(
[0] => array (
[usage] => 'Rack 01',
[usage] => '741,60',
[auk] => '1.409.04',
[cost] => '267,72')
[1] => array (
[usage] => 'Rack 02',
[usage] => 'value???',
[auk] => 'value???',
[cost] => 'value???')
any help would be apreaciate
Something like this. Also note that trim will do both left and right:
foreach($html->find('tr') as $row) {
$rack = trim($row->find('td',0)->plaintext);
$location = trim($row->find('td',1)->plaintext);
$usage = trim($row->find('td',2)->plaintext);
$auk = trim($row->find('td',3)->plaintext);
$cost = trim($row->find('td',4)->plaintext);
$table[] = array('rack' => $rack,
'usage' => $usage,
'auk' => $auk,
'cost' => $cost);
}

Modify array in loop using php

I have a php multidimensional array(A) and I want to build another array(B) from that array A
My code is
$question = array(
"ques_15" => array(
"name" => array(
"0" => "aaa"
)
),
"ques_16" => array(
"name" => array(
"0" => "bbb",
"1" => "ccc"
)
)
);
$i=0;
foreach($question as $k=>$v)
{
list(,$qid) = explode("_",$k);
$filename .= $question[$k]['name'][$i]."#&";
$insertData[] = array(':quid'=>$qid,':answer'=>$filename);
$i++;
}
echo '<pre>';
print_r($insertData);
echo '</pre>';
It prints
Array
(
[0] => Array
(
[:quid] => 15
[:answer] => aaa#&
)
[1] => Array
(
[:quid] => 16
[:answer] => aaa#&ccc#&
)
)
But I want it to be
Array
(
[0] => Array
(
[:quid] => 15
[:answer] => aaa
)
[1] => Array
(
[:quid] => 16
[:answer] => aaa#&ccc
)
)
$i=0;
foreach($question as $k=>$v)
{
list(,$qid) = explode("_",$k);
$insertData[$i][':quid'] = $qid;
$insertData[$i][':answer'] = implode('#&',$v['name']);
$i++;
}
$filename .= (empty($filename) ? '' : '#&') . $question[$k]['name'][$i];
If aaa#&ccc is a typo and it should be bbb#&ccc, then you can simply do:
foreach($question as $k=>$v)
{
list(,$qid) = explode("_",$k);
$filename = implode("#&", $v['name']);
$insertData[] = array(':quid'=>$qid,':answer'=>$filename);
}
Remove "#&" and place it in condition. It will work;
Just Add a condition.
$filename .= $question[$k]['name'][$i];
if(!empty($filename)){
$filename .= '#&';
}

PHP arrays. There must be a simpler method to do this

I have this array in php returned from db
Array
(
[inv_templates] => Array
(
[0] => Array
(
[inven_subgroup_template_id] => 1
[inven_group] => Wires
[inven_subgroup] => CopperWires
[inven_template_id] => 1
[inven_template_name] => CopperWires6G
[constrained] => 0
[value_constraints] =>
[accept_range] => 2 - 16
[information] => Measured Manual
)
[1] => Array
(
[inven_subgroup_template_id] => 1
[inven_group] => Wires
[inven_subgroup] => CopperWires
[inven_template_id] => 2
[inven_template_name] => CopperWires2G
[constrained] => 0
[value_constraints] =>
[accept_range] => 1 - 7
[information] => Measured by Automated Calipers
)
)
)
I need to output this kind of multidimensional stuff
Array
(
[Wires] => Array
(
[inv_group_name] => Wires
[inv_subgroups] => Array
(
[CopperWires] => Array
(
[inv_subgroup_id] => 1
[inv_subgroup_name] => CopperWires
[inv_templates] => Array
(
[CopperWires6G] => Array
(
[inv_name] => CopperWires6G
[inv_id] => 1
)
[CopperWires2G] => Array
(
[inv_name] => CopperWires2G
[inv_id] => 2
)
)
)
)
)
)
I currently do this stuff
foreach ($data['inv_templates'] as $key => $value) {
$processeddata[$value['inven_group']]['inv_group_name'] = $value['inven_group'];
$processeddata[$value['inven_group']]['inv_subgroups'][$value['inven_subgroup']]['inv_subgroup_id'] = $value['inven_subgroup_template_id'];
$processeddata[$value['inven_group']]['inv_subgroups'][$value['inven_subgroup']]['inv_subgroup_name'] = $value['inven_subgroup'];
$processeddata[$value['inven_group']]['inv_subgroups'][$value['inven_subgroup']]['inv_templates'][$value['inven_template_name']]['inv_name'] = $value['inven_template_name'];
$processeddata[$value['inven_group']]['inv_subgroups'][$value['inven_subgroup']]['inv_templates'][$value['inven_template_name']]['inv_id'] = $value['inven_template_id'];
}
return $processeddata;
EDIT : A var_export
array (
'inv_templates' =>
array (
0 =>
array (
'inven_subgroup_template_id' => '1',
'inven_group' => 'Wires',
'inven_subgroup' => 'CopperWires',
'inven_template_id' => '1',
'inven_template_name' => 'CopperWires6G',
'constrained' => '0',
'value_constraints' => '',
'accept_range' => '2 - 16',
'information' => 'Measured Manual',
),
1 =>
array (
'inven_subgroup_template_id' => '1',
'inven_group' => 'Wires',
'inven_subgroup' => 'CopperWires',
'inven_template_id' => '2',
'inven_template_name' => 'CopperWires6G',
'constrained' => '0',
'value_constraints' => '',
'accept_range' => '1 - 7',
'information' => 'Measured by Automated Calipers',
),
),
)
The foreach is almost unreadable. There must be a simpler way
$processeddata = array();
foreach($data['inv_templates'] as $key => $value) {
$group = $value['inven_group'];
$processeddata[$group]['inv_group_name'] = $group;
$subgroup = &$processeddata[$group]['inv_subgroups'][$value['inven_subgroup']];
$subgroup['inv_subgroup_id'] = $value['inven_subgroup_template_id'];
$subgroup['inv_subgroup_name'] = $value['inven_subgroup'];
$template = $value['inven_template_name'];
$subgroup['inv_templates'][$template]['inv_name'] = $template;
$subgroup['inv_templates'][$template]['inv_id'] = $value['inven_template_id'];
}
return $processeddata;
Untested code. This structures the array in a multidimensional way, and then uses array_merge_recursive to merge them with the already processed data.
if (!isset($processeddata[$value['inven_group']]))
$processeddata[$value['inven_group']] = array();
$processeddata[$value['inven_group']] = array_merge_recursive(
$processeddata[$value['inven_group']],
array(
'inv_group_name' => $value['inven_group'],
'inv_subgroups' => array(
$value['inven_subgroup'] => array(
'inv_subgroup_id' => $value['inven_subgroup_template_id'],
'inv_subgroup_name' => $value['inven_subgroup'],
'inv_templates' => array(
$value['inven_template_name'] => array(
'inv_name' => $value['inven_template_name'],
'inv_id' => $value['inven_template_id'],
),
),
),
),
)
);
I find this format usually works for me. You could do it more efficient, I've just never cared :D
I started traversing at $yourArray['inv_templates'] though.
function groupToStructure(array $rows, array $keys) {
$tree = array();
$keys = array_reverse($keys);
foreach ($rows as $row) {
$subTree = array($row);
foreach ($keys as $key) {
$subTree = array($row[$key] => $subTree);
}
$tree = array_merge_recursive($tree, $subTree);
}
return $tree;
}
print_r(groupToStructure($rows, array('inven_group', 'inven_subgroup', 'inven_template_name')));

Categories