PHP merge array issues - php

I have an array as such
Array
(
[1] => Array
(
[0] => PrettyName1
[1] => PrettyName2
[2] => PrettyName3
)
[2] => Array
(
[0] => UglyURL1
[1] => UglyURL2
[2] => UglyURL3
)
)
I want to be able to put these into an array for a code-igniter template, well when I push and merge arrays I end up messing the whole thing up. Can someone show me the proper way to merge these arrays? I need something like
Array
(
PrettyName1 => UglyURL1
PrettyName2 => UglyURL2
PrettyName3 => UglyURL3
)

Maybe something like this array_combine($ar[1], $ar[2]) ?

Related

PHP - Re-arrange multi-array so that certain combinations have the same key(index)

I have an array, which looks like this:
Array
(
[sku_0017768] => Array
(
[0] => 0f359ce5bf6dc855f160c9b89b4aada0
)
[sku_0017766] => Array
(
[0] => ff5a47bd699bcb52944d0727a5b443b6
[1] => 0f359ce5bf6dc855f160c9b89b4aada0
)
[sku_0017723] => Array
(
[3] => a3c83832b4089d83d164b3cac596fc7e
)
[sku_0017767] => Array
(
[0] => fb91d14d405be52ce989acd2918ea1a0
)
[sku_8402350] => Array
(
[0] => a3c83832b4089d83d164b3cac596fc7e
[1] => fb91d14d405be52ce989acd2918ea1a0
[2] => ff5a47bd699bcb52944d0727a5b443b6
[3] => d14afce16e477663ebe51fdd65cd5010
)
[sku_8402200] => Array
(
[0] => d14afce16e477663ebe51fdd65cd5010
)
)
Now, this array represents a list of SKU's with one or more 'combination codes'. So, in this example 'sku_0017768' matches product sku_0017766 because they share the same combination code.
sku_8402350 has multiple combination as you can see.
What I need to have now is a new array, where the index(key) of the combination code needs to be same among the other products within the combination.
Output should look like this:
Array
(
[sku_0017768] => Array
(
[0] => 0f359ce5bf6dc855f160c9b89b4aada0
)
[sku_0017766] => Array
(
[0] => 0f359ce5bf6dc855f160c9b89b4aada0
[1] => ff5a47bd699bcb52944d0727a5b443b6
)
[sku_0017723] => Array
(
[0] => a3c83832b4089d83d164b3cac596fc7e
)
[sku_0017767] => Array
(
[0] => fb91d14d405be52ce989acd2918ea1a0
)
[sku_8402350] => Array
(
[0] => fb91d14d405be52ce989acd2918ea1a0
[1] => ff5a47bd699bcb52944d0727a5b443b6
[2] => d14afce16e477663ebe51fdd65cd5010
[3] => a3c83832b4089d83d164b3cac596fc7e
)
[sku_8402200] => Array
(
[2] => d14afce16e477663ebe51fdd65cd5010
)
)
The difficulty I have is that when a key is already in use, the key of the other products within the combination should increase (with 1) as well, until there is an empty key which the combination can use.
Can anybody help me pointing me in the right direction? Feel free to ask if anything is not clear.

Moving keys towards 0 after they are removed

So I have an array:
Array ( [Favorite-Search-Engines] => Array (
[0] => Google
[1] => Yahoo
[2] => Bing
[3] => DuckDuckGo ) )
I want the user to be able to remove a key, but if they remove one:
Array (
[Favorite-Search-Engines] => Array (
[0] => Google
[2] => Bing
[3] => DuckDuckGo ) )
I would like the keys to move down towards 0 like this:
Array (
[Favorite-Search-Engines] => Array (
[0] => Google
[1] => Bing
[2] => DuckDuckGo ) )
Is there any way to do this?
Thanks in advance
The easiest way to to that is to use:
$re_indexed_array = array_values($your_modified_array);

Select every first array value in associative array

I dynamically create this associative file array through a PHP script, and now I am wondering how do I select all values inside the root array?
I'll explain here in code.
This is the array that is currently getting generated everytime:
Array
(
[HTML] => Array
(
[0] => Index.php
)
[Javascript] => Array
(
[0] => Javascript.js
[1] => Jquery.js
)
[0] => New Text Document.txt
[Scripts] => Array
(
[0] => Get_Server_files.Script.php
)
[Style] => Array
(
[0] => General.css
[1] => Menu.css
[2] => Style.css
)
[images] => Array
(
[WelcomeImages] => Array
(
[0] => WelcomeImage0.png
[1] => WelcomeImage1.png
[2] => WelcomeImage2.png
[3] => WelcomeImage3.png
[4] => WelcomeImage4.png
[5] => WelcomeImage5.png
[6] => WelcomeImage6.png
)
[0] => bg.jpg
)
)
From here, how do I select every first value? As in selecting
HTML
Javascript
Scripts
Style
Images.
And not anything that is inside these arrays.
I have searched but everytime it's not quite what I would like.
You can use array_keys() to get the names and then use array_filter() to remove the 0 values:
$names = array_filter(array_keys($data));
print_r($names);
Output:
Array
(
[0] => HTML
[1] => Javascript
[3] => Scripts
[4] => Style
[5] => images
)
Demo.
Use array_keys()
$keys = array_keys($array);

Sort array according to the search string occurence php

I want to sort two dimensional array according to the occurrence of the search string in php. i tried to fetch data according to the relevance of search string . i come across match and against but it will work on the MYISAM but my table is in innodb. so plan to sort the array using any sort function but i cant find out anything.
my search pressure tes string array
Array
(
[0] => pressure
[1] => tes
)
My output array which matches the above string with title is
Array
(
[0] => Array
(
[title] => tests.doc
[link] => http://localhost/test.doc
[snippet] =>
)
[1] => Array
(
[title] => Pressure Testing Your Company
[link] => http://localhost/Pressure_Testing_Your_Companys.pdf
[snippet] => Questions used by the CFO against dimensions critical to success
)
[2] => Array
(
[title] => pressure.doc
[link] => http://localhost/pressure.doc
[snippet] => Templates for services
)
)
In the above array the most relevant array[1] then array[2] and then array[0] should be in this order. i want to sort this array accordingly. my output should be like below:
Array
(
[0] => Array
(
[title] => Pressure Testing Your Company
[link] => http://localhost/Pressure_Testing_Your_Companys.pdf
[snippet] => Questions used by the CFO against dimensions critical to success
)
[1] => Array
(
[title] => pressure.doc
[link] => http://localhost/pressure.doc
[snippet] => Templates for services
)
[2] => Array
(
[title] => tests.doc
[link] => http://localhost/test.doc
[snippet] =>
)
)
please help me!!!!
Look at the example #3, I think that's what you want.
http://php.net/manual/en/function.array-multisort.php
Just tested this, it seems to be working :
$titles = array();
foreach ( $array as $key => $value )
{
$titles[ $key ] = $value['title'];
}
array_multisort( $titles, SORT_ASC , $array );
I would highly recommend to sort your results in your MySQL query, that would be much better performance wise.

How to get key of second argument in in_array?

I have an array (array01) that contains a bunch of sub arrays consisting of two pieces of data each, like:
Array ( [0] => Array ( [0] => 10CC [1] => Dreadlock Holiday )
[1] => Array ( [0] => 10CC [1] => I\'m Not In Love )
[2] => Array ( [0] => 10CC [1] => Dreadlock Holiday ) )
etc...
I have another array (array02) like:
Array ( [66] => Array ( [0] => 10CC [1] => Dreadlock Holiday )
[585] => Array ( [0] => 10CC [1] => I\'m Not In Love )
etc...
I'm successfully using foreach and then in_array to see what array01 elements are in array02. However, what I am struggling to figure out is how to get the id of what element in array2 the hit was on.
So for example, array01's 0 and 2 elements (both 10CC, Dreadlock Holiday), are matched in array02, but how do I get the ID of the element (in this case, 66)?
Thanks for your help.
Have you tried the array_search function?
look for documentation for the array_intersect() in PHP manual. Note that the original keys are preserved so it returns an array with the elements present on the array02 array with the original keys.
Hope it's what you're looking for
Cheers

Categories