Create a new array using another array in PHP - php

I have array of categories like this and I want to merge the same value like 1 and 0 index value etc in another new array how it is possible.
Array
(
[0] => Array
(
[id] => 29
[parentId] => 0
[serviceName] => Hair
[parentServiceImg] => 7ffb7f5aa2a210ba927e9de64ef17f93.jpeg
[subCategory] => Array
(
[id] => 30
[parentId] => 29
[serviceName] => Blow Out
[subCategory] => Array
(
[id] => 31
[parentId] => 30
[serviceName] => Add Clients Hair Extensions & Style
[subCategory] => Array
(
[id] => 49
[parentId] => 31
[serviceName] => Hair style color
[servicePrice] => 100
)
)
)
)
[1] => Array
(
[id] => 1
[parentId] => 0
[serviceName] => Makeup
[parentServiceImg] => e00d3576847fef9f717d1de2647ed954.jpeg
[subCategory] => Array
(
[id] => 2
[parentId] => 1
[serviceName] => Special Event Makeup
[subCategory] => Array
(
[id] => 3
[parentId] => 2
[serviceName] => Add Airbrush
[servicePrice] => 232
)
)
)
[2] => Array
(
[id] => 1
[parentId] => 0
[serviceName] => Makeup
[parentServiceImg] => e00d3576847fef9f717d1de2647ed954.jpeg
[subCategory] => Array
(
[id] => 6
[parentId] => 1
[serviceName] => Photoshot Makeup
[subCategory] => Array
(
[id] => 7
[parentId] => 6
[serviceName] => Add Airbrush
[servicePrice] => 8
)
)
)
[3] => Array
(
[id] => 1
[parentId] => 0
[serviceName] => Makeup
[parentServiceImg] => e00d3576847fef9f717d1de2647ed954.jpeg
[subCategory] => Array
(
[id] => 2
[parentId] => 1
[serviceName] => Special Event Makeup
[subCategory] => Array
(
[id] => 4
[parentId] => 2
[serviceName] => Add Lashes
[servicePrice] => 6
)
)
)
[4] => Array
(
[id] => 29
[parentId] => 0
[serviceName] => Hair
[parentServiceImg] => 7ffb7f5aa2a210ba927e9de64ef17f93.jpeg
[subCategory] => Array
(
[id] => 2
[parentId] => 1
[serviceName] => Special Event Makeup
[subCategory] => Array
(
[id] => 47
[parentId] => 29
[serviceName] => Wedding Day Mother of the Bride / Mother of the Groom Hair Touch Ups
[servicePrice] => 5
)
)
)
)
I want to 0 index value merge with 4 index value and get on 0 index of new array and 1 to 3 index value merge and get on 1 index in new array using php. I need help for this. thanks.

It should help you:
<?php
$your_array = [0=>['array1'], 1=>['array2'],2=>['array3'], 3=>['array4'], 4=>['array5']];
$new_array = [];
$first = array_shift($your_array);
$last = array_pop($your_array);
$array = array_merge($first, $last);
$new_array[] = $array;
$new_array[] = $your_array;
?>

Related

How to map multidimensional array

I have an array of arrays that I need to consolidate into another array.
I have tried mapping over it, matching object_id, and gathering all account_ids for said object_id, but all my attempts are not even close as I am trying to learn PHP
This is the original array
[0] => Array
(
[rank] => 0
[id] => 6
[object_id] => 3
[account_id] => 13
)
[1] => Array
(
[rank] => 1
[id] => 7
[object_id] => 3
[account_id] => 565
)
[2] => Array
(
[rank] => 2
[id] => 1823
[object_id] => 825
[account_id] => 563
)
[3] => Array
(
[rank] => 3
[id] => 1824
[object_id] => 825
[account_id] => 564
)
[4] => Array
(
[rank] => 4
[id] => 1825
[object_id] => 825
[account_id] => 565
)
[5] => Array
(
[rank] => 5
[id] => 7187
[object_id] => 3113
[account_id] => 564
)
[6] => Array
(
[rank] => 6
[id] => 7188
[object_id] => 3113
[account_id] => 565
)
This is the desired result
[3] => [13, 565],
[825] => [563, 564, 565],
[3113] => [564, 565],
You need to create a new array by using object_id index.
Example:
<?
$array = array(
array('rank'=>0,'id'=>6,'object_id'=>3,'account_id'=>13),
array('rank'=>1,'id'=>7,'object_id'=>3,'account_id'=>565),
array('rank'=>2,'id'=>1823,'object_id'=>825,'account_id'=>563),
array('rank'=>3,'id'=>1824,'object_id'=>825,'account_id'=>564),
array('rank'=>4,'id'=>1825,'object_id'=>825,'account_id'=>565),
array('rank'=>5,'id'=>7187,'object_id'=>3113,'account_id'=>564),
array('rank'=>6,'id'=>7188,'object_id'=>3113,'account_id'=>565),
);
$newArray = array(); // initiliaze array
foreach ($array as $key => $value) {
$newArray[$value['object_id']][] = $value['account_id']; // save it in group
}
echo "<pre>";
print_r($newArray); // result
?>
Running Example

php array common key sum array of array

I want key rating sum common slug key value. Please help me out.
Array
(
[0] => Array
(
[0] => Array
(
[id] => 6
[slug] => scenario
[avis_id] => 2
[rating] => 0
)
[1] => Array
(
[id] => 7
[slug] => jeu_d_acteur
[avis_id] => 2
[rating] => 9
)
[2] => Array
(
[id] => 8
[slug] => effets_speciaux
[avis_id] => 2
[rating] => 0
)
[3] => Array
(
[id] => 9
[slug] => prises_de_vues
[avis_id] => 2
[rating] => 0
)
[4] => Array
(
[id] => 10
[slug] => bande_son
[avis_id] => 2
[rating] => 6.8
)
)
[1] => Array
(
[0] => Array
(
[id] => 1
[slug] => scenario
[avis_id] => 1
[rating] => 8
)
[1] => Array
(
[id] => 2
[slug] => jeu_d_acteur
[avis_id] => 1
[rating] => 5
)
[2] => Array
(
[id] => 3
[slug] => effets_speciaux
[avis_id] => 1
[rating] => 5
)
[3] => Array
(
[id] => 4
[slug] => prises_de_vues
[avis_id] => 1
[rating] => 6
)
[4] => Array
(
[id] => 5
[slug] => bande_son
[avis_id] => 1
[rating] => 8
)
)
)
output:
Array
(
[slug] => scenario
[rating] => 8
)
[1] => Array
(
[slug] => jeu_d_acteur
[rating] => 14
)
[2] => Array
(
[slug] => effets_speciaux
[rating] => 3
)
[3] => Array
(
[slug] => prises_de_vues
[rating] => 6
)
[4] => Array
(
[slug] => bande_son
[rating] => 14.8
)
)
You can merge them by using ... splat operator and loop through to group by slug and sum rating
$merged = array_merge(...$a);
$r = [];
foreach($merged as $v){
isset($r[$v['slug']])
? ($r[$v['slug']]['rating'] += $v['rating'] )
: ($r[$v['slug']] = ['slug'=>$v['slug'],'rating'=>$v['rating']]);
}
Working example :- https://3v4l.org/odCl6

Joining two multidimensional arrays

I have two arrays each coming from different DB queries, I can't join them in DB so I have to join them using php.
The first:
Array
(
[0] => Array
(
[id] => 23
[host_id] => 5
[pos_id] => 2
[status] => 1
)
[1] => Array
(
[id] => 25
[host_id] => 5
[pos_id] => 1
[status] => 1
)
[2] => Array
(
[id] => 24
[host_id] => 5
[pos_id] => 2
[status] => 1
)
)
And I wanna join it with this array on pos_id and id
Array
(
[0] => Array
(
[id] => 1
[name] => Front
)
[1] => Array
(
[id] => 2
[name] => Back
)
)
so that, the result is:
Array
(
[0] => Array
(
[id] => 23
[host_id] => 5
[pos_id] => 2
[name] => Back
[status] => 1
)
[1] => Array
(
[id] => 25
[host_id] => 5
[pos_id] => 1
[name] => Front
[status] => 1
)
[2] => Array
(
[id] => 24
[host_id] => 5
[pos_id] => 2
[name] => Back
[status] => 1
)
)
The code I have uses an inner loop, matches the value and pushes into array:
foreach($events as &$event) {
foreach($positions as $position) {
if($player[pos_id] == $position[id]){
array_push($event,"name",$position[name]);
}
}
}

sum of array based on key(Group by user)

enter image description here
This is my first array which i get from mysql
Array
([0] => Array
(
[created_by] => 4
[count] => 1
[total] => 300
)
[1] => Array
(
[created_by] => 4
[count] => 1
[total] => 450
)
[2] => Array
(
[created_by] => 3
[count] => 1
[total] => 500
)
)
I need a output as
[0] => Array
(
[created_by] => 4
[count] => 2
[total] => 750
)
[1] => Array
(
[created_by] => 3
[count] => 1
[total] => 500
)
Sum of array total based on created_by
Need to group by created_by count.

PHP add arrays to an array on a specific element

I am trying to merge 2 arrays a single array on a multidimensional array where a given key-value = a value
the first array looks like this:
Array
(
[0] => Array
(
[id] => 4
[subcategories] => Array
(
[0] => Array
(
[id] => 5
[category_order] => 0
[parent_id] => 4
[name] => Audio Equipment
)
[1] => Array
(
[id] => 6
[category_order] => 0
[parent_id] => 4
[name] => Home Entertainment
)
[2] => Array
(
[id] => 7
[category_order] => 0
[parent_id] => 4
[name] => Photography
)
[3] => Array
(
[id] => 8
[category_order] => 0
[parent_id] => 4
[name] => Portable Audio
)
[4] => Array
(
[id] => 9
[category_order] => 0
[parent_id] => 4
[name] => Televisions
)
)
)
)
and the second like this:
Array
(
[0] => Array
(
[id] => 10
[parent_id] => 5
[name] => Amplifiers & Receivers
)
[1] => Array
(
[id] => 11
[parent_id] => 5
[name] => Audio Systems
)
[2] => Array
(
[id] => 12
[parent_id] => 5
[name] => Cassette Decks
)
[3] => Array
(
[id] => 13
[parent_id] => 5
[name] => CD Players
)
[4] => Array
(
[id] => 14
[parent_id] => 5
[name] => Radios
)
[5] => Array
(
[id] => 15
[parent_id] => 5
[name] => HiFi Speakers
)
)
What I want to do is add each of the second arrays to a sub array of the first multidimensional array where the parent_id of the second array = the id of the subcategories array of the first array so it will look like this:
array
(
[0]=> Array
(
[id] => 4
[subcategories] => Array
(
[0] => Array
(
[id] => 5
[category_order] => 0
[parent_id] => 4
[name] => Audio Equipment
[subsubcategories] = array
(
[id] => 10
[parent_id] => 5
[name] => Amplifiers & Receivers
)
)
Something like this should work just rename the array names because you didn't provide them. But I think you'll get the idea :) Mainly you loop through all subcategories with foreach loop or another you'll get the parent id and can access the main array with that parent id and save the sub categories info in there.
foreach( $sub_array as $item ) {
$main_array[ category_id ][ $item[ 'parent_id' ] ][ 'subsubcategories' ] = $item;
}

Categories