JS array to combine into one array - php

I have following js array using serialisedArray -
Array
(
[0] => Array
(
[name] => sub_maintenance_template[1][maintenance_location_id]
[value] => 54321
)
[1] => Array
(
[name] => sub_maintenance_template[1][maintenance_problem_id]
[value] => 65432
)
[2] => Array
(
[name] => sub_maintenance_template[1][maintenance_priority_id]
[value] => 76896
)
[3] => Array
(
[name] => sub_maintenance_template[1][description]
[value] => sample description
)
)
Expected array -
[sub_maintenance_template] => Array (
[1] =>
(
[maintenance_location_id]=> 54321
[maintenance_problem_id]=> 65432
[maintenance_priority_id]=>76896
[description]=> sample description
)
)
I tried like this-
foreach( $tableData as $key => $value ) {
echo $key;
$newArray['sub_maintenance_template'][3][] = $value['name'];
$newArray['sub_maintenance_template'][3][] = $value['value'];
}
Even though I iterate it through foreach but failed to get desired output. IS there any way to get desired one?

It would be better to pass these as actual arrays in GET or POST, but since the string in name is how arrays would be passed in a URL query string, you can use parse_str:
foreach($array as $values) {
parse_str("{$values['name']} = {$values['value']}", $result);
}
print_r($result);
Or another way; extract and build key/value pairs to build a query string and then parse it:
parse_str(http_build_query(array_column($array, 'value', 'name')), $result);
print_r($result);

Related

Unwrap array inside another array PHP

I am getting a string separated from commas and I am trying to split them into an array, which works. The only problem is that there is an outer array wrapping the array I want to use. So I don't want to use the $excludes[0] when passing the array to a function. Does anyone know a function I can use to unwrap the array inside $excludes[0]
$excludes = [];
Array ( [0] => Array (
[0] => company_logo,
[1] => social_links,
[2] => rss_link,
[3] => telephone ) )
My expected results would be the below.
Array (
[0] => company_logo,
[1] => social_links,
[2] => rss_link,
[3] => telephone
)
You can simple do :
1st Option:
print_r(array_shift($excludes));
2nd Option:
$new_array = $excludes[0];
print_r($new_array);
Hope this will work
check this
$excludes = Array ( 0 => Array (
0 => 'company_logo',
1 => 'social_links',
2 => 'rss_link',
3 => 'telephone' ) ) ;
$excludes=$excludes[0];
print_r($excludes);die();

How to create an array with the values of a nested array?

I have a multi-dimensional array that looks like this:
Array
(
[0] => Array
(
[name] => nonce
[value] => 4OdIiR6JhZ,1565652176,9c1abd8d4e7c717bb1c8a27552aabce58b3bf4b3
)
[1] => Array
(
[name] => firstName
[value] => Honkey
)
[2] => Array
(
[name] => lastName
[value] => McDonalds
)
)
and I want to get an array that looks like this:
Array
(
[nonce] => 4OdIiR6JhZ,1565652176,9c1abd8d4e7c717bb1c8a27552aabce58b3bf4b3
[firstName] => Honkey
[lastName] => McDonalds
)
I know that I could accomplish this by doing a foreach loop and creating a new array.
$newForm = [];
foreach ($something as $index => $item) {
$newIndex = $item['name'];
$newForm[$newIndex] = $item['value'];
}
But I am wondering if there is a better way to do this (perhaps using one of PHP's array functions)?
This is what the 3 parameter form of array_column is perfect for:
$output = array_column($input, 'value', 'name');
Output:
Array
(
[nonce] => 4OdIiR6JhZ,1565652176,9c1abd8d4e7c717bb1c8a27552aabce58b3bf4b3
[firstName] => Honkey
[lastName] => McDonalds
)
Demo on 3v4l.org
With foreach,
$result = [];
foreach($array as $v){
$result[$v["name"]] = $v["value"];
}
Check Demo

How to delete the value inside the array in PHP

I want to delete the ['Phone'] inside my array.
i tried foreach and unset but only the first array delete the ['Phone'].
my example array below.
Array
(
[0] => Array
(
[Name] => ads
[Phone] => 32132
)
[1] => Array
(
[Name] => ads
[Phone] => 321322
)
[2] => Array
(
[Name] => ads
[Phone] => 3213222
)
)
and my expected array.
Array
(
[0] => Array
(
[Name] => ads
)
[1] => Array
(
[Name] => ads
)
[2] => Array
(
[Name] => ads
)
)
You can use array_walk
array_walk($arr, function(&$v, $k){
unset($v['Phone']);
});
If all you want is to fetch the names, you can just pluck those using array_column().
$array = array_column($array, "Name");
Live demo at https://3v4l.org/VJTli
You can use map() function of Laravel Collection. and alter your original array however you want.
$newArray = collect($oldArray)->map(function($element) {
return [
'name' => $element['name'];
];
})->toArray();
Qirel's answer is faster I would imagine if you only have "name" and "phone". Another alternative that is easy on the eyes is:
foreach ($array as &$val) {
unset($val["Phone"]);
}

Array push to one of my multidimentional array

I have array like below. I would like to push it to 0 element array.
$csvdata is contain original array $pushHeaderSpec variable is what i want to push into original array i have also tried array_merge but not work as expected merge well on output only but when i print original data in csv it is not there.i m generating $csvdata array first and then append this array on last.
Array
(
[Ruder] => no value need on this
[Glas] => no value need on this
[Not] => no value need on this
)
My Multidimention array look something like
Array
(
[0] => Array
(
[0] => Sort order
[1] => Sku
[2] => Title
)
)
Many more element on above array so i just want to merge my first array keys to this array on first element that is 0.
I did try using below code but it doesn't give me output what i want.
array_push($csvdata[0],array_keys($pushHeaderSpec));
Output from code
Array
(
[0] => Array
(
[0] => Sort order
[1] => Sku
[2] => Title
[3] =>array (
[0] => Ruder
[1] => Glas
[2] => Not
)
)
)
Expecting Output
Array
(
[0] => Array
(
[0] => Sort order
[1] => Sku
[2] => Title
[3] => Ruder
[4] => Glas
[5] => Not
)
)
It was just
foreach (array_keys($pushHeaderSpec) as $key => $value) {
array_push($csvdata[0],$value);
}
Is that what you are looking for ?
foreach ($pushHeaderSpec as $key => $val) {
$csvdata[0][] = $key;
}

Merge array values in a single array by removing duplicates and null values in PHP

I have an array like this:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1234
[name] => John
)
[1] => Array
(
)
)
[1] => Array
(
[0] => Array
(
[id] => 1234
[name] => John
)
[1] => Array
(
[id] => 5678
[name] => Sally
)
[2] => Array
(
[id] => 1234
[name] => Duke
)
)
My resulting array should be the following (basically merging and getting rid of duplicates and removing null values):
Array
(
[0] => Array
(
[id] => 1234
[name] => John
)
[1] => Array
(
[id] => 5678
[name] => Sally
)
[2] => Array
(
[id] => 1234
[name] => Duke
)
)
Is there an easy way to do this using PHP's array functions?
EDIT: So far I have this:
$result = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$result = array_merge($result, $value);
}
}
print_r($result);
Use array_merge to merge the arrays. Then use array_unique to remove duplicates. Finally, to remove null keys, use a loop.
foreach($array as $key=>$value)
{
if(is_null($value) || $value == '')
unset($array[$key]);
}
Documentation on these methods can be found here.
Flatten your unnecessarily depth array structure by unpacking the subarrays into a merge call.
Filter out the empty subarrays with array_filter().
Remove the duplicate subarrays with array_unique(). (SORT_REGULAR prevents "Warning: Array to string conversion")
Code: (Demo)
var_export(
array_unique(
array_filter(
array_merge(...$array)
),
SORT_REGULAR
)
);
Optionally, call array_values() on the resultant array to re-index it (remove the old keys).

Categories