how to get array from given array in php - php

I've array like this in php:
Array
(
[0] => Array
(
[offer_id] => 62122
[quantity] => 1
)
[1] => Array
(
[offer_id] => 62123
[quantity] => 2
)
[2] => Array
(
[offer_id] => 62124
[quantity] => 2
)
)
I want to create new array from the above array like this:
Array
(
[62122] => 1
[62123] => 2
[62124] => 2
)
and so on. Any help will be appreciated. Thanks in advance.

There is a PHP function that can do this for you: array_column().
The first argument is the array.
The second argument is the key you want as value.
The third (optional) argument is which key should be used as index
$newArray = array_column($array, 'quantity', 'offer_id');
Here's a demo: https://3v4l.org/AANUO

$resultArr = [];
foreach ($org_array as $one){
$resultArr[ $one["offer_id"] ] = $one["quantity"];
}
// $resultArr now contains your desired structure

<?php
$originalArray = [
[
"offer_id" => 62122,
"quantity" => 1
], [
"offer_id" => 62123,
"quantity" => 2
], [
"offer_id" => 62124,
"quantity" => 2
]
];
$newArray = array_column($originalArray, 'quantity', 'offer_id');
print_r($newArray);
// Output :
// Array
// (
// [62122] => 1
// [62123] => 2
// [62124] => 2
// )

Related

how to Grouping array in php

I would like to know how to combine and overwrite the already existed array value in php.
my current array look like:
Array
(
[1149] => 3
[4108] => 5
)
As shown above values, i need expected result in php as below :
Array
(
[0] => Array
(
[offer_id] => 1149
[quantity] => 3
)
[1] => Array
(
[offer_id] => 4108
[quantity] => 5
)
)
How about:
$result = [];
foreach (
[
1149 => 3,
4108 => 3,
] as $key => $value
) {
$result[] = [
'offer_id' => $key,
'quantity' => $value,
];
}
print_r($result);

Shift array elements one level to the right (down one level) when using foreach

Sorry if there's an obvious answer, but I've been trying for hours to google the solution and tried various ways. Basically I'm trying to shift an array within a multidimensional array one level to the right. The output array is to be fed into a program which only accepts the desired format of multidimensional array.
We have ($result):
Array (
[0] => Array (
[id] => 1
[data] => AAA
)
[1] => Array (
[id] => 2
[data] => BBB
)
[2] => Array (
[id] => 3
[data] => CCC
)
)
We want to insert a new array [test] in between the multidimensional array. Desired result:
Array (
[0] => Array (
[test] => Array (
[id] => 1
[data] => AAA
)
)
[1] => Array (
[test] => Array (
[id] => 2
[data] => BBB
)
)
[2] => Array (
[test] => Array (
[id] => 3
[data] => CCC
)
)
)
What I've got so far:
foreach ($result as $key => $value) {
$newarray[] = array($key => $value)
};
return $newarray;
Unfortunately the above inserts an incremental index instead of [test] where we want it to.
Can anyone help?
You could use a loop and index in the same array using the current key. Then update the value with a new array where using test as the key and $v as the value.
$arrays = [
0 => [
"id" => 1,
"data" => "AAA"
],
1 => [
"id" => 2,
"data" => "BBB"
],
2 => [
"id" => 3,
"data" => "CCC"
],
];
foreach ($arrays as $k => $v) {
$arrays[$k] = ['test' => $v];
};
print_r($arrays);
Result
Array
(
[0] => Array
(
[test] => Array
(
[id] => 1
[data] => AAA
)
)
[1] => Array
(
[test] => Array
(
[id] => 2
[data] => BBB
)
)
[2] => Array
(
[test] => Array
(
[id] => 3
[data] => CCC
)
)
)
Demo
Try this:
foreach ($result as $key => $value) {
$newarray[$key] = ['test' => $value];
};
This would be a working and flexible approach:
<?php
$data = [
[
'id' => 1,
'data' => "AAA"
],
[
'id' => 2,
'data' => "BBB"
],
[
'id' => 3,
'data' => "CCC"
]
];
array_walk($data, function(&$entry) {
$entry = [
'test' => $entry
];
});
print_r($data);
The output obviously is:
Array
(
[0] => Array
(
[test] => Array
(
[id] => 1
[data] => AAA
)
)
[1] => Array
(
[test] => Array
(
[id] => 2
[data] => BBB
)
)
[2] => Array
(
[test] => Array
(
[id] => 3
[data] => CCC
)
)
)
$data = [
[
'id' => 1,
'data' => "AAA"
],
[
'id' => 2,
'data' => "BBB"
],
[
'id' => 3,
'data' => "CCC"
]
];
$newArray = array_map(function ($value) {
return ['test' => $value];
}, $data);
var_dump($newArray);
https://secure.php.net/manual/en/function.array-map.php

How to use array_column or foreach to get result

This is my array,with the use of array_column or any loop I want to replace keys as value of next element .I don't want to change parent array index
Array
(
[0] => Array
(
[id] => 11
[total] => 100000
[content] => abc
)
[1] => Array
(
[id] => 22
[total] => 200000
[content] => def
)
)
This is the array I would like to have.
Array
(
[0] => Array
(
[11] => 100000
[content] => abc
)
[1] => Array
(
[22] => 200000
[content] => def
)
)
It is very simple Try this:-
$array = array(
'0'=>array('id'=> 10,'total'=> 100000,'content' => 'abc'),
'1'=>array('id'=> 11,'total'=> 200000,'content' => 'def')
);
foreach($array as $key => $val){
$array[$key][$val['id']] = $val['total'];
unset($array[$key]['total']);
unset($array[$key]['id']);
}
echo "<pre>"; print_r($array); die; // print array data here
Hope it helps!
Using 'foreach' to modify existing array works perfect, but 'array_walk' is just more expressive, because it was designed for this task.
<?php
$array = [
[
'id' => 11,
'total' => 10000,
'content' => 'abc'
],
[
'id' => 22,
'total' => 200000,
'content' => 'def'
]
];
array_walk($array, function(&$row) {
$row[$row['id']] = $row['total'];
unset($row['id'], $row['total']);
});
var_dump($array);
It can be done with array_map function
$new = array_map(function($x) {
return [ $x['id']=> $x['total'], 'content' => $x['content']]; },
$array);
demo

PHP merge 2 array with same key

Hi everyone how I can merge/push one array to another to specific keys? Here is my arrays:
// Array 1
Array
(
[1] => Array
(
[name] => test
)
)
// Array 2
Array
(
[1] => Array
(
[age] => 25
)
)
I want this result:
Array
(
[1] => Array
(
[name] => test
[age] => 25
)
)
I use PHP and will be very grateful if someone help me. Thanks in advance!
$arr = [ 1 => [ "name" => "Test" ] ];
$arr2 = [ 1 => [ "age" => 25 ] ];
foreach ($arr as $key => $value) {
if (isset($arr2[$key])) {
$arr[$key] = array_merge($value,$arr2[$key]);
}
}
print_r($arr);
Check the output at https://eval.in/602680
Just add them together:
<?php
$array1 = array('name' => 'test');
$array2 = array('age' => 21);
var_dump($array1 + $array2);

How do I move array child value to parent key?

I need some clarity as to what PHP function can achieve what I'm aiming for.
This is a PHP array I have:
Array
(
[0] => Array
(
[id] => 6
[index] => 1
[active] => 1
[name] => MyName
)
[1] => Array
(
[id] => 1
[index] => 2
[active] => 1
[name] => YourName
)
[2] => Array
(
[id] => 2
[index] => 4
[active] => 1
[name] => TheirName
)
}
I want to take the "index" value and make it a KEY of that array parent, so the array would become this:
Array
(
[1] => Array
(
[id] => 6
[index] => 1
[active] => 1
[name] => MyName
)
[2] => Array
(
[id] => 1
[index] => 2
[active] => 1
[name] => YourName
)
[4] => Array
(
[id] => 2
[index] => 4
[active] => 1
[name] => TheirName
)
}
Can anyone please tell me how would I do this in PHP?
Thank you in advance.
you can use: array_column($array, null, 'index');
is the better solution, but only work for >= 5.5 php version
Not the most elegant solution, but it works (it doesn't actually move the array, it just generates a new one that corresponds to your requirements):
$resultArr = array();
foreach ($mainArr as $value) {
$resultArr[$value['index']] = $value;
}
unset($mainArr); // or $mainArr = $resultArr;
This way you won't overwrite any existing keys in your original array.
$a = array
(
0 => array
(
"id" => 6,
"index" => 1,
"active" => 1,
"name" => "MyName"
),
1 => Array
(
"id" => 1,
"index" => 2,
"active" => 1,
"name" => "YourName"
),
2 => Array
(
"id" => 2,
"index" => 4,
"active" => 1,
"name" => "TheirName"
)
);
$newArray = array();
foreach ($a as $foo) {
$newArray[$foo['index']] = $foo;
}
You have to do it manually with:
$input = array( /* your data */ );
$output = array();
foreach ( $input as $values ) {
$output[ $values['index'] ] = $values;
}
public static function normArray($key, $inputArray)
{
$outputArray = array();
foreach ($inputArray as $item) {
$index = intval($item[$key]);
$outputArray[$index] = $item;
}
return $outputArray;
}

Categories