Can't get my Hash::combine right in CakePHP - php

I have an array that I'm trying to remodel with Hash::combine.
This is my array
Array
(
[0] => Array
(
[T1] => Array
(
[second] => APPLES
[Color] => GREEN
[Color_en] => #99cc66
[nombre] => 56
)
)
[1] => Array
(
[T1] => Array
(
[second] => APPLES
[Color] => BLUE
[Color_en] => #0099ff
[nombre] => 678
)
)
[2] => Array
(
[T1] => Array
(
[second] => BANANAS
[Color] => GREEN
[Color_en] => #99cc66
[nombre] => 366
)
)
[3] => Array
(
[T1] => Array
(
[second] => BANANAS
[Color] => BLUE
[Color_en] => #0099ff
[nombre] => 2000
)
)
)
And what I want to achieve is the array below :
Array
(
[0] => Array
(
[0] => Array
(
[y] => 56
[color] => #99cc66
)
[1] => Array
(
[y] => 678
[color] => #0099ff
)
)
)
[1] => Array
(
[0] => Array
(
[y] => 366
[color] => #99cc66
)
[1] => Array
(
[y] => 2000
[color] => #0099ff
)
)
)
So first array is "APPLES" and second is "BANANAS", "y" is "nombre and "Color_en" becomes "color".
I've tried so many different ways with Hash::combine but I'm out of ideas how tho do this.

This is outside the scope of Hash::combine(). You can't use it to rename array indices.
The closest thing you could probably do is combine the array values by {n}.T1.second to group the fruits together like this:-
Hash::combine($array, '{n}.T1.nombre', '{n}.T1', '{n}.T1.second');
For what you are trying to achieve you will need to rebuild the array yourself using a foreach loop. Something like this:-
$data = [];
foreach ($array as $value) {
$data[$value['T1']['second']][] = [
'y' => $value['T1']['nombre'],
'color' => $value['T1']['Color_en']
];
}

Related

I am trying to condense an array in PHP

Array
(
[3M] => Array
(
[0] => Array
(
[name] => 3M
[price] => 158.15
)
[440] => Array
(
[name] => 3M
[price] => 156.69
)
)
[AO Smith] => Array
(
[1] => Array
(
[name] => AO Smith
[price] => 47.29
)
[441] => Array
(
[name] => AO Smith
[price] => 47.19
)
)
So I have an Array that is above^^^. I would like to get it into a condensed array format. I need a function that loops through the above and outputs it in the format below.
Array
(
[3M] => Array
(
[price1] => 158.15
[price2] => 156.69
)
[AO Smith] => Array
(
[price1] => 47.29
[price2] => 47.19
)
)
Above is how I would like the data oriented.
Thanks for the help.
What you'll find is the format you want is not good and not as usable or flexible. This however will give you a better format. name and price are descriptive, price1 and price2 are no different than 0 and 1:
foreach($array as $key => $values) {
$result[$key] = array_column($values, 'price');
}
Yields:
Array
(
[3M] => Array
(
[0] => 158.15
[1] => 156.69
)
[AO Smith] => Array
(
[0] => 47.29
[1] => 47.19
)
)

How to merge two arrays (song&title) into one multidimensional array?

I have two arrays:
Array
(
[0] => Black
[1] => Five Hours
[2] => Bvulgari
[3] => Imaginary
)
Array
(
[0] => Pearl Jam
[1] => Deorro
[2] => Daddy's Groove
[3] => Brennan Heart
)
I want to be able to achieve the following:
I want to have the song title and the Artist into one 'dimension' of the array,
This is how I want it to be:
Array
(
[0] => Array
(
[0] => Black
[1] => Pearl Jam
)
[1] => Array
(
[0] => Five Hours
[1] => Deorro
)
[2] => Array
(
[0] => Bvulgari
[1] => Daddy's Groove
)
[3] => Array
(
[0] => Imaginary
[1] => Brennan Heart
)
)
The two arrays that are the input can change depending on the requestor's input.
You can use a simple foreach loop:
$songs_artists = array();
foreach ($songs as $key => $title) {
$songs_artists[] = array($title, $artists[$key]);
}
Output is as you desire. Demo on 3v4l.org
As an alternative you could use array_map and use both array's:
$result = array_map(function ($s, $a) {
return [$s, $a];
}, $songs, $artists);
print_r($result);
See a Php demo
Result
Array
(
[0] => Array
(
[0] => Black
[1] => Pearl Jam
)
[1] => Array
(
[0] => Five Hours
[1] => Deorro
)
[2] => Array
(
[0] => Bvulgari
[1] => Daddy's Groove
)
[3] => Array
(
[0] => Imaginary
[1] => Brennan Heart
)
)
Another way to do is Array_Combine
$songs_artists = array_combine($songs,$artists);
Array (
[Black] => Pearl Jam
[Five Hours] => Deorro
[Bvulgari] => Daddy's Groove
[Imaginary] => Brennan Heart
)

How to convert array first value as key and second value as value

Hi I am working on some array operations.
I need to convert first value of array as key and second value of array as value.
I have one variable $testArray which stores array like below.
Array
(
[0] => Array
(
[0] => Color
[1] => White on Red
)
[1] => Array
(
[0] => Depicted Text
[1] => EMPTY
)
[2] => Array
(
[0] => Depth [Nom]
[1] => 0.004 in
)
[3] => Array
(
[0] => Language
[1] => English
)
[4] => Array
(
[0] => Length [Nom]
[1] => 10 in
)
[5] => Array
(
[0] => Material
[1] => Adhesive Vinyl
)
[6] => Array
(
[0] => Mounting
[1] => Surface
)
[7] => Array
(
[0] => Width [Nom]
[1] => 14 in
)
[8] => Array
(
[0] => Wt.
[1] => 0.056 lb
)
)
Expected output :
Array
(
[0] => Array
(
[Color] => White on Red
)
[1] => Array
(
[Depicted Text] => EMPTY
)
[2] => Array
(
[Depth [Nom]] => 0.004 in
)
[3] => Array
(
[Language] => English
)
[4] => Array
(
[Length [Nom]] => 10 in
)
[5] => Array
(
[Material] => Adhesive Vinyl
)
[6] => Array
(
[Mounting] => Surface
)
[7] => Array
(
[Width [Nom]] => 14 in
)
[8] => Array
(
[Wt.] => 0.056 lb
)
)
I have already tried with array function array_keys and array_values but it won't working
Simple solution using array_map function:
$result = array_map(function($v){
return [$v[0] => $v[1]];
}, $testArray);
Assuming that structure will always be the same, you could do this:
$output = array();
foreach($testArray as $v){
$output[] = array($v[0] => $v[1]);
}
See it in action here.

Replace numeric array keys with associative keys from array

I have a dataset similar to this in which I am trying to replace the numeric key values within DATA to the corresponding values in COLUMNS. I can do this in a loop but I don't think I'm doing it in the most efficient way possible. Can anyone suggest any nice functions that I haven't considered to accomplish this?
Existing Style
stdClass Object
(
[COLUMNS] => Array
(
[0] => MATCHID
[1] => SEASON
[2] => COMPETITION
[3] => ROUNDID
[4] => ROUNDSORT
[5] => ROUNDNAME
)
[DATA] => Array
(
[0] => Array
(
[0] => 141627
[1] => 2013/2014
[2] => The Scottish Cup
[3] => 18
[4] => 11
[5] => Final
)
[1] => Array
(
[0] => 140895
[1] => 2013/2014
[2] => The Scottish Cup
[3] => 16
[4] => 10
[5] => Semi-Final
)
)
)
Desired Style
stdClass Object
(
[COLUMNS] => Array
(
[0] => MATCHID
[1] => SEASON
[2] => COMPETITION
[3] => ROUNDID
[4] => ROUNDSORT
[5] => ROUNDNAME
)
[DATA] => Array
(
[0] => Array
(
[MATCHID] => 141627
[SEASON] => 2013/2014
[COMPETITION] => The Scottish Cup
[ROUNDID] => 18
[ROUNDSORT] => 11
[ROUNDNAME] => Final
)
[1] => Array
(
[MATCHID] => 140895
[SEASON] => 2013/2014
[COMPETITION] => The Scottish Cup
[ROUNDID] => 16
[ROUNDSORT] => 10
[ROUNDNAME] => Semi-Final
)
)
)
foreach ($data->DATA as $key => $array) {
$data->DATA[$key] = array_combine($data->COLUMNS, $array);
}
$data is the object you showed.
Loop trough the data and combine the keys with the data, see array_combine
$data->DATA = array_map(function (array $entry) use ($data) {
return array_combine($data->COLUMNS, $entry);
}, $data->DATA);

PHP Merge array with same keys and one same value

I need to merge a PHP array, this array has 2 arrays into it named "targetXX", I can have 2 or more. Each target have the same keys, for each key I have an array with 2 values a and b, a is always the same in both targets, but I need to merge both B values like this:
Array
(
[0] => Array
(
[target] => hitcount(stats.asdf1.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 1200
[1] => 1392282200
)
[1] => Array
(
[0] => 1400
[1] => 1392282260
)
[2] => Array
(
[0] => 600
[1] => 1392282320
)
[3] => Array
(
[0] => 200
[1] => 1392282380
)
[4] => Array
(
[0] => 400
[1] => 1392282440
)
[5] => Array
(
[0] => 600
[1] => 1392282500
)
)
)
[1] => Array
(
[target] => hitcount(stats.asdf.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 4321
[1] => 1392282200
)
[1] => Array
(
[0] => 76567
[1] => 1392282260
)
[2] => Array
(
[0] => 5556
[1] => 1392282320
)
[3] => Array
(
[0] => 7675
[1] => 1392282380
)
[4] => Array
(
[0] => 2344
[1] => 1392282440
)
[5] => Array
(
[0] => 0999
[1] => 1392282500
)
)
)
Result:
Array
(
[0] => Array
(
[target] => hitcount(stats.asdf1.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 1200
[1] => 1392282200
[2] => 4321
)
[1] => Array
(
[0] => 1400
[1] => 1392282260
[2] => 76567
)
[2] => Array
(
[0] => 600
[1] => 1392282320
[2] => 5556
)
[3] => Array
(
[0] => 200
[1] => 1392282380
[2] => 7675
)
[4] => Array
(
[0] => 400
[1] => 1392282440
[2] => 2344
)
[5] => Array
(
[0] => 600
[1] => 1392282500
[2] => 0999
)
)
)
Use array_merge() to achieve this:
$newArray = array();
foreach ($myArray['target2'] as $key => $innerArr1) {
$newArray['target'][$key] = array_merge(
$myArray['target1'][$key], /* 0th and 1st index */
array($innerArr1[1]) /* 2nd index */
);
}
print_r($newArray);
Output:
Array
(
[target] => Array
(
[0] => Array
(
[0] => 333333
[1] => 13
[2] => 99
)
[1] => Array
(
[0] => 444444
[1] => 15
[2] => 98
)
[2] => Array
(
[0] => 555555
[1] => 17
[2] => 97
)
)
)
Demo
The built-in function array_merge may do the work for you. You need to merge each subarrays in fact, as the array_merge_recursive function doesn't handle indexes.
$newArray = array();
foreach ($myArray['target2'] as $key => $arr) {
$newArray['target'][$key] = array_merge($myArray['target1'][$key], $arr[1]);
}
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
If you have more than 2 keys to merge, you can loop on the algorithm multiple times.

Categories