PHP array_push with custom key - php

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);

Related

value is not inserting into array properly

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

PHP merge two arrays where keys match

I'm trying to merge two arrays where the main key matches, I tried using array_merge but the key is just overwritten.
For example I have this array:
$date = '2017-08-01';
$price_arr_1 = array();
$price_arr_1[$date]['adult_1'] = 10;
$price_arr_1[$date]['child_1'] = 2;
print_r($price_arr_1);
Which outputs:
Array ( [2017-08-01] => Array ( [adult_1] => 10 [child_1] => 2 ) )
And I have this array:
$date = '2017-08-01';
$price_arr_2 = array();
$price_arr_2[$date]['adult_2'] = 10;
$price_arr_2[$date]['child_2'] = 2;
print_r($price_arr_2);
Which outputs:
Array ( [2017-08-01] => Array ( [adult_2] => 10 [child_2] => 2 ) )
When I try and merge them like this:
print_r(array_merge($price_arr_1,$price_arr_2));
It output this:
Array ( [2017-08-01] => Array ( [adult_2] => 10 [child_2] => 2 ) )
I want to output this:
Array ( [2017-08-01] => Array ( [adult_1] => 10 [adult_2] => 10 [child_1] => 2 [child_2] => 2 ) )
Appreciated any ideas as to how to achieve the above!
In this case you can use simple array_merge_recursive:
$a1 = Array ( '2017-08-01' => Array ( 'adult_1' => 10, 'child_1' => 2, ) );
$a2 = Array ( '2017-08-01' => Array ( 'adult_2' => 20, 'child_2' => 4, ) );
echo'<pre>',print_r(array_merge_recursive($a1, $a2)),'</pre>';
You should merge with respect to date ($date):
<?php
$date = '2017-08-01';
$price_arr_1 = array();
$price_arr_1[$date]['adult_1'] = 10;
$price_arr_1[$date]['child_1'] = 2;
print_r($price_arr_1);
$date = '2017-08-01';
$price_arr_2 = array();
$price_arr_2[$date]['adult_2'] = 10;
$price_arr_2[$date]['child_2'] = 2;
print_r($price_arr_2);
print_r(array_merge($price_arr_1[$date],$price_arr_2[$date]));
Here is the output:
Array
(
[2017-08-01] => Array
(
[adult_1] => 10
[child_1] => 2
)
)
Array
(
[2017-08-01] => Array
(
[adult_2] => 10
[child_2] => 2
)
)
Array
(
[adult_1] => 10
[child_1] => 2
[adult_2] => 10
[child_2] => 2
)
Working demo: https://eval.in/839408
Are you expecting something like this?
Try this code snippet here
<?php
ini_set('display_errors', 1);
$date = '2017-08-01';
$price_arr_1 = array();
$price_arr_1[$date]['adult_1'] = 10;
$price_arr_1[$date]['child_1'] = 2;
$date = '2017-08-01';
$price_arr_2 = array();
$price_arr_2[$date]['adult_2'] = 10;
$price_arr_2[$date]['child_2'] = 2;
foreach($price_arr_1 as $someDate => $data)
{
if(isset($price_arr_2[$someDate]))
{
$price_arr_1[$someDate]=array_merge($price_arr_1[$someDate],$price_arr_2[$someDate]);
}
else
{
$price_arr_1[$someDate]=$price_arr_2[$someDate];
}
}
print_r($price_arr_1);
Better than putting a bandaid on your code, I will urge you to change your approach entirely. Most simply, avoid using any array functions at all. Just build the result array as you declare each set of elements. Improve your code's performance like this:
Code: (Demo)
$date = '2017-08-01';
$prices[$date]=['adult_1'=>10,'child_1'=>2];
$prices[$date]+=['adult_2'=>10,'child_2'=>2]; // notice the + sign
ksort($prices[$date]); // optionally, you can sort the subarrays ASC by key name
var_export($prices);
Output:
array (
'2017-08-01' =>
array (
'adult_1' => 10,
'adult_2' => 10,
'child_1' => 2,
'child_2' => 2,
),
)

create new array merge

I have an array:
array(
[0] => Array
(
[d1] => Array
(
................
)
[d2] => Array
(
................
)
)
[1] => Array
(
[d1] => Array
(
................
)
[d2] => Array
(
................
)
)
)
How to create a new array to merge it, so only d1 and d2, remove the index 0 and 1.
For PHP > 5.5.0 you can simply use array_column like as
$result['d1'] = call_user_func_array('array_merge',array_column($your_array,'d1'));
$result['d2'] = call_user_func_array('array_merge',array_column($your_array,'d2'));
print_r($result);
Demo
Let us say your array name is $a, so we can have:
$result = [];
$result['d1'] = [];
$result['d2'] = [];
foreach ($a as $v) {
$result['d1'] = array_merge($result['d1'], $v['d1'])
$result['d2'] = array_merge($result['d2'], $v['d2'])
}
Now you have what you want in $result.
here is your solution visit here
Suppose your array $menus and after filter new array will be $filteredMenu . Your final result $filteredMenu
<?php
$menus = array(
0 =>array(
"d1" => array (
"id"=> "----",
),
"d2" => array (
"id"=> "----",
)
),
1 =>array(
"d1" => array (
"id"=> "----",
),
"d2" => array (
"id"=> "----",
)
)
);
$filteredMenu = [];
$filteredMenu['d1'] = [];
$filteredMenu['d2'] = [];
foreach ($menus as $item) {
$filteredMenu['d1'] = array_merge($filteredMenu['d1'], $item['d1']);
$filteredMenu['d2'] = array_merge($filteredMenu['d2'], $item['d2']);
}
print_r($filteredMenu);
?>

Morris.js chart with PHP array

I need your help for this case.
I have an array in PHP.
How can I apply this array:
$visits = $ga->query($params);
Witch gave me something like this:
Array
(
[http_code] => 200
[kind] => analytics#gaData
[rows] => Array
(
[0] => Array
(
[0] => 20141223
[1] => 26
)
[1] => Array
(
[0] => 20141224
[1] => 15
)
...
In this code :
<? function getVisits() {
$morris = new MorrisLineCharts('getVisits');
$morris->xkey = array('date');
$morris->ykeys = array('value');
$morris->labels = array('Money');
$morris->data = array(
array('date' => '20141223', 'value' => 26),
array('date' => '20141224', 'value' => 15),
);
echo $morris->toJavascript();
}
getVisits();
?>
Thanks a lot.
You could loop over the data returned by Google Analytics, to construct an array suitable for Morris.
<? function getVisits( $ga_rows = array() ) {
foreach( $ga_rows as &$_row ) {
$_row = array('date' => $_row [0], 'value' => $_row [1]);
}
$morris = new MorrisLineCharts('getVisits');
$morris->xkey = array('date');
$morris->ykeys = array('value');
$morris->labels = array('Money');
$morris->data = $ga_rows;
echo $morris->toJavascript();
}
// the relevant data from the array you retreived from Google Analytics
getVisits( $google_analytics_data['rows'] );
?>

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

Categories