help needed restructuring a php array - php

I was wondering if anyone could help me restructure a predefined php array. The output of my current array is:
Array
(
[71-ctns] => 1
[71-units] => 1
[308-units] => 1
[305-ctns] => 1
[306-units] => 2
)
And I would like it to look like:
Array
(
[71] => Array
(
[ctns] => 1
[units] => 1
)
[308] => Array
(
[units] => 1
)
[305] => Array
(
[ctns] => 1
)
[306] => Array
(
[units] => 2
)
)
Is this possible?

This should do it
$merged = array();
foreach($a as $k=>$v){
$t = explode('-',$k);
$id = intval($t[0]);
if(!array_key_exists($id, $merged))
$merged[$id] = array();
$merged[$id][$t[1]] = $v;
}
EDIT:
Sorry you should use explode instead of split.

Yes, but you need to loop (note: array_map can also work, but this example is more explicit):
$fin = array();
foreach( $complex as $item => $val )
{
$pieces = explode('-', $item);
$fin[$pieces[0]] = isset($fin[$pieces[0]])?:array();
$fin[$pieces[0]][$pieces[1]] = $val;
}

Find below code to restructure a predefined php array
<?php
$newArray=array();
$result = array("71-ctns"=>1,"71-units"=>1,"308-ctns"=>1,"308-units"=>1,"305-units"=>1,"306-units"=>2);
if(is_array($result) && count($result)>0) {
foreach($result as $key=>$val) {
$getKeyArray = explode("-",$key);
$newArray[$getKeyArray[0]][$getKeyArray[1]] =$val;
}
}
print"<pre>";
print_r($newArray);
?>

Related

php compare multidimensional array equal

Array 1
Array
(
[0] => Array
(
[prid] => 110
[size_id] => 24
)
[1] => Array
(
[prid] => 117
[size_id] => 24
)
[2] => Array
(
[prid] => 174
[size_id] => 24
)
)
Array 2
Array
(
[0] => Array
(
[prid] => 174
[size_id] => 24
)
[1] => Array
(
[prid] => 174
[size_id] => 25
)
[2] => Array
(
[prid] => 163
[size_id] => 24
)
)
I have 2 multidimensional arrays. I want to compare both arrays and resultant arrays are equal in both.
(i.e) The answer is
[prid] => 174
[size_id] => 24
Note:- i am using array_intersect_assoc(),but not working
How can we retrieve this???
Please tell the answer to this
You can use foreach() and array_column() to compare second array value with first array
$final_array = [];
foreach($array2 as $arr){
if(in_array($arr['prid'],array_column($array1,'prid')) && in_array($arr['size_id'],array_column($array1,'size_id'))){
$final_array[$arr['prid']] = $arr;
}
}
$final_array = array_values($final_array);
print_r($final_array);
Output:- https://eval.in/979112
Another easier solution using array_intersect() and array_column()
$final_array = [];
$common_array = array_intersect (array_column($array1,'prid'),array_column($array2,'prid'));
foreach($common_array as $key=>$val){
$final_array[] = $array1[$key];
}
print_r($final_array);
Output:-https://eval.in/979135
$new_array = array();
foreach($array1 as $value1){
foreach($array2 as $value2){
if($value1['prid'] == $value2['prid'] && $value1['size_id'] == $value2['size_id']){
$new_array[] = $value1;
}
}
}
echo print_r($new_array);
You can get this done by using couple of array function and a foreach loop.
Try like this:-
$array1=[
['prid'=>110,'size_id'=>24],
['prid'=>117,'size_id'=>24],
['prid'=>174,'size_id'=>24],
];
$array2=[
['prid'=>174,'size_id'=>24],
['prid'=>174,'size_id'=>25],
['prid'=>163,'size_id'=>24]
];
$prid = array_intersect(array_column($array1,'prid'),array_column($array2,'prid'));
$size_id = array_intersect(array_column($array1,'size_id'),array_column($array2,'size_id'));
$array3 = array_merge($array1,$array2);
$result=[];
foreach ($array3 as $data){
if(in_array($data['prid'],$prid) && in_array($data['size_id'],$size_id)){
$result = $data;
break;
}
}
or like this
$result=[];
foreach ($array1 as $data1){
foreach ($array2 as $data2){
if($data1['prid']==$data2['prid'] && $data1['size_id']==$data2['size_id']){
$result = $data1;
break;
}
}
}
It will give o/p like this

How to transfer one array to another array in php

I have a simple Two array
$ages[] = array("Peter"=>22, "Clark"=>32, "John"=>28);
$ages1[] = array("demo"=>22);
When I print this arrays it should be like following:
Array
(
[0] => Array
(
[Peter] => 22
[Clark] => 32
[John] => 28
)
)
Array
(
[0] => Array
(
[demo] => 22
)
)
But I want to create third array which will be show demo kye value into first array like following:
Array
(
[0] => Array
(
[Peter] => 22
[Clark] => 32
[John] => 28
[demo] => 22
)
)
Can we do two array into single array in PHP like Above
Not sure what are you trying to achieve here...little more context would be helpful. But this is how you can do this,
$ages[] = array("Peter"=>22, "Clark"=>32, "John"=>28);
$ages1[] = array("demo"=>22);
$result[] = array_merge($ages[0],$ages1[0]);
This would do the job.
<?php
$ages[] = array("Peter"=>22, "Clark"=>32, "John"=>28);
$ages1[] = array("demo"=>22);
$output = prepend_array($ages,$ages1);
print_r($output);
// Function to prepend arrays
function prepend_array()
{
$num_args = count(func_get_args());
$new_array = array();
foreach (func_get_args() as $params){
foreach($params as $out_key => $param)
{
foreach($param as $key => $value)
$new_array[$out_key][$key] = $value;
}
}
return $new_array;
}

How To Subtract Values From An Array In PHP?

I am trying to "subtract" the values of an array in php. I used array_diff but it doesn't seem to work for more than one value.
<?php
$array1 = array(1,3,7,10,7);
$array2 = array(1,7);
$result=array_diff($array1,$array2);
print_r($result);
?>
//Output//
Array ( [1] => 3 [3] => 10 )
What I would like to do is return 3,7,10 instead of excluding all 7's. Thanks in advance!
Try:
$array1 = array(1,3,7,10,7);
$removals = Array(1,7);
foreach( $removals as $remove ) {
foreach( $array1 as $key => $value ) {
if ($value === $remove ) {
unset($array1[ $key ]);
break;
}
}
}
print_r($array1); // Array ( [1] => 3 [3] => 10 [4] => 7 )
sort($array1)
print_r($array1); // Array ( [0] => 3 [1] => 7 [2] => 10 )
based on thelastshadows post but shorter and may faster because only one foreach
$array1 = array(1,3,7,10,7);
$removals = Array(1,7);
foreach( $removals as $remove ) {
unset($array1[array_search($remove,$array1)]);
}
sort($array1);
print_r($array1);

PHP Sort Mega-Array by Value

I am looking for some advices how could I sort this kind of Array by 'variant_name' key.
Because Array is really huge I minified it to the looking result state.
...
$filter[2413][1][81][sub_id] = 1;
$filter[2413][1][81][variant_id] = 81;
$filter[2413][1][81][variant_name] = 'Banana';
$filter[2413][2][87][sub_id] = 2;
$filter[2413][2][87][variant_id] = 87;
$filter[2413][2][87][variant_name] = 'Apple';
$filter[2413][3][32][sub_id] = 3;
$filter[2413][3][32][variant_id] = 32;
$filter[2413][3][32][variant_name] = 'Carrot';
...
Keys $filter[x][x][x] are not sequential.
I have tried the sort function I used before but it doesn't work with this kind of Array:
function array_sort_by_column(&$arr, $col, $dir = SORT_ASC) {
$sort_col = array();
foreach ($arr as $key=> $row) {
$sort_col[$key] = $row[$col];
}
array_multisort($sort_col, $dir, $arr);
}
array_sort_by_column($filter[][][], 'variant_name');
My target is modify array by sorting 'variant_name' to 'Apple', 'Banana', 'Carrot' accordingly keeping the array structure.
It's a working code tested from given examples.
Note that my codes still can be optimized, etc. Just take it as my advice.
TL;DR = Use usort().
function mySort($a,$b)
{
$av = "";
$bv = "";
foreach($a as $ak)
$av = $ak['variant_name'];
foreach($b as $bk)
$bv = $bk['variant_name'];
if($av[0] < $bv[0])
return false;
else return true;
}
How to use it? You have to specify which first level array to sort.
usort($filter['2413'],"mySort");
Then the result I got is:
Array
(
[2413] => Array
(
[0] => Array
(
[87] => Array
(
[sub_id] => 2
[variant_id] => 87
[variant_name] => Apple
)
)
[1] => Array
(
[81] => Array
(
[sub_id] => 1
[variant_id] => 81
[variant_name] => Banana
)
)
[2] => Array
(
[32] => Array
(
[sub_id] => 3
[variant_id] => 32
[variant_name] => Carrot
)
)
)
)

How to get particular array value in php?

I have the following $qtips_messages array,
Array
(
[0] => Array
(
[id] => 1
[tips_title] => email_tips
[tips_message] => ex:xxxxx#xyz.com
[tips_key] => key_email
)
[1] => Array
(
[id] => 2
[tips_title] => website_tips
[tips_message] => ex:http://www.yahoo.co.in
[tips_key] => key_website
)
[2] => Array
(
[id] => 3
[tips_title] => zipcode_tips
[tips_message] => ex:60612
[tips_key] => key_zipcode
)
[3] => Array
(
[id] => 4
[tips_title] => phone_tips
[tips_message] => ex:1234567890
[tips_key] => key_phone
)
)
For example, I want to get the tips message for the tip_title 'email_tips'
I have tried with following code,
foreach($qtips_messages as $qtipsArray){
foreach($qtipsArray as $qkey=>$qvalue){
if($qtipsArray['tips_title'] == 'email_tips'){
$emailtipsMessage = $qtipsArray['tips_message'];
}
}
}
When i ran the above code i did not get any value.
What is wrong with this code?
You only need one loop:
$message = null;
foreach ($array as $tips) {
if ($tips['tips_title'] == 'email_tips') {
$message = $tips['tips_message'];
break;
}
}
I'd probably go for something like this though:
$message = current(array_filter($array, function ($tip) { return $tip['tips_title'] == 'email_tips'; }));
echo $message['tips_message'];
$array = array();
foreach($result AS $k =>$val)
$array[$val['tips_key']] = $val['tips_message'];
return $array;
Now the array $array have all the values for q-tips based on their keys...
Hope this will helps you...
try this way.
$array = array();
foreach($qtips_messages AS $k =>$val):
if($val['tips_title']=='email_tips')
{
$array[$val['tips_key']] = $val['tips_message'];
print_r($array);
break;
}
endforeach;

Categories