Creating a multi dimensional Array from two arrays - php

I have the following 2 arrays:
Array 1
(
[0] => Speed
[1] => Grade
[2] => Speed
[3] => Grade
[4] => Speed
[5] => Grade
[6] => Grade
[7] => Speed
[8] => Size
)
Array 2
(
[0] => 5200 rpm
[1] => Red
[2] => 7200 rpm
[3] => Blue
[4] => 8900 rpm
[5] => Green
[6] => Purple
[7] => 10000 rpm
[8] => Big
)
The values are matching each other. For example: Speed - 5200 rpm , Grade - Red and so forth.
I need to make the above like the following :
$collection = array(
"Speed" => array (
5200 rpm,
7200 rpm,
8900 rpm,
10000 rpm
),
"Grade" => array (
Red,
Blue,
Green,
Purple
),
"Size" => array (
Big
)
);
It needs to make an array for each label and store the necessary values into the array. I have tried merging and combining and looping. I am going wrong somewhere.
Can someone please help me out.

Try this code
$array_1 = array('Speed','Grade','Speed','Grade','Speed','Grade','Grade','Speed','Size');
$array_2 = array('5200 rpm','Red','7200 rpm','Blue','8900 rpm','Green','Purple','10000 rpm','Big');
foreach($array_1 as $key=>$elm){
$finalArray[$elm][] = $array_2[$key];
}
echo("<pre>");
print_r($finalArray);
echo("</pre>");
With foreach you can use label for create new array multi-dimensional! Is very easy.
I hope I have helped you, for any questions, please comment

Save array 1 as $description and array 2 as $value.
You need to get the array values of both arrays, using:
$description = array_values($description);
And then you can use array_combine.
$combined = array_combine($description, $value);
Related (for array values) Convert an associative array to a simple array of its values in php
PHP Docs for array_combine here.

EDIT
I read the question again and I see that index are the same for array 1 and array 2
$collection = array();
foreach (array1 as $key => $value){
$collection[$value][] = $array2[$key];
}

Related

Handling PHP Array post values and merging - PHP Arrays

I know this won't take much time for experts here. But still please help me out
My Array output is like this
Array ( [0] => 1 [1] => 37 [2] => 1035 ) 1
Array ( [0] => 1 [1] => 37 [2] => 1035 ) mystatusmessage1
Array ( [0] => 4 [1] => 37 [2] => 2925 ) 2
Array ( [0] => 4 [1] => 37 [2] => 2925 ) mystatusmessage2
What I would like to get it is in a single string value like this so that I can insert into database.
1,37,1035,1,mystatusmessage1
4,37,2925,2,mystatusmessage2
How can I achieve that. I'm trying to do with foreach but still I'm not able to do it.
Thanks,
Kimz
use implode function to make string from array for example
if you have array like Array('a','b','c');
implode(',',array('a','b','c') )
will return a,b,c as string
here first argument is your glue by which you want to join string
Here you go.
// Original array
$array = array(0 => 1, 1 => 37, 2 => 1035);
// $_POST array
$_POST = array(1,'mystatusmessage1');
// Jump to the end of array
end($array);
// Merge the post with original array
$newArr = array_merge($array,$_POST);
// Impode
echo implode(",",$newArr);
Repeat with other array.

Sort array based on value ascending or descending php

my array is
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
[6] => 6
)
start value is = 3
Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 6
[4] => 0
[5] => 1
[6] => 2
)
any idea friends..
try
$arr = array(0,1,2,3,4,5,6);
foreach($arr as $k=>$v) {
if($v >= 3)
$a[] = $v;
else
$b[] = $v;
}
$c = array_merge($a, $b);
print_r($c); //Array ( [0] => 3 [1] => 4 [2] => 5 [3] => 6 [4] => 0 [5] => 1 [6] => 2 )
Step 0 (optional if needed):
order the array (heap or some other sort algorithm)
Step 1:
create an output array
Step 2:
loop trough the array and find your start point move the value and the ones after to new array and set the value in the original array to null
Step 3:
loop trough the array and move the remaining values over
DONE
Just do this:
Call asort on the array.
Call array_search to find the index you want to slice.
Call array_slice to cut off the part you want.
Call array_merge to merge them in the right order.
I don't know PHP that well but a quick look at php.net and I suspect this will work.
Seeing how your array is already sorted and 0-indexed, you can just slice and reorganise it:
array_values(array_slice($arr, 3, null, true) + array_slice($arr, 0, 3, true));

using array_intersect_key on a single array

I have an array which looks like this:
Array
(
[0] => Array
(
[Binding] => Video Game
[Brand] => Sony
[Color] => Crystal black
[EAN] => 0151903136010
[Edition] => WiFi
)
[1] => Array(
[Binding] => Console
[Brand] => Nintendo
[Color] => blk n wht
[EAN] => 0045496880866
[Edition] => Deluxe Set
)
What I want to do is to be able to extract only the common keys, the value doesn't matter. The items in this array can range from 2 to 6.
It seems like array_intersect_key is the function that I'm looking for but it takes 2 or more arrays as an argument so I'll have to do something like:
$item_count = count($items);
if($item_count == 2){
$intersection = array_intersect_key($items[0], $items[1]);
}else if($item_count == 3){
$intersection = array_intersect_key($items[0], $items[1], $items[2]);
}
It feels like pretty tedious to do it this way. Any ideas what would be the easier and more elegant way to do this without using ifs? Thanks in advance!
Use call_user_func_array():
//$array is your original array
$result = call_user_func_array('array_intersect_key', $array);

PHP sort array using another array no keys

Hello i have an array in php
Array
(
[0] => red
[1] => blue
[2] => green
[3] => yellow
[4] => purple
[5] => white
)
and i want to sort it using that array
Array
(
[0] =>
[1] => 0
[2] => -1
[3] => -5
[4] => -5
[5] => 9
)
so i want the element with the greatest value on the second array to come first on the first array not its value from the second array but the element it self from the first array to move in the first position on the first array! The second bigger to the second place etc.. elements with the same value don't care me how they will be arranged!
the output i want to get is
Array
(
[0] => white
[1] => blue
[2] => green
[3] => yellow
[4] => purple
[5] => red
)
You can use array_multisort() :
$ar1 = array(/* your SO links */);
$ar2 = array(/* your numbers */);
array_multisort($ar2, SORT_DESC, $ar1);
Documentation here
Use array_multisort.
see http://www.php.net/manual/fr/function.array-multisort.php, follow the "Exemple #1 Trier plusieurs tableaux"
Cordially
Lets call your arrays are $dataArray, and $sortArray respectively
asort($sortArray);
foreach ( $sortArray as $key=>$val ) {
$newArray[] = $dataArray[$key];
}
If you need it reversed, just add in a call to array_reverse() at the end.
I think what the OP wants is to sort the first array by the second array's values.
IE- the second array is the vote count for the first array's web pages.
This could be solved more simply if in the first array you have each element as an array with two elements in it, webpage & vote count
$pages = Array
(
array(
'page' => 'http://stackoverflow.com/questions/640805/open-source-ios-components-reusable-views-controllers-buttons-table-cells-e',
'count' => 0
)
array(
'page' => 'http://stackoverflow.com/questions/3889634/fast-and-lean-pdf-viewer-for-iphone-ipad-ios-tips-and-hints',
'count' => -1
)
// etc for the rest...
)
But since the question asked how to do it in the current data structure:
$sorted_values = asort($pages);
$new_pages = array();
$sorted_keys = array_keys($sorted_values);
foreach($sorted_keys as $page_key)
array_push($new_pages, $pages[$page_key]);

Change value in first of two arrays (multidimensional) PHP

I want to change the number in the first array in a multidimensional array. I have a code that outputs the value to an array and there is no chance for it to start counting from one - in my code. So my idea is to change the value starting from one - after it has been declared. My array look like this:
Array
(
[53] => Array
(
[name] => Volkswagen
[regularePrice] => 2139.00
)
[54] => Array
(
[name] => BMW
[regularePrice] => 2219.00
)
[55] => Array
(
[name] => Chrysler
[regularePrice] => 2399.00
)
)
I want - through a while or for - go through the array and change the values 53 to 1, 54 to 2, 55 to 3 and so on depending on how long the array is.
How do I accomplish this?
The answer is:
array_values($arr);
did you try:
$array = array_values($array);

Categories