This question already has answers here:
PHP: How to identify AND CHANGE duplicate values in an array?
(2 answers)
Closed 7 years ago.
How do I go about getting all of the duplicate values in an array, and assigning an integer to each of them to make them unique. For example:
array(
0 => 'Title',
1 => 'Primary Contact: Name',
2 => 'Primary Contact: Email',
3 = > 'Title',
4 = > 'Title'
);
My goal is to turn that into the following:
array(
0 => 'Title - 1',
1 => 'Primary Contact: Name',
2 => 'Primary Contact: Email',
3 = > 'Title - 2',
4 = > 'Title - 3'
);
You could use a foreach to integrate through the array and use array_keys with the search option to check for the value in the array and update it.
http://php.net/manual/en/function.array-keys.php
Related
This question already has answers here:
PHP check if array contains start of string
(4 answers)
Closed 3 months ago.
I have these 2 arrays:
array(
100 => 'this is some text',
161 => 'prefix1 : this is some text',
224 => 'some other text',
356 => 'prefix2 : some other text',
// ...
)
and
array(
0 => 'prefix1',
1 => 'prefix2',
// ...
)
The first array should not contain the prefixes, so I would like to identify the errors like this as a result:
array(
161 => 'prefix1 : this is some text',
356 => 'prefix2 : some other text',
// ...
)
You can use array_filter with str_contains like:
$a = [
100 => 'this is some text',
161 => 'prefix1 : this is some text',
224 => 'some other text',
356 => 'prefix2 : some other text',
];
$b = ['prefix1','prefix2'];
print_r(array_filter($a, function($a) use ($b){
foreach($b as $pref){
if(str_contains($a, $pref)){
return true;
}
}
}));
Output:
Array
(
[161] => prefix1 : this is some text
[356] => prefix2 : some other text
)
Example:
https://sandbox.onlinephpfunctions.com/c/c55a9
Reference:
array_filter
str_contains
<?
$categoriesID = array("popular","old");
$product => array (
Product 1
'categoryID' => $categoriesID[1],
'Name' => 'Product One',
Product 2
'categoryID' => $categoriesID[2],
'Name' => 'Product Two',
Product 3
'categoryID' => $categoriesID[2],
'Name' => 'Product Two',
Product 4
'categoryID' => $categoriesID[2],
'Name' => 'Product Two',
);
How can I loop through this to reflect that product 1 belongs to category 1, product 2 belongs to category 2, product 3 belongs to category 2 and so on?
I tried the following but no luck..
foreach($product as $key => $pro){
var_dump($categoriesID[$key]);
}
I would really appreciated any suggestions or how what i'm doing wrong.The goal is to insert the relationship into a database table where in order to insert a product a category_id is required.
Your arrays are not written correctly. You got a multi dimensional array here (arrays inside of an array). Read this to understand how they are written and how you can work with them: http://php.net/manual/en/language.types.array.php
If your categories are numeric you should also consider to use numeric values: 1 instead of '1' inside of the $categoriesID array or depending on the database auto casting capability you will get issues inserting strings as decimals.
Here is your given code modified as working example. Ive changed the var_dump output for better readability of the result.
Ive also changed the array indexes you have used since arrays start at 0. If you need the numbers still to start at 1 you could add some nonsense value at the beginning of the array or subtract 1 when accessing the array. Keep in mind that this is an quick & dirty solution to the given problem.
Nevertheless as Patrick Q said you should consider some introduction to PHP.
<?php
$categoriesID = array('1','2');
$product = array (
array(
'categoryID' => $categoriesID[0],
'Name' => 'Product One',
),
array(
'categoryID' => $categoriesID[1],
'Name' => 'Product Two',
),
array(
'categoryID' => $categoriesID[1],
'Name' => 'Product Two',
),
array(
'categoryID' => $categoriesID[1],
'Name' => 'Product Two',
)
);
foreach($product as $key => $value){
echo var_export($value, true) . '<br>';
}
You could further edit Mariusz's answer to do something like this:
foreach($product as $item){
echo $item['Name'].' - '.$item['categoryID'].'<br>';
}
This would give you easy access to both product name and category ID.
I'm working on trying to figure out how to show a search result from closest match to least closest.
Let's assume this is the multidimensional array of results. You will notice that there are arrays with the same "id", but have different "categories". I'm pretending this is a one-to-many relationship. So I'm assuming, for 1 "id", a user might have tagged it to 3 different relevant categories.
$results[] = array(
'id' => 1 ,
'text' => 'this is my first post',
'category' => 'blue'
);
$results[] = array(
'id' => 1 ,
'text' => 'this is my first post',
'category' => 'green'
);
$results[] = array(
'id' => 1 ,
'text' => 'this is my first post',
'category' => 'purple'
);
$results[] = array(
'id' => 2 ,
'text' => 'this is my second post',
'category' => 'blue'
);
$results[] = array(
'id' => 2 ,
'text' => 'this is my second post',
'category' => 'green'
);
Now, let's assume there are criteria that the user selected. I'll show it in array form:
$criterias = array('blue', 'green', 'purple');
Using this example, that means the $results "id" of 1 should show up first, and I want to show it's "text". This is because it scored 3 out of 3 (based on matching the criteria that was set in $criterias). Then following this logic the $results "id" of 2 should show up second because it only scored a 2 out of 3.
The final form what what I'm looking to do is be able to echo out the "text" value from highest score to lowest.
My level of programming in PHP is intermediate, so if you could please demonstrate a less complex solution that an intermediate could understand that would be great.
What I tried and didn't get to work was trying to first try to score it and put it into another multidimensional array and sort it, then echo it, but I couldn't get it to work.
This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 6 months ago.
Hah, I had no idea how else to phrase that. I'm trying to reformat a set of three arrays generated by form field inputs, into something that better matches my models, so I can save the values to the db.
Not sure if the solution should be some array manipulation or that I should change the "name" attribute in my form fields.
currently I have an array of my input data:
array(
'image_id' =>
array
0 => '454' (length=3),
1 => '455' (length=3),
2 => '456' (length=3)
'title' =>
array
0 => 'title1' (length=6),
1 => 'title2' (length=0),
2 => '' (length=6)
'caption' =>
array
0 => 'caption1' (length=8),
1 => '' (length=8),
2 => 'caption3' (length=8)
);
and would like to change it to something like, so I can iterate over and save each array of values to the corresponding resource in my db.
array(
0 =>
array
'image_id' => '454',
'title' => 'title1',
'caption' => 'caption1'
1 =>
array
'image_id' => '455',
'title' => 'title2',
'caption' => ''
2 =>
array
'image_id' => '456',
'title' => '',
'caption' => 'caption3'
);
This would iterate through the array with 2 foreach loops. They would use each other's key to construct the new array, so it would work in any case:
$data = array(
'image_id' => array(454, 455, 456),
'title' => array('title1', 'title2', ''),
'caption' => array('caption1', '', 'caption3')
);
$result = array();
foreach($data as $key => $value) {
foreach ($value as $k => $v) {
$result[$k][$key] = $v;
}
}
This'll do it:
$array = call_user_func_array('array_map', array_merge(
[function () use ($array) { return array_combine(array_keys($array), func_get_args()); }],
$array
));
Assuming though that this data is originally coming from an HTML form, you can fix the data right there already:
<input name="data[0][image_id]">
<input name="data[0][title]">
<input name="data[0][caption]">
<input name="data[1][image_id]">
<input name="data[1][title]">
<input name="data[1][caption]">
Then it will get to your server in the correct format already.
I have a list of codes that need to replace values in an array. This process should leave the other elements in the array unchanged. For example, I have an array that looks like this:
$data=array(
'container_label_1' => '1 gallon',
'container_num_1' => '1',
'container_label_2' => '5 gallon',
'container_num_2' => '1',
'container_label_3' => '2',
'container_num_3' => '5 gallon' );
And I have a second array of variable length that looks like this
$modifier=array(
'1 gallon'=>'1 gallon code',
'5 gallon'=>'5 gallon code',
'10 gallon'=>'10 gallon code'
in the format of:
label value to be replaced=>code
(In actual use the code values I'm using here would be something else that didn't include the container size.)
I want the array to look like this when it's done:
$data=array(
'container_label_1' => '1 gallon code',
'container_num_1' => '1',
'container_label_2' => '5 gallon code',
'container_num_2' => '1',
'container_label_3' => '2',
'container_num_3' => '5 gallon code');
It should only modify container labels (container_label_1, container_label_2,container_label_3, etc). The items in the $modifier array will not necessarily be in the $data array as shown in the example.
It seems like there should be a fairly simple way to accomplish this, but I'm just not thinking of it. I've tried looking for similar cases on here and in the php.net documentation and I was thinking about using array_map, but I just can't seem to wrap my head around how this would work with my situation. I'm looking for something that's more efficient than checking every array item for each item in the modifier array as these arrays are much larger than the example.
I saw something that looked promising here:
http://www.php.net/manual/en/function.array-replace.php
steelpandrummer's post seems to do something close to what I want, but it compares keys and I need to compare values, not keys. I can't do an array flip because my values are not often going to be unique. And array flip would thus lose data.
Any help would be appreciated.
Actually, array_map works ok :
$data = array(
'container_label_1' => '1 gallon',
'container_num_1' => '1',
'container_label_2' => '5 gallon',
'container_num_2' => '1',
'container_label_3' => '2',
'container_num_3' => '5 gallon'
);
function replaceValues($val) {
$modifier = array(
'1 gallon' => '1 gallon code',
'5 gallon' => '5 gallon code',
'10 gallon' => '10 gallon code'
);
return isset($modifier[$val]) ? $modifier[$val] : $val;
}
print_r(array_map('replaceValues', $data));
Result is
Array
(
[container_label_1] => 1 gallon code
[container_num_1] => 1
[container_label_2] => 5 gallon code
[container_num_2] => 1
[container_label_3] => 2
[container_num_3] => 5 gallon code
)
Clean version with lambda function:
array_walk($data, function(&$v, $k) {
global $modifier;
$v = array_key_exists($v, $modifier) ? $modifier[$v] : $v;
});